-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlazyload.js
42 lines (40 loc) · 1022 Bytes
/
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
'use strict';
var ieEvent = !document.addEventListener
var scrollListeners = []
function onScroll (e) {
for (var i = 0; i < scrollListeners.length; i++) {
scrollListeners[i](getScrollTop())
}
}
function getScrollTop () {
return window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
}
function getClientHeight () {
if (document.compatMode == 'BackCompat') {
return document.body.clientHeight
} else {
return document.documentElement.clientHeight
}
}
function getOffsetTop (el) {
return el.getBoundingClientRect().top + getScrollTop()
}
if (ieEvent) {
window.attachEvent('onscroll', onScroll)
} else {
window.addEventListener('scroll', onScroll)
}
module.exports = Reve.directive('lazyload', {
bind: function (src) {
var that = this
var top = getOffsetTop(this.$el)
scrollListeners.push(function (scrollTop) {
if (that._loaded) return
if ( (scrollTop + getClientHeight()) > top ) {
that.$el.src = src
that._loaded = true
}
})
onScroll()
}
})