-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathinfinite-scroll.js
More file actions
99 lines (96 loc) · 2.88 KB
/
infinite-scroll.js
File metadata and controls
99 lines (96 loc) · 2.88 KB
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
import $ from '../../shared/dom7.js';
import { bindMethods } from '../../shared/utils.js';
const InfiniteScroll = {
handle(el, e) {
const app = this;
const $el = $(el);
const scrollTop = $el[0].scrollTop;
const scrollHeight = $el[0].scrollHeight;
const height = $el[0].offsetHeight;
let distance = $el[0].getAttribute('data-infinite-distance');
const virtualListContainer = $el.find('.virtual-list');
let virtualList;
const onTop = $el.hasClass('infinite-scroll-top');
if (!distance) distance = 50;
if (typeof distance === 'string' && distance.indexOf('%') >= 0) {
distance = (parseInt(distance, 10) / 100) * height;
}
if (distance > height) distance = height;
if (onTop) {
if (scrollTop < distance) {
$el.trigger('infinite', e);
app.emit('infinite', $el[0], e);
}
} else if (scrollTop + height >= scrollHeight - distance) {
if (virtualListContainer.length > 0) {
virtualList = virtualListContainer.eq(-1)[0].f7VirtualList;
if (virtualList && !virtualList.reachEnd && !virtualList.params.updatableScroll) {
return;
}
}
$el.trigger('infinite', e);
app.emit('infinite', $el[0], e);
}
},
create(el) {
const $el = $(el);
const app = this;
function scrollHandler(e) {
app.infiniteScroll.handle(this, e);
}
$el.each((element) => {
// skip if already initialized
if (element.f7InfiniteScrollHandler) return;
element.f7InfiniteScrollHandler = scrollHandler;
element.addEventListener('scroll', element.f7InfiniteScrollHandler);
});
},
destroy(el) {
const $el = $(el);
$el.each((element) => {
element.removeEventListener('scroll', element.f7InfiniteScrollHandler);
delete element.f7InfiniteScrollHandler;
});
},
};
export default {
name: 'infiniteScroll',
create() {
const app = this;
bindMethods(app, {
infiniteScroll: InfiniteScroll,
});
},
on: {
tabMounted(tabEl) {
const app = this;
const $tabEl = $(tabEl);
const $isEls = $tabEl.find('.infinite-scroll-content');
if ($tabEl.is('.infinite-scroll-content')) $isEls.add($tabEl);
$isEls.each((el) => {
app.infiniteScroll.create(el);
});
},
tabBeforeRemove(tabEl) {
const $tabEl = $(tabEl);
const app = this;
const $isEls = $tabEl.find('.infinite-scroll-content');
if ($tabEl.is('.infinite-scroll-content')) $isEls.add($tabEl);
$isEls.each((el) => {
app.infiniteScroll.destroy(el);
});
},
pageInit(page) {
const app = this;
page.$el.find('.infinite-scroll-content').each((el) => {
app.infiniteScroll.create(el);
});
},
pageBeforeRemove(page) {
const app = this;
page.$el.find('.infinite-scroll-content').each((el) => {
app.infiniteScroll.destroy(el);
});
},
},
};