Skip to content

Commit c7e7870

Browse files
committed
Lazy magic
1 parent ccb9d83 commit c7e7870

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

angular-lazy-img.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
angular.module('angularLazyImg', []);
2+
3+
angular.module('angularLazyImg', []).factory('LazyImgMagic', [
4+
'$window', '$document', 'lazyImgConfig', function(lazyImgConfig){
5+
'use strict';
6+
7+
var winWidth, winHeight, images, count, isListening, options;
8+
var checkImagesT, saveWinOffsetT;
9+
10+
images = [];
11+
isListening = false;
12+
options = lazyImgConfig.getOptions();
13+
14+
function saveWinOffset(){
15+
winHeight = window.innerHeight;
16+
winWidth = window.innerWidth;
17+
}
18+
19+
// http://remysharp.com/2010/07/21/throttling-function-calls/
20+
function throttle(fn, threshhold, scope) {
21+
var last, deferTimer;
22+
return function () {
23+
var context = scope || this;
24+
var now = +new Date,
25+
args = arguments;
26+
if (last && now < last + threshhold) {
27+
clearTimeout(deferTimer);
28+
deferTimer = setTimeout(function () {
29+
last = now;
30+
fn.apply(context, args);
31+
}, threshhold);
32+
} else {
33+
last = now;
34+
fn.apply(context, args);
35+
}
36+
};
37+
}
38+
39+
checkImagesT = throttle(checkImages, 25);
40+
saveWinOffsetT = throttle(saveWinOffset, 50);
41+
saveWinOffset();
42+
43+
function listen(param){
44+
lazyImgConfig.win[param]('scroll', checkImagesT);
45+
lazyImgConfig.win[param]('resize', checkImagesT);
46+
lazyImgConfig.win[param]('resize', saveWinOffsetT);
47+
}
48+
49+
function startListenig(){
50+
isListening = true;
51+
setTimeout(function(){ checkImages(); }, 1);
52+
listen('on');
53+
}
54+
55+
function stopListenig(){
56+
isListening = false;
57+
listen('off');
58+
}
59+
60+
function checkImages() {
61+
for(var i = 0; i < count; i++){
62+
var image = images[i];
63+
if(isElementInView(image)) {
64+
loadImage(image);
65+
images.splice(i, 1);
66+
count--;
67+
i--;
68+
}
69+
}
70+
if(count === 0) stopListening();
71+
}
72+
73+
function loadImage(photo){
74+
if(photo.elem.offsetWidth > 0 && photo.elem.offsetHeight > 0) {
75+
var img = new Image();
76+
img.onerror = function() {
77+
if(photo.onError) photo.onError();
78+
photo.elem.addClass(options.errorClass);
79+
};
80+
img.onload = function() {
81+
setPhotoSrc(photo.elem, photo.src);
82+
photo.addClass(options.successClass);
83+
if(photo.success) photo.success();
84+
};
85+
img.src = photo.src;
86+
}
87+
}
88+
89+
function setPhotoSrc($elem, src){
90+
var isImage = $elem.nodeName.toLowerCase() === 'img';
91+
isImage ? $elem.src = src : $elem.css('background-image', 'url("' + src + '")');
92+
}
93+
94+
function isElementInView(ele) {
95+
var rect = ele.getBoundingClientRect();
96+
var bottomline = winHeight + options.offset;
97+
return (
98+
rect.left >= 0 && rect.right <= winWidth + options.offset && (
99+
rect.top >= 0 && rect.top <= bottomline ||
100+
rect.bottom <= bottomline && rect.bottom >= 0 - options.offset
101+
)
102+
);
103+
}
104+
105+
}
106+
]);

0 commit comments

Comments
 (0)