Skip to content

Commit 7cd753b

Browse files
committed
throttle of scrolling
1 parent 2d06327 commit 7cd753b

File tree

9 files changed

+53
-17
lines changed

9 files changed

+53
-17
lines changed

example/src/App.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export default {
7373
},
7474
7575
fetch() {
76-
for (let i = 0; i < 15; i++) {
76+
for (let i = 0; i < 10; i++) {
7777
this.list.push(this.list.length + 1);
7878
}
7979
this.page++;

lib/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/index.module.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vuejs-loadmore",
3-
"version": "1.0.4",
3+
"version": "1.0.6",
44
"description": "A pull-down refresh and pull-up loadmore scroll component for Vue.js",
55
"entry": "packages/index.js",
66
"main": "lib/index.js",

packages/style/var.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ $refresh-head-text-color: $gray-1;
1111
// loadmore
1212
$loadmore-text-color: $gray-1;
1313
$loadmore-text-font-size: $font-size-md;
14-
$loadmore-text-line-height: 40px;
14+
$loadmore-text-line-height: 30px;
1515
// icon
1616
$padding-base: 4px;
1717
$loading-spinner-color: $gray-2;

packages/utils/scroll.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// get nearest scroll element
22
const overflowScrollReg = /scroll|auto/i;
33

4-
// 遍历父级返回最近一个可滚动的父级元素(overflow-y: auto的元素),找不到则为window(不能为html,body)
4+
// 遍历父级返回最近一个可滚动的父级元素(overflow-y: scroll和auto的元素),找不到则为window(不能为html,body)
55
export function getScroller (el, root = window) {
66
let node = el;
77

packages/utils/throttle.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* 节流函数
3+
*/
4+
5+
export function throttle (handle, wait) {
6+
var now, previous, timer, context, args;
7+
8+
var execute = function () {
9+
handle.apply(context, args);
10+
previous = now;
11+
};
12+
13+
return function () {
14+
context = this;
15+
args = arguments;
16+
17+
now = Date.now();
18+
19+
if (timer) {
20+
clearTimeout(timer);
21+
timer = null;
22+
}
23+
24+
if (previous) {
25+
var diff = wait - (now - previous);
26+
if (diff < 0) {
27+
// 第一次触发可以立即响应
28+
execute();
29+
} else {
30+
// 结束触发后也能有响应
31+
timer = setTimeout(() => {
32+
execute();
33+
}, diff);
34+
}
35+
} else {
36+
execute();
37+
}
38+
};
39+
};

packages/vuejs-loadmore/index.scss

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
}
2828

2929
.vuejs-loadmore {
30+
height: $loadmore-text-line-height;
3031
&-loading,
3132
&-finished-text,
3233
&-error-text {
@@ -35,9 +36,4 @@
3536
line-height: $loadmore-text-line-height;
3637
text-align: center;
3738
}
38-
39-
&-placeholder {
40-
height: 0;
41-
pointer-events: none;
42-
}
4339
}

packages/vuejs-loadmore/index.vue

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
<slot></slot>
2121

2222
<!-- 上拉加载 -->
23-
<div class="vuejs-loadmore">
23+
<div class="vuejs-loadmore" ref="placeholder">
24+
2425
<div class="vuejs-loadmore-loading" v-if="loadLoading && !finished && !error">
2526
<Loading>{{ loadingText || t(`loadmore.loading`) }}</Loading>
2627
</div>
@@ -33,7 +34,6 @@
3334
{{ errorText || t(`loadmore.error`) }}
3435
</div>
3536

36-
<div ref="placeholder" class="vuejs-loadmore-placeholder" />
3737
</div>
3838
</div>
3939
</div>
@@ -47,6 +47,7 @@ import { TimeoutMixin } from '../mixins/timer';
4747
// utils
4848
import { preventDefault } from '../utils/event';
4949
import { getScroller, getScrollTop } from '../utils/scroll';
50+
import { throttle } from '../utils/throttle';
5051
import locale from '../locale';
5152
// Icon
5253
import Loading from '../icon';
@@ -62,7 +63,8 @@ export default {
6263
this.scroller = getScroller(this.$el);
6364
}
6465
65-
bind(this.scroller, 'scroll', this.checkSroll);
66+
// scroll节流
67+
bind(this.scroller, 'scroll', throttle(this.checkSroll, 200));
6668
}),
6769
TimeoutMixin
6870
],
@@ -286,9 +288,8 @@ export default {
286288
}
287289
288290
const placeholderRect = this.$refs.placeholder.getBoundingClientRect();
289-
const bottomDistance = placeholderRect.bottom - scrollerRect.bottom;
290-
// placeholderRect在scrollerRect外面而不是里面(>0),并且在底部位置(<= loadOffset)
291-
const bottomReached = bottomDistance > 0 && bottomDistance <= loadOffset;
291+
// 取绝对值,placeholderRect在scrollerRect容器的正负loadOffset区间则达到底部
292+
const bottomReached = Math.abs(placeholderRect.bottom - scrollerRect.bottom) <= loadOffset;
292293
293294
if (bottomReached) {
294295
this.loadLoading = true;

0 commit comments

Comments
 (0)