imageLoad.js
1.69 KB
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
47
48
49
50
51
52
/**
* Created by moli on 15/7/4.
*/
$.fn.imageLoad = function(beginDis){
// Lazy image array[object]
var lazyArray = $(this);
var images = [];
var murl = null;
// Get page top method
function pageTop() {
return ( document.body.clientHeight < document.documentElement.clientHeight ? document.body.clientHeight : document.documentElement.clientHeight ) + Math.max( document.documentElement.scrollTop, document.body.scrollTop );
}
// Image load method
function imgLoad() {
// Each the images
lazyArray.each(function() {
var oft = $( this ).offset();
if( !oft || this.nodeType === undefined ) return;
if( oft.top <= pageTop() + ( beginDis || 0 ) ) {
var original = $( this ).attr( 'original' ) || $( this ).attr( 'data-original' ),
_src = $( this ).attr( 'src' );
// To display the image
if( original && !$( this ).hasClass( 'load-img' ) ) {
murl = $(this).attr( 'src' );
$(this).attr( 'src', original ).removeAttr( 'original').removeAttr( 'data-original');
$(this).attr("isOriginal", true);
$(this).bind("error", function(){
//if($(this).attr("isOriginal") == "true"){
$(this).attr( 'src', _src ).removeAttr( 'original' ).removeAttr( 'data-original').addClass( 'load-img' );
// };
});
}
}
});
}
// Default Check
imgLoad();
// Bind the scroll event for window
$( window ).bind( 'scroll', function() {
imgLoad();
});
};