Skip to content

Commit 06fef3d

Browse files
committed
Change view_height as const value.
1 parent 4a840de commit 06fef3d

File tree

5 files changed

+32
-23
lines changed

5 files changed

+32
-23
lines changed

demo/demo.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
3030
methods: {
3131
onBottom () {
32+
console.log('REQUEST NEXT');
3233
this.items = this.items.concat(fetchData());
3334
}
3435
}

demo/item.vue

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
<ul class="user-list">
44
<li class="user-item indx">{{ item.indx }}</li>
55
<li class="user-item name">{{ item.name }}</li>
6-
<li class="user-item urid">{{ item.urid }}</li>
76
<li class="user-item date">{{ item.date }}</li>
87
</ul>
98
</div>
@@ -22,30 +21,30 @@
2221
height: 30px;
2322
line-height: 30px;
2423
}
25-
.user:nth-child(2n) {
24+
/*.user:nth-child(2n) {
2625
background: #efefef;
2726
}
2827
.user:nth-child(2n-1) {
2928
background: #d1e3f5;
30-
}
29+
}*/
3130
.user-list {
3231
list-style: none;
3332
}
3433
.user-item {
3534
display: inline-block;
36-
text-align: center;
3735
}
3836
.indx {
3937
width: 50px;
4038
font-weight: bold;
41-
}
42-
.urid {
43-
width: 60px;
39+
text-align: center;
4440
}
4541
.name {
46-
width: 120px;
42+
width: 180px;
43+
padding-left: 20px;
44+
text-align: left;
4745
}
4846
.date {
49-
width: 130px;
47+
width: 120px;
48+
text-align: right;
5049
}
5150
</style>

demo/request_mock.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ export function fetchData (count) {
1313
while (count--) {
1414
list.push({
1515
indx: user_count++,
16-
urid: Random.natural(1000, 9000),
17-
name: Random.cname(),
16+
name: Random.name(),
1817
date: Random.date()
1918
});
2019
}

src/index.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,18 @@ import { throttle } from './util';
33

44
// some data help to calculate
55
let delta = {
6-
total: 0,
6+
// about scroll
77
direct: '',
88
last_top: 0,
99
page_type: '',
10+
11+
// about data
12+
total: 0,
13+
joints: 0,
1014
start_index: 0,
15+
16+
// about style
17+
view_height: 0,
1118
all_padding: 0,
1219
padding_top: 0,
1320
bench_padding: 0
@@ -42,12 +49,8 @@ Vue.component('virtual-list', {
4249

4350
methods: {
4451
onScroll: throttle(function () {
45-
let cont = this.$refs.container;
46-
let listbox = this.$refs.listbox;
47-
48-
let scrollTop = cont.scrollTop;
49-
let viewHeight = cont.offsetHeight;
50-
let listHeight = listbox.offsetHeight;
52+
let scrollTop = this.$refs.container.scrollTop;
53+
let listHeight = this.$refs.listbox.offsetHeight;
5154

5255
saveDirect(scrollTop);
5356

@@ -58,7 +61,7 @@ Vue.component('virtual-list', {
5861

5962
// scroll to bottom
6063
let paddingBottom = delta.all_padding - delta.padding_top;
61-
if (listHeight <= scrollTop + viewHeight + paddingBottom) {
64+
if (listHeight <= scrollTop + delta.view_height + paddingBottom) {
6265
this.showNext();
6366
}
6467

@@ -137,10 +140,17 @@ Vue.component('virtual-list', {
137140
}
138141
},
139142

143+
beforeMount () {
144+
delta.view_height = this.remain * this.unit;
145+
},
146+
140147
mounted () {
141-
delta.bench_padding = Math.ceil(this.remain / 2) * this.unit;
148+
delta.joints = Math.ceil(this.remain / 2);
149+
delta.bench_padding = delta.joints * this.unit;
142150
},
143151

152+
beforeUpdate () {},
153+
144154
updated () {
145155
window.requestAnimationFrame(() => {
146156
this.$refs.container.scrollTop = delta.padding_top + delta.bench_padding;
@@ -156,7 +166,7 @@ Vue.component('virtual-list', {
156166
'class': 'virtual-list',
157167
'style': {
158168
'overflow-y': 'auto',
159-
'height': (this.remain * this.unit) + 'px'
169+
'height': delta.view_height + 'px'
160170
},
161171
'on': {
162172
'scroll': this.onScroll

src/util.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
export function throttle (fn, delay, immediate, debounce) {
33
let curr = +new Date();
44
let last_call = 0, last_exec = 0;
5-
let timer = null, diff, context, args;
6-
let exec = function () {
5+
let timer = null, diff, context, args;
6+
let exec = function () {
77
last_exec = curr;
88
fn.apply(context, args);
99
};

0 commit comments

Comments
 (0)