/
lazy-component.js
55 lines (54 loc) · 1.17 KB
/
lazy-component.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
import { inBrowser } from './util'
export default (lazy) => {
return {
props: {
tag: {
type: String,
default: 'div'
}
},
render (h) {
if (this.show === false) {
return h(this.tag)
}
return h(this.tag, null, this.$slots.default)
},
data () {
return {
el: null,
state: {
loaded: false
},
rect: {},
show: false
}
},
mounted () {
this.el = this.$el
lazy.addLazyBox(this)
lazy.lazyLoadHandler()
},
beforeDestroy () {
lazy.removeComponent(this)
},
methods: {
getRect () {
this.rect = this.$el.getBoundingClientRect()
},
checkInView () {
this.getRect()
return inBrowser &&
(this.rect.top < window.innerHeight * lazy.options.preLoad && this.rect.bottom > 0) &&
(this.rect.left < window.innerWidth * lazy.options.preLoad && this.rect.right > 0)
},
load () {
this.show = true
this.state.loaded = true
this.$emit('show', this)
},
destroy () {
return this.$destroy
}
}
}
}