Skip to content

Commit 3084c72

Browse files
committed
feat: [vue2] Infinite Scroll
1 parent f8e7264 commit 3084c72

File tree

2 files changed

+27
-32
lines changed

2 files changed

+27
-32
lines changed

src/utils/dom.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
export function offset (el) {
2-
let rect = el.getBoundingClientRect()
3-
4-
return {
5-
top: rect.top,
6-
left: rect.left
2+
if (el === window) {
3+
return {top: 0, left: 0}
74
}
5+
let {top, left} = el.getBoundingClientRect()
6+
7+
return {top, left}
88
}
99

1010
export function style (el, property) {
1111
return window.getComputedStyle(el).getPropertyValue(property)
1212
}
1313

1414
export function height (el) {
15+
if (el === window) {
16+
return viewport().height
17+
}
1518
return parseFloat(window.getComputedStyle(el).getPropertyValue('height'), 10)
1619
}
1720

1821
export function width (el) {
22+
if (el === window) {
23+
return viewport().width
24+
}
1925
return parseFloat(window.getComputedStyle(el).getPropertyValue('width'), 10)
2026
}
2127

src/vue-components/infinite-scroll/infinite-scroll.vue

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,36 +21,22 @@ export default {
2121
},
2222
inline: {
2323
type: Boolean,
24-
default: false,
25-
coerce: Boolean
24+
default: false
2625
},
2726
offset: {
2827
type: Number,
2928
default: 0
30-
},
31-
working: {
32-
type: Boolean,
33-
default: true,
34-
coerce: Boolean,
35-
twoWay: true
3629
}
3730
},
3831
data () {
3932
return {
4033
index: 0,
41-
fetching: false
42-
}
43-
},
44-
watch: {
45-
working (value) {
46-
this.scrollContainer[value ? 'addEventListener' : 'removeEventListener']('scroll', this.scroll)
47-
if (value) {
48-
this.scroll()
49-
}
34+
fetching: false,
35+
working: true
5036
}
5137
},
5238
methods: {
53-
scroll () {
39+
poll () {
5440
if (this.fetching || !this.working) {
5541
return
5642
}
@@ -78,7 +64,7 @@ export default {
7864
return
7965
}
8066
if (this.element.closest('body')) {
81-
this.scroll()
67+
this.poll()
8268
}
8369
})
8470
},
@@ -87,29 +73,32 @@ export default {
8773
},
8874
resume () {
8975
this.working = true
76+
this.scrollContainer.addEventListener('scroll', this.poll)
77+
this.poll()
9078
},
9179
stop () {
9280
this.working = false
81+
this.scrollContainer.removeEventListener('scroll', this.poll)
9382
}
9483
},
9584
mounted () {
85+
console.log('mounted', this.working)
9686
this.$nextTick(() => {
97-
this.scroll = Utils.debounce(this.scroll, 50)
87+
this.poll = Utils.debounce(this.poll, 50)
9888
this.element = this.$refs.content
9989
100-
this.scrollContainer = this.inline ? this.$el : this.element.closest('.layout-view')
101-
if (!this.scrollContainer) {
102-
this.scrollContainer = window
103-
}
90+
console.log('before scrollcontainer')
91+
this.scrollContainer = this.inline ? this.$el : Utils.dom.getScrollTarget(this.$el)
92+
console.log('intermediate scrollcontainer')
10493
if (this.working) {
105-
this.scrollContainer.addEventListener('scroll', this.scroll)
94+
this.scrollContainer.addEventListener('scroll', this.poll)
10695
}
10796
108-
this.scroll()
97+
this.poll()
10998
})
11099
},
111100
beforeDestroy () {
112-
this.scrollContainer.removeEventListener('scroll', this.scroll)
101+
this.scrollContainer.removeEventListener('scroll', this.poll)
113102
}
114103
}
115104
</script>

0 commit comments

Comments
 (0)