-
Notifications
You must be signed in to change notification settings - Fork 43
/
index.js
142 lines (130 loc) · 3.57 KB
/
index.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
138
139
140
141
142
require('./index.styl')
class Progressive {
constructor(option) {
this.el = option.el
this.lazyClass = option.lazyClass || 'lazy'
this.removePreview = option.removePreview || false
this.scale = option.scale || false
this.EVENTS = ['scroll', 'wheel', 'mousewheel', 'resize', 'touchmove']
this.Util = {
throttle(action, delay) {
let timeout = null
let lastRun = 0
return function() {
if (timeout) {
return
}
const elapsed = Date.now() - lastRun
const context = this
const args = arguments
const runCallback = function() {
lastRun = Date.now()
timeout = false
action.apply(context, args)
}
if (elapsed >= delay) {
runCallback()
} else {
timeout = setTimeout(runCallback, delay)
}
}
},
on(el, ev, fn) {
el.addEventListener(ev, fn)
},
off(el, ev, fn) {
el.removeEventListener(ev, fn)
}
}
this.windowHasBind = false
this.lazy = this.Util.throttle( _ => {
this.fire()
}, 300)
this.animationEvent = this.getAnimationEvent()
}
fire() {
if(!this.windowHasBind){
this.windowHasBind = true
this.events(window, true)
}
const lazys = document.querySelectorAll(`${this.el} img.${this.lazyClass}`)
const l = lazys.length
if(l>0){
for (let i = 0; i < l; i++) {
const rect = lazys[i].getBoundingClientRect()
if (rect.top < window.innerHeight && rect.bottom > 0 && rect.left < window.innerWidth && rect.right > 0) {
this.loadImage(lazys[i])
}
}
}else {
this.windowHasBind = false
this.events(window, false)
}
}
events(el, bind) {
if(bind){
this.EVENTS.forEach(evt => {
this.Util.on(el, evt, this.lazy)
})
}else {
this.EVENTS.forEach(evt => {
this.Util.off(el, evt, this.lazy)
})
}
}
loadImage(item) {
const img = new Image()
if (item.dataset) {
item.dataset.srcset && (img.srcset = item.dataset.srcset)
item.dataset.sizes && (img.sizes = item.dataset.sizes)
}
img.src = item.dataset.src
img.className = 'origin'
if(this.scale) {
img.className = 'origin-scale'
}
item.classList.remove('lazy')
img.onload = _ => {
this.mountImage(item, img)
}
img.onerror = _ => {
item.classList.add('lazy')
}
}
getAnimationEvent(){
const el = document.createElement('fake')
const animations = {
"animation" : "animationend",
"OAnimation" : "oAnimationEnd",
"MozAnimation" : "animationend",
"WebkitAnimation": "webkitAnimationEnd"
}
for (let a in animations){
if (el.style[a] !== undefined){
return animations[a]
}
}
}
mountImage(preview, img) {
const parent = preview.parentNode
parent.appendChild(img).addEventListener(this.animationEvent, e => {
e.target.alt = preview.alt || ''
preview.classList.add('hide')
if(this.removePreview){
parent.removeChild(preview)
e.target.classList.remove('origin')
e.target.classList.remove('origin-scale')
}
})
}
}
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports)
exports = module.exports = Progressive
exports.Progressive = Progressive
} else if (typeof define === 'function' && define.amd)
define('Progressive', [], function() {
return Progressive
})
else
this.Progressive = Progressive