vue底部加載更多的實(shí)例代碼
要實(shí)現(xiàn)的效果如下:
<template> <div class="newsList"> <div v-for="(items, index) in newsList"> <div class="date">{{showDay(index)}}</div> <div class="list" > <ul> <li class="list-item" v-for="item in items"> <span class="text">{{item.title}}</span> <img :src="attachImageUrl(item.images[0])" class="image"/> </li> </ul> </div> </div> <div class="infinite-scroll" v-show="loading"> <svg class="loader-circular" viewBox="25 25 50 50"> <circle class="loader-path" cx="50" cy="50" r="20" fill="none" stroke="rgb(53, 157, 218)" stroke-width="5"></circle> </svg> <span class="infinite-scroll-text">{{tips}}</span> </div> </div> </template> <script> import axios from 'axios'; export default { data () { return { newsList: [], date: [], todayDate: '', REQUIRE: true, loading: false, tips: '努力加載中...' } }, created () { // 獲取今日新聞 axios.get('http://zhihuapi.herokuapp.com/api/4/news/latest') .then( (res) => { this.newsList.push(res.data['stories']) this.date.push(res.data['date']); this.todayDate = res.data['date'] }) }, mounted () { // 添加滾動(dòng)事件,檢測(cè)滾動(dòng)到頁(yè)面底部 window.addEventListener('scroll', this.scrollBottom) }, methods: { scrollBottom() { // 滾動(dòng)到頁(yè)面底部時(shí),請(qǐng)求前一天的文章內(nèi)容 if (((window.screen.height + document.body.scrollTop) > (document.body.clientHeight)) && this.REQUIRE) { // 請(qǐng)求的數(shù)據(jù)未加載完成時(shí),滾動(dòng)到底部不再請(qǐng)求前一天的數(shù)據(jù) this.REQUIRE = false; this.loading = true; this.tips = '努力加載中...'; axios.get('http://zhihuapi.herokuapp.com/api/4/news/before/' + this.todayDate).then((res) => { this.newsList.push(res.data['stories']); this.date.push(res.data['date']); this.todayDate = res.data['date']; // 請(qǐng)求的數(shù)據(jù)加載完成后,再次滾動(dòng)到底部時(shí),允許請(qǐng)求前一天數(shù)據(jù) this.$nextTick(() => { this.REQUIRE = true; this.loading = false; }); }).catch(() => { this.tips = '連接失敗,請(qǐng)稍后重試'; // 請(qǐng)求失敗時(shí),將 REQUIRE 置為 true,滾動(dòng)到底部時(shí),再次請(qǐng)求 this.REQUIRE = true; }); } }, showDay (index) { if (index === 0) { return '今日新聞' } else { return this.getToday(index) } }, getToday (index) { let year = this.date[index].slice(0, 4); let month = this.date[index].slice(4, 6); let day = this.date[index].slice(6); let today = new Date(year + '/' + month + '/' + day); let week = ['日', '一', '二', '三', '四', '五', '六']; return month + '月' + day + '日' + ' 星期' + week[today.getDay()]; }, attachImageUrl (srcUrl) { if (srcUrl !== undefined) { return 'http://read.html5.qq.com/image?src=forum&q=5&r=0&imgflag=7&imageUrl=' + srcUrl.slice(0, 4) + srcUrl.slice(5); } } } } </script>
總結(jié)
以上所述是小編給大家介紹的vue底部加載更多的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Vue3+TypeScript實(shí)現(xiàn)Docx/Excel預(yù)覽組件
這篇文章主要為大家詳細(xì)介紹了如何使用Vue3+TypeScript實(shí)現(xiàn)Docx/Excel預(yù)覽組件,文中的示例代碼講解詳細(xì),有需要的小伙伴可以參考下2024-04-04Vue頁(yè)面跳轉(zhuǎn)傳遞參數(shù)及接收方式
這篇文章主要介紹了Vue頁(yè)面跳轉(zhuǎn)傳遞參數(shù)及接收方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09Vue3+TS項(xiàng)目中eslint、prettier安裝配置詳細(xì)指南
為了更好的統(tǒng)一項(xiàng)目的代碼風(fēng)格,因此在編寫(xiě)時(shí)就可以使用eslint+prettier,它們不僅能方便代碼編寫(xiě),還能避免不必要的錯(cuò)誤,讓代碼變得更加嚴(yán)謹(jǐn),這篇文章主要給大家介紹了關(guān)于Vue3+TS項(xiàng)目中eslint、prettier安裝配置的相關(guān)資料,需要的朋友可以參考下2024-07-07vue項(xiàng)目API接口get請(qǐng)求傳遞參數(shù)方式
這篇文章主要介紹了vue項(xiàng)目API接口get請(qǐng)求傳遞參數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07vue 的點(diǎn)擊事件獲取當(dāng)前點(diǎn)擊的元素方法
今天小編就為大家分享一篇vue 的點(diǎn)擊事件獲取當(dāng)前點(diǎn)擊的元素方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09