JavaScript implements true picture asynchronous loading function

Source: Internet
Author: User

In the picture after the success of the load can be added to the appropriate display (example in the wood is reflected, you can try to find a lot of source code on the Internet, but are false, is not to achieve what we want to save the browser resources of the code, the following I have to modify their own and reference to the realizes the true visual area loading picture method. Implement website Http://www.111cn.net

JavaScript source code is as follows:

var scrollload = (function (options) {
var defaults = (Arguments.length = = 0)? {src: ' xsrc ', time:300}: {SRC:OPTIONS.SRC | | ' Xsrc ', Time:options.time | | 300};
var camelize = function (s) {
Return S.replace (/-(w)/g, function (Strmatch, p1) {
return P1.touppercase ();
});
};
This.getstyle = function (element, property) {
if (arguments.length!= 2) return false;
var value = Element.style[camelize (property)];
if (!value) {
if (Document.defaultview && document.defaultView.getComputedStyle) {
The var css tutorial = Document.defaultView.getComputedStyle (element, NULL);
Value = CSS? Css.getpropertyvalue (property): null;
else if (Element.currentstyle) {
Value = Element.currentstyle[camelize (property)];
}
}
return value = = ' Auto '? ': value;
};
var _init = function () {
var offsetpage = Window.pageyoffset? Window.pageYOffset:window.document.documentElement.scrollTop,
Offsetwindow = offsetpage + number (window.innerheight? window.innerHeight:document.documentElement.clientHeight),
Docimg = Document.images,
_len = Docimg.length;
if (!_len) return false;
for (var i = 0; i < _len; i++) {
var attrsrc = Docimg[i].getattribute (DEFAULTS.SRC),
o = docimg[i], tag = O.nodename.tolowercase ();
if (o) {
Postpage = O.getboundingclientrect (). Top + Window.document.documentElement.scrollTop + Window.document.body.scrollTop; Postwindow = postpage + number (This.getstyle (o, ' height '). Replace (' px ', '));
if ((Postpage > Offsetpage && postpage < Offsetwindow) | | (Postwindow > Offsetpage && postwindow < Offsetwindow)) {
if (tag = = "img" && attrsrc!== null) {
O.setattribute ("src", attrsrc);
}
o = null;
}
}
};
Window.onscroll = function () {
settimeout (function () {
_init ();
}, Defaults.time);
}
};
return _init ();
});
Scrollload ();


Dynamically changed to SRC value

Scrollload ({
SRC: ' usersrc ',//String type
TIME:100//Digital type
})

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.