forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLazyLoad.jsx
151 lines (143 loc) · 4.15 KB
/
LazyLoad.jsx
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
143
144
145
146
147
148
149
150
151
import PropTypes from '../../_util/vue-types';
import BaseMixin from '../../_util/BaseMixin';
import addEventListener from '../../vc-util/Dom/addEventListener';
import { initDefaultProps, findDOMNode, getSlot } from '../../_util/props-util';
import warning from '../../_util/warning';
import debounce from 'lodash-es/debounce';
import throttle from 'lodash-es/throttle';
import parentScroll from './utils/parentScroll';
import inViewport from './utils/inViewport';
import { watchEffect } from 'vue';
const lazyLoadProps = {
debounce: PropTypes.bool,
elementType: PropTypes.string,
height: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
offset: PropTypes.number,
offsetBottom: PropTypes.number,
offsetHorizontal: PropTypes.number,
offsetLeft: PropTypes.number,
offsetRight: PropTypes.number,
offsetTop: PropTypes.number,
offsetVertical: PropTypes.number,
threshold: PropTypes.number,
throttle: PropTypes.number,
width: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
};
export default {
name: 'LazyLoad',
mixins: [BaseMixin],
inheritAttrs: false,
props: initDefaultProps(lazyLoadProps, {
elementType: 'div',
debounce: true,
offset: 0,
offsetBottom: 0,
offsetHorizontal: 0,
offsetLeft: 0,
offsetRight: 0,
offsetTop: 0,
offsetVertical: 0,
throttle: 250,
}),
data() {
if (this.throttle > 0) {
if (this.debounce) {
this.lazyLoadHandler = debounce(this.lazyLoadHandler, this.throttle);
} else {
this.lazyLoadHandler = throttle(this.lazyLoadHandler, this.throttle);
}
}
return {
visible: false,
};
},
mounted() {
this.$nextTick(() => {
watchEffect(() => {
if (!this.visible) {
this.lazyLoadHandler(this.$props);
}
});
const eventNode = this.getEventNode();
if (this.lazyLoadHandler.flush) {
this.lazyLoadHandler.flush();
}
this.resizeHander = addEventListener(window, 'resize', this.lazyLoadHandler);
this.scrollHander = addEventListener(eventNode, 'scroll', this.lazyLoadHandler);
});
},
beforeUnmount() {
if (this.lazyLoadHandler.cancel) {
this.lazyLoadHandler.cancel();
}
this.detachListeners();
},
methods: {
getEventNode() {
return parentScroll(findDOMNode(this));
},
getOffset() {
const {
offset,
offsetVertical,
offsetHorizontal,
offsetTop,
offsetBottom,
offsetLeft,
offsetRight,
threshold,
} = this.$props;
const _offsetAll = threshold || offset;
const _offsetVertical = offsetVertical || _offsetAll;
const _offsetHorizontal = offsetHorizontal || _offsetAll;
return {
top: offsetTop || _offsetVertical,
bottom: offsetBottom || _offsetVertical,
left: offsetLeft || _offsetHorizontal,
right: offsetRight || _offsetHorizontal,
};
},
lazyLoadHandler() {
if (!this._.isMounted) {
return;
}
const offset = this.getOffset();
const node = findDOMNode(this);
const eventNode = this.getEventNode();
if (inViewport(node, eventNode, offset)) {
this.setState({ visible: true }, () => {
this.__emit('contentVisible');
});
this.detachListeners();
}
},
detachListeners() {
this.resizeHander && this.resizeHander.remove();
this.scrollHander && this.scrollHander.remove();
},
},
render() {
const children = getSlot(this);
if (children.length !== 1) {
warning(false, 'lazyLoad组件只能包含一个子元素');
return null;
}
const { height, width, elementType: ElementType } = this.$props;
const { visible } = this;
const { class: className } = this.$attrs;
const elStyles = {
height: typeof height === 'number' ? height + 'px' : height,
width: typeof width === 'number' ? width + 'px' : width,
};
const elClasses = {
LazyLoad: true,
'is-visible': visible,
[className]: className,
};
return (
<ElementType class={elClasses} style={elStyles}>
{visible ? children[0] : null}
</ElementType>
);
},
};