This repository has been archived by the owner on Oct 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
/
me-lazyload.js
137 lines (123 loc) · 4.4 KB
/
me-lazyload.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
angular.module('me-lazyload', [])
.directive('lazySrc', ['$window', '$document', function($window, $document){
var doc = $document[0],
body = doc.body,
win = $window,
$win = angular.element(win),
uid = 0,
elements = {};
function getUid(el){
var __uid = el.data("__uid");
if (! __uid) {
el.data("__uid", (__uid = '' + ++uid));
}
return __uid;
}
function getWindowOffset(){
var t,
pageXOffset = (typeof win.pageXOffset == 'number') ? win.pageXOffset : (((t = doc.documentElement) || (t = body.parentNode)) && typeof t.scrollLeft == 'number' ? t : body).scrollLeft,
pageYOffset = (typeof win.pageYOffset == 'number') ? win.pageYOffset : (((t = doc.documentElement) || (t = body.parentNode)) && typeof t.scrollTop == 'number' ? t : body).scrollTop;
return {
offsetX: pageXOffset,
offsetY: pageYOffset
};
}
function isVisible(iElement){
var elem = iElement[0],
elemRect = elem.getBoundingClientRect(),
windowOffset = getWindowOffset(),
winOffsetX = windowOffset.offsetX,
winOffsetY = windowOffset.offsetY,
elemWidth = elemRect.width || elem.width,
elemHeight = elemRect.height || elem.height,
elemOffsetX = elemRect.left + winOffsetX,
elemOffsetY = elemRect.top + winOffsetY,
viewWidth = Math.max(doc.documentElement.clientWidth, win.innerWidth || 0),
viewHeight = Math.max(doc.documentElement.clientHeight, win.innerHeight || 0),
xVisible,
yVisible;
if(elemOffsetY <= winOffsetY){
if(elemOffsetY + elemHeight >= winOffsetY){
yVisible = true;
}
}else if(elemOffsetY >= winOffsetY){
if(elemOffsetY <= winOffsetY + viewHeight){
yVisible = true;
}
}
if(elemOffsetX <= winOffsetX){
if(elemOffsetX + elemWidth >= winOffsetX){
xVisible = true;
}
}else if(elemOffsetX >= winOffsetX){
if(elemOffsetX <= winOffsetX + viewWidth){
xVisible = true;
}
}
return xVisible && yVisible;
};
function checkImage(){
angular.forEach(elements, function(obj, key) {
var iElement = obj.iElement,
$scope = obj.$scope;
if(isVisible(iElement)){
iElement.attr('src', $scope.lazySrc);
}
});
}
$win.bind('scroll', checkImage);
$win.bind('resize', checkImage);
function onLoad(){
var $el = angular.element(this),
uid = getUid($el);
$el.css('opacity', 1);
if(elements.hasOwnProperty(uid)){
delete elements[uid];
}
}
return {
restrict: 'A',
scope: {
lazySrc: '@',
animateVisible: '@',
animateSpeed: '@'
},
link: function($scope, iElement){
iElement.bind('load', onLoad);
$scope.$watch('lazySrc', function(){
var speed = "1s";
if ($scope.animateSpeed != null) {
speed = $scope.animateSpeed;
}
if(isVisible(iElement)){
if ($scope.animateVisible) {
iElement.css({
'opacity': 0,
'-webkit-transition': 'opacity ' + speed,
'transition': 'opacity ' + speed
});
}
iElement.attr('src', $scope.lazySrc);
}else{
var uid = getUid(iElement);
iElement.css({
'opacity': 0,
'-webkit-transition': 'opacity ' + speed,
'transition': 'opacity ' + speed
});
elements[uid] = {
iElement: iElement,
$scope: $scope
};
}
});
$scope.$on('$destroy', function(){
iElement.unbind('load');
var uid = getUid(iElement);
if(elements.hasOwnProperty(uid)){
delete elements[uid];
}
});
}
};
}]);