JavaScript picture preload

Source: Internet
Author: User

Picture preloading is a very common feature that is used by both PC and mobile, especially on mobile, as long as it involves loading more images. The following is used by the mobile side, introducing the Zepto.

<!     DOCTYPE html>;} . IMG-wrap{width:100%;overflow:hidden;background-repeat:no-repeat;background-Size:cover;} . IMG-wrap img{width:100%;} </style>

Lazyload.js:

varLazyload =function(container) {varLazyclsname = "Util-lazyload";//Picture Style namevar$container =container; varThreshold = 1; //gets the element under the container that needs to be lazy loaded    functiongetlazyelements (container) {return$container. Find ("." +lazyclsname); }    //Browser scrolling axis scrolling    varHandlescroll =function() {        varelements =getlazyelements (container); Elements.each (function() {            if(!belowthefold ($ ( This))) {                $( This). Trigger ("appear");        }        });  for(vari = 0, length = elements.length; i < length; i++) {            if(elements[i].loaded = = =true) {$ (elements[i]). Removeclass (Lazyclsname);    }        }    }; if($container. Data ("Lazyload")!==true) {$ (window). On (' Scroll ', Handlescroll); $container. On ("Appear", "." + Lazyclsname,function(e) {vartarget =E.target; if(target.loaded!==true) {preload (target); target.loaded=true;        }        }); $ (container). Data (' Lazyload ',true); }    //picture Pre-loading    functionpreload (Element) {varimg = document.createelement (' img ')), SRC= Element.attr (' Data-origin '); $ (IMG). bind ("Load",function() {element.parent (). CSS (' BackgroundImage ', ' url (' + src + ') '); Element.css (' Visibility ', ' hidden '); }). bind (' Error ',function() {$self. css (' Visibility ', ' visible '); }). attr ("SRC", SRC); }    //determine if the visible area    functionBelowthefold (element, threshold) {varFold = Window.innerheight +window.scrolly; returnFold <=$ (Element). Offset (). Top; }        /*Force Initial Check if images should appear.*/setTimeout (Handlescroll,0); return$ (container);}; Lazyload ($ (". Lazyload-wrap"));

JavaScript picture preload

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.