|
2 | 2 | <h3>Use with 3rd-party scroll plugin</h3>
|
3 | 3 | <p>If you want to use this plugin with 3rd-party scroll plugin like <a target="_blank" href="https://github.com/noraesae/perfect-scrollbar">perfect-scrollbar</a>, you can set a special attribute called <code>infinite-wrapper</code> for your real scroll parent, this plugin will find the closest container which has <code>overflow-y: auto;</code> CSS style or has <code>infinite-wrapper</code> attribute as the scroll parent and listen it's scroll event.</p>
|
4 | 4 | <p>The template should be like this:</p>
|
5 |
| - <pre v-highlightjs> |
| 5 | + <pre v-if="$parent.docVersion < 2.2" v-highlightjs> |
6 | 6 | <div id="perfect-scroll-wrapper" ref="scrollWrapper" infinite-wrapper>
|
7 | 7 | <ul>
|
8 | 8 | <li v-for="item in list" v-text="item"></li>
|
9 | 9 | </ul>
|
10 | 10 | <infinite-loading :on-infinite="onInfinite" ref="infiniteLoading"></infinite-loading>
|
| 11 | +</div></pre> |
| 12 | + <pre v-if="$parent.docVersion >= 2.2" v-highlightjs> |
| 13 | +<div id="perfect-scroll-wrapper" ref="scrollWrapper" infinite-wrapper> |
| 14 | + <ul> |
| 15 | + <li v-for="item in list" v-text="item"></li> |
| 16 | + </ul> |
| 17 | + <infinite-loading @infinite="infiniteHandler"></infinite-loading> |
11 | 18 | </div></pre>
|
12 | 19 | <p>Script:</p>
|
13 |
| - <pre v-highlightjs> |
| 20 | + <pre v-if="$parent.docVersion < 2.2" v-highlightjs> |
14 | 21 | import Ps from 'perfect-scrollbar';
|
15 | 22 |
|
16 | 23 | export default {
|
@@ -40,11 +47,42 @@ export default {
|
40 | 47 | components: {
|
41 | 48 | InfiniteLoading,
|
42 | 49 | },
|
| 50 | +};</pre> |
| 51 | + <pre v-if="$parent.docVersion >= 2.2" v-highlightjs> |
| 52 | +import Ps from 'perfect-scrollbar'; |
| 53 | + |
| 54 | +export default { |
| 55 | + data() { |
| 56 | + return { |
| 57 | + list: [], |
| 58 | + }; |
| 59 | + }, |
| 60 | + mounted() { |
| 61 | + Ps.initialize(this.$refs.scrollWrapper); |
| 62 | + }, |
| 63 | + methods: { |
| 64 | + infiniteHandler($state) { |
| 65 | + setTimeout(() => { |
| 66 | + const temp = []; |
| 67 | + for (let i = this.list.length + 1; i <= this.list.length + 20; i++) { |
| 68 | + temp.push(i); |
| 69 | + } |
| 70 | + this.list = this.list.concat(temp); |
| 71 | + $state.loaded(); |
| 72 | + this.$nextTick(() => { |
| 73 | + Ps.update(this.$refs.scrollWrapper); |
| 74 | + }); |
| 75 | + }, 1000); |
| 76 | + }, |
| 77 | + }, |
| 78 | + components: { |
| 79 | + InfiniteLoading, |
| 80 | + }, |
43 | 81 | };</pre>
|
44 | 82 | <p>Now it will work properly with <code>perfect-scrollbar</code>!</p>
|
45 | 83 | <h4>With iScroll (or other plugin like it)</h4>
|
46 | 84 | <p>If you use this plugin with <a target="_blank" href="https://github.com/cubiq/iscroll"><code>iScroll</code></a>, it cannot work directly with <code>iScroll</code>, because the <code>iScroll</code> will not fire the scroll event after smooth scrolling, even if it can, this plugin also cannot get the correct <code>scrollTop</code> by strandard way, so we must use the following workaround:</p>
|
47 |
| -<pre v-highlightjs> |
| 85 | +<pre v-if="$parent.docVersion < 2.2" v-highlightjs> |
48 | 86 | import iScroll from 'iscroll/iscroll-probe';
|
49 | 87 |
|
50 | 88 | export default {
|
@@ -85,5 +123,46 @@ export default {
|
85 | 123 | InfiniteLoading,
|
86 | 124 | },
|
87 | 125 | };</pre>
|
| 126 | +<pre v-if="$parent.docVersion >= 2.2" v-highlightjs> |
| 127 | +import iScroll from 'iscroll/iscroll-probe'; |
| 128 | + |
| 129 | +export default { |
| 130 | + data() { |
| 131 | + return { |
| 132 | + list: [], |
| 133 | + iScroll, |
| 134 | + }; |
| 135 | + }, |
| 136 | + mounted() { |
| 137 | + this.iScroll = new IScroll(this.$refs.scrollWrapper, { |
| 138 | + probeType: 3, // see http://iscrolljs.com/#custom-events |
| 139 | + }); |
| 140 | + this.iScroll.on('scroll', () => { |
| 141 | + let shouldBeCalled; // you should calculate the scroll top manually |
| 142 | + if (shouldBeCalled) { |
| 143 | + this.$refs.infiniteLoading.isLoading = true; // display the spinner manually |
| 144 | + this.$refs.infiniteLoading.$emit('infinite'); // emit the event manually |
| 145 | + } |
| 146 | + }); |
| 147 | + }, |
| 148 | + methods: { |
| 149 | + infiniteHandler($state) { |
| 150 | + setTimeout(() => { |
| 151 | + const temp = []; |
| 152 | + for (let i = this.list.length + 1; i <= this.list.length + 20; i++) { |
| 153 | + temp.push(i); |
| 154 | + } |
| 155 | + this.list = this.list.concat(temp); |
| 156 | + $state.loaded(); |
| 157 | + this.$nextTick(() => { |
| 158 | + this.iScroll.refresh(); |
| 159 | + }); |
| 160 | + }, 1000); |
| 161 | + }, |
| 162 | + }, |
| 163 | + components: { |
| 164 | + InfiniteLoading, |
| 165 | + }, |
| 166 | +};</pre> |
88 | 167 | <p>But is an ugly way, so if you have any good idea to solve it, please feedback to me in <a target="_blank" href="https://github.com/PeachScript/vue-infinite-loading/issues/44">this issue</a>, thank you very much!</p>
|
89 | 168 | </template>
|
0 commit comments