-
Notifications
You must be signed in to change notification settings - Fork 0
/
j.scrollLoadImg.js
46 lines (40 loc) · 1.25 KB
/
j.scrollLoadImg.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
* name : j.scrollLoadImg.js
* 对所有含有xSrc属性的图片进行滚动条加载
* author : zk
* date : 2013/05/21
*/
(function($) {
var imgs = $('img[xSrc]');
var errorLoadTxt = 'image load error';
var scrollLoad = function() {
imgs.each(function() {
var $this = $(this);
if (!$this.attr('xSrc')) {//如果没有xSrc这个属性,则跳出
return false;
}
var xSrc = $this.attr('xSrc');
var browserHeight = document.compatMode == 'BackCompat' ? document.body.clientHeight : document.documentElement.clientHeight;
var wScrollTop = $(window).scrollTop();
var thisTop = $this.offset().top;
if (browserHeight + wScrollTop > thisTop) {
console.log('loading img with xSrc:' + xSrc);
$this.attr('src', xSrc).error(function() {
console.log('error loading img with xSrc:' + xSrc);
$this.attr('alt', errorLoadTxt);
});
$this.removeAttr('xSrc');
if ($('img[xSrc]').length == 0) {
$(window).unbind('scroll', scrollLoad);
console.log('scrollLoad unbinded');
}
}
});
};
//在页面添加个标志位,来决定是否允许滚动加载图片
if (typeof scrollLoadFlag !== 'undefined' && !scrollLoadFlag) {
return false;
} else {
$(window).bind('scroll', scrollLoad);
}
})(jQuery);