159
159
</template >
160
160
161
161
<script >
162
+ const getData = (store , self ) => {
163
+ let params = {
164
+ suid: ' aemu3ZqVijiqQj2QEFiB' ,
165
+ ab: ' welcome_3' ,
166
+ src: ' web'
167
+ }
168
+ return new Promise (resolve => {
169
+ store .dispatch (' recommend' , params).then (res => {
170
+ if (self ) {
171
+ const { d , m , s } = res
172
+ if (s === 1 ) {
173
+ self .recommends .push (... d)
174
+ } else {
175
+ self .$message ({
176
+ message: m,
177
+ type: ' warning'
178
+ })
179
+ }
180
+ self .scrollStatus = true
181
+ }
182
+ resolve (res)
183
+ })
184
+ })
185
+ }
186
+
162
187
export default {
163
188
data () {
164
189
return {
165
- scrollStatus: true
190
+ scrollStatus: true ,
191
+ recommends: []
166
192
}
167
193
},
168
194
filters: {
@@ -184,66 +210,46 @@ export default {
184
210
}
185
211
},
186
212
async asyncData ({ store, error }) {
187
- let params = {
188
- suid: ' aemu3ZqVijiqQj2QEFiB' ,
189
- ab: ' welcome_3' ,
190
- src: ' web'
191
- }
192
- let [ res ] = await Promise .all ([
193
- store .dispatch (' recommend' , params)
194
- ]).catch ((e ) => {
195
- error ({ statusCode: 404 , message: ' Post not found' })
196
- })
213
+ let res = await getData (store)
197
214
return {
198
215
recommends: res .d
199
216
}
200
217
},
201
218
mounted () {
202
- const self = this
203
- const $el = document .documentElement
204
- const $entry = self .$refs .entry
205
- let clienHeight = $el .clientHeight
206
- let containerHeight = ~~ (window .getComputedStyle ($entry, null ).getPropertyValue (' height' ).split (' px' )[0 ]) + 140
207
-
208
- const params = {
209
- suid: ' aemu3ZqVijiqQj2QEFiB' ,
210
- ab: ' welcome_3' ,
211
- src: ' web'
212
- }
213
- window .onscroll = () => {
214
- if ($el .scrollTop > 120 ) {
215
- document .getElementsByClassName (' to-top-btn' )[0 ].classList .add (' show' )
216
- } else {
217
- document .getElementsByClassName (' to-top-btn' )[0 ].classList .remove (' show' )
218
- }
219
- // console.log(containerHeight, $el.scrollTop + clienHeight, clienHeight)
220
- if ($el .scrollTop + clienHeight > containerHeight - 10 && self .scrollStatus ) {
221
- self .scrollStatus = false
222
- self .$store .dispatch (' recommend' , params).then (
223
- res => {
224
- const { d , m , s } = res
225
- if (s === 1 ) {
226
- self .recommends = [... self .recommends , ... d]
227
- containerHeight = ~~ (window .getComputedStyle ($entry, null ).getPropertyValue (' height' ).split (' px' )[0 ]) + 140
228
- // console.log('containerHeight', containerHeight)
229
- } else {
230
- self .$message ({
231
- message: m,
232
- type: ' warning'
233
- })
234
- }
235
- }
236
- ).then (() => {
237
- setTimeout (() => {
238
- self .scrollStatus = true
239
- }, 300 )
219
+ this .getFullPageData ()
220
+ window .addEventListener (' scroll' , this .handleScroll )
221
+ },
222
+ destroyed () {
223
+ window .removeEventListener (' scroll' , this .handleScroll )
224
+ },
225
+ methods: {
226
+ getFullPageData () {
227
+ if (document .body .offsetHeight < window .innerHeight ) {
228
+ this .loadMoreData ().then (() => {
229
+ this .getFullPageData ()
240
230
})
241
231
}
242
- }
243
- window .onresize = () => {
244
- clienHeight = $el .clientHeight
245
- containerHeight = ~~ (window .getComputedStyle ($entry, null ).getPropertyValue (' height' ).split (' px' )[0 ]) + 140
246
- // console.log('clienHeight', clienHeight)
232
+ },
233
+ handleScroll () {
234
+ this .timer && clearTimeout (this .timer )
235
+ this .timer = setTimeout (this .loadMoreData , 300 )
236
+ },
237
+ loadMoreData () {
238
+ return new Promise ((resolve ) => {
239
+ let $el = document .documentElement
240
+ let $entry = this .$refs .entry
241
+ let clienHeight = $el .clientHeight
242
+ let containerHeight = ~~ (window .getComputedStyle ($entry, null ).getPropertyValue (' height' ).split (' px' )[0 ]) + 140
243
+ // 设置【返回顶部】显示隐藏
244
+ document .querySelector (' .to-top-btn' ).classList [$el .scrollTop > 120 ? ' add' : ' remove' ](' show' )
245
+ // console.log(containerHeight, $el.scrollTop + clienHeight, clienHeight)
246
+ // 滚动到一定高度,重新加载数据
247
+ if ($el .scrollTop + clienHeight > containerHeight - 10 && this .scrollStatus ) {
248
+ getData (this .$store , this ).then (res => {
249
+ resolve (res)
250
+ })
251
+ }
252
+ })
247
253
}
248
254
}
249
255
}
0 commit comments