Execute JS code after the picture is loaded

Source: Internet
Author: User

$ ("#img"). Load (function () {...});

This is a method provided by jquery, but in IE there will be BUG,IE8 not supported, IE9 above the refresh will not be executed, only forced refresh to execute, so you cannot use the method provided by this jquery

Should use:

Only through the JS onload to trigger the event, of course, the triggered event code can use jquery code (however, IE8 also has a bug, normal refresh when the picture from the cache read, read faster than the code, resulting in the onload code can not be run, the workaround see continue to look down)

document.getElementById (' Topbanner '). onload =function() {        varTop = $ (' #nav '). Offset (). Top; $ (window). Scroll (function() {            varScroll =$ (document). ScrollTop (); varpos = scroll-top; if(Scroll >top) {                $(' #nav '). CSS (' top ', pos + + ' px ')); } Else {                $(' #nav '). CSS (' top ', ' 12px '));        }; });

Fully compatible with IE8:

//first, declare an empty image objectvarIMG =NewImage ();//The OnLoad event is called through an IMG object; an event handler is defined, not executed (similar to defining an event within a tag)Img.onload =function() {    varTop = $ (' #nav '). Offset (). Top; $ (window). Scroll (function() {        varScroll =$ (document). ScrollTop (); varpos = scroll-top; if(Scroll >top) {            $(' #nav '). CSS (' top ', pos + + ' px ')); } Else {            $(' #nav '). CSS (' top ', ' 12px '));    }; });}//The IMG object is then assigned a value of SRC, so that the IMG object has a specific direction before the OnLoad event is started. This will be compatible with IE8.IMG.SRC = document.getElementById (' Topbanner ')). src;$ (window). Resize (function() {Top= $ (' #nav '). Offset (). Top; $ (window). Scroll (function() {        varScroll =$ (document). ScrollTop (); varpos = scroll-top; if(Scroll >top) {            $(' #nav '). CSS (' top ', pos + + ' px ')); } Else {            $(' #nav '). CSS (' top ', ' 12px '));    }; });});}

Execute JS code after the picture is loaded

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.