JavaScript Native Picture lazy loading

Source: Internet
Author: User
Tags gety

One, native JavaScript pictures lazy loading

1. How to use, e.g.

// picture address to bind 

2. Introduce the following native JavaScript code in the page

varLazy = {    "IMG":NULL,    "GetY":function(b) {varA = 0; if(b && b.offsetparent) while(b.offsetparent) A + = B.offsettop, B = b.offsetparent;Elseb && B.y && (A + =b.y); returnA; },    "GetX":function(b) {varA = 0; if(b && b.offsetparent) while(b.offsetparent) A + = B.offsetleft, B = b.offsetparent;Elseb && b.x && (A + =b.x); returnA; },    "Scrolly":function() {        varA =document.documentelement; returnSelf.pageyoffset | | A && a.scrolltop | | Document.body.scrollTop | | 0; },    "SCROLLX":function() {        varA =document.documentelement; returnSelf.pagexoffset | | A && A.scrollleft | | Document.body.scrollLeft | | 0; },    "WindowWidth":function() {        varA =document.documentelement; returnSelf.innerwidth | | A && a.clientwidth | |Document.body.clientWidth; },    "WindowHeight":function() {        varA =document.documentelement; returnSelf.innerheight | | A && a.clientheight | |Document.body.clientHeight; },    "Currentheight":function() {        returnLazy.scrolly () +lazy.windowheight (); },    "Currentwidth":function() {        returnLAZY.SCROLLX () +lazy.windowwidth (); },    "Load":function(d) {lazy.init (); varf = Lazy.currentheight (), B =lazy.currentwidth ();  for(_index = 0; _index < Lazy.Img.length; _index++) {            varA =Lazy.img[_index]; $ (a). attr ("lazy") = = undefined && $ (a). attr ("lazy", "n"); if($ (a). attr ("lazy") = = = "Y")Continue; /*$ (a). Bind ("Error", function () {this.id = = "Subject"? $ (this). attr ("src", ""): $ (this). attr ("src", "H            Ttp://wap.cmread.com/rbc/p/content/repository/ues/image/s109/nopic.png "); });*/            if(d = = Undefined | | d = = "" | | D = =NULL) {                varc = Lazy.gety (a), E =Lazy.getx (a); //E < b && C < f && (a.src = A.getattribute ("Data-src"), $ (a). attr ("lazy", "Y"), A.removeattribute ("DATA-SRC"));C < f && (a.src = A.getattribute ("Data-src"), $ (a). attr ("lazy", "Y"), A.removeattribute ("Data-src")); $ (a). attr ("Data-rel", E); } Else if(d = = "X") {                varc =Lazy.getx (a); C< b && (a.src = A.getattribute ("Data-src"), $ (a). attr ("lazy", "Y")); }        }    },    "Init":function() {        varA = Document.queryselectorall ("Img[data-src")); Lazy.img=A; }};//to bind a picture to a scroll axis lazy Load eventDocument.onscroll =function() {lazy.load ();}; SetTimeout (function() {lazy.load ();},100);//default configuration Lazy loading

JavaScript Native Picture lazy loading

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.