|
1 | 1 | <template> |
2 | 2 | <div id="app"> |
| 3 | + <h2>vue-scroll-list with infinite data</h2> |
| 4 | + <h3>random height</h3> |
| 5 | + <h4>total: {{count}}</h4> |
3 | 6 | <ul> |
4 | 7 | <scroll-list |
5 | 8 | :heights="heightList" |
|
8 | 11 | @toBottom="onBottom" |
9 | 12 | @scrolling="onScroll"> |
10 | 13 | <li v-for="(item, index) in list" |
11 | | - :key="item.name" |
12 | | - :style="{height: item.itemHeight + 'px'}"> |
13 | | - {{item.name}}/{{item.price}}/{{item.itemHeight}} |
| 14 | + :key="item.index" |
| 15 | + :style="{height: item.itemHeight + 'px', 'line-height': item.itemHeight + 'px'}"> |
| 16 | + index:{{item.index}} / height:{{item.itemHeight}} |
14 | 17 | </li> |
15 | 18 | </scroll-list> |
16 | 19 | </ul> |
|
33 | 36 | }, |
34 | 37 | methods: { |
35 | 38 | onTop() { |
36 | | - console.log('page to top.'); |
| 39 | + console.log('[demo]:page to top.'); |
37 | 40 | }, |
38 | 41 | onBottom() { |
39 | | - console.log('page to bottom.'); |
40 | | - this.createData(); |
| 42 | + console.log('[demo]:page to bottom.'); |
| 43 | + !window.__stopLoadData && this.createData(); |
41 | 44 | }, |
42 | 45 | onScroll(event) { |
43 | | - console.log(event); |
| 46 | + window.__showScrollEvent && console.log(event); |
44 | 47 | }, |
45 | 48 | createData() { |
46 | | - let size = 40; |
| 49 | + let size = window.__createSize || 40; |
47 | 50 | this.count += size; |
48 | 51 | for (let i = this.count - size; i < this.count; i++) { |
49 | | - let itemHeight = Math.random() > 0.5 ? 40 : 100; |
| 52 | + let itemHeight = Math.round(Math.random() * 100) + 40; |
50 | 53 | this.list.push({ |
51 | | - name: 'name-' + i, |
52 | | - price: Math.floor(Math.random() * 1000), |
| 54 | + index: i, |
53 | 55 | itemHeight: itemHeight |
54 | 56 | }); |
55 | 57 | this.heightList.push(itemHeight); |
56 | 58 | } |
| 59 | + console.log('[demo]:' + size + ' items are created.') |
| 60 | + }, |
| 61 | + logSomeInfo() { |
| 62 | + console.log('%cThere are some useful variables in window object:', 'color: green'); |
| 63 | + console.log('%c- `__createSize`: the number of items are created every time.', 'color: green'); |
| 64 | + console.log('%c- `__stopLoadData`: set true to stop loading data when the page is on bottom.', 'color: green'); |
| 65 | + console.log('%c- `__showScrollEvent`: set true to show scroll event.', 'color: green'); |
57 | 66 | } |
58 | 67 | }, |
59 | 68 | created() { |
| 69 | + window.__createSize = 40; |
| 70 | + window.__stopLoadData = false; |
| 71 | + window.__showScrollEvent = false; |
60 | 72 | this.createData(); |
| 73 | + this.logSomeInfo(); |
61 | 74 | } |
62 | 75 | }; |
63 | 76 | </script> |
64 | 77 | <style scoped> |
| 78 | + #app { |
| 79 | + text-align: center; |
| 80 | + } |
| 81 | +
|
65 | 82 | ul { |
66 | | - border: 1px solid #eee; |
67 | 83 | height: 400px; |
| 84 | + padding: 0; |
| 85 | + border: 1px solid #eee; |
| 86 | + } |
| 87 | +
|
| 88 | + li { |
| 89 | + border-bottom: 1px solid #eee; |
| 90 | + overflow: hidden; |
| 91 | + } |
| 92 | +
|
| 93 | + li:last-child { |
| 94 | + border-bottom: 0; |
68 | 95 | } |
69 | 96 |
|
70 | 97 | .scroll-container { |
|
0 commit comments