Jquery implements delayed image loading

Source: Internet
Author: User

The Jquery plug-in on the Internet is not easy to use. You will know it after you try it.


Helpless, I wrote a piece of code myself


$ (Function (){
Var CPos = function (x, y) {this. x = x; this. y = y ;}
Var GetObjPos = function (ATarget ){
Var target = ATarget;
Var pos = new CPos (target. offsetLeft, target. offsetTop );

Var target = target. offsetParent;
While (target ){
Pos. x + = target. offsetLeft;
Pos. y + = target. offsetTop;

Target = target. offsetParent;
}
Return pos;
}

Var reload = function (){
$ ('Img. lazy '). each (function (){
Var o = $ (this );
If (! O. attr ('data-original') return true;
If (o. attr ('src') = o. attr ('data-original') return true;

Var top = GetObjPos (this) ['y'];
If (top = 0) return true;

Var t=(document.doc umentElement & document.doc umentElement. scrollTop )? Document.doc umentElement. scrollTop: document. body. scrollTop;

If (top <t + document.doc umentElement. clientHeight + 400 & top + 400> t ){
Var org = o. attr ('data-original ');
O. attr ('src', org );
};
Return true;
})
}
$ (Window). resize (reload)
SetInterval (reload, 1000)
})


The above is called when the window size changes, and is called every second (if it is placed in a rolling event, leading to a card screen)

The above 400 is the pre-loaded height and can be adjusted


Image HTML:

Data-original = "Real Image address" alt = "{$ title}" width = "210px" height = "165px"/>

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.