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)文章
vue+echarts5實(shí)現(xiàn)中國(guó)地圖的示例代碼
本文主要介紹了vue+echarts5實(shí)現(xiàn)中國(guó)地圖的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
解讀element ui el-row標(biāo)簽中的gutter用法
這篇文章主要介紹了解讀element ui el-row標(biāo)簽中的gutter用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
在vue項(xiàng)目中如何獲取視頻的時(shí)長(zhǎng)
這篇文章主要介紹了在vue項(xiàng)目中如何獲取視頻的時(shí)長(zhǎng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
vue3.0找不到模塊“./App.vue”或其相應(yīng)的類型聲明(多種情況分析)
這篇文章主要介紹了vue3.0找不到模塊“./App.vue”或其相應(yīng)的類型聲明,報(bào)錯(cuò)原因是typescript?只能理解?.ts?文件,無(wú)法理解?.vue文件,本文通過(guò)多種情況分析給大家詳細(xì)講解,需要的朋友可以參考下2023-01-01
Vue Autocomplete 自動(dòng)完成功能簡(jiǎn)單示例
這篇文章主要介紹了Vue Autocomplete 自動(dòng)完成功能,結(jié)合簡(jiǎn)單示例形式分析了Vue使用el-autocomplete組件實(shí)現(xiàn)自動(dòng)完成功能相關(guān)操作技巧,需要的朋友可以參考下2019-05-05
uniapp微信小程序webview和h5數(shù)據(jù)通信代碼示例
這篇文章主要介紹了uniapp微信小程序webview和h5數(shù)據(jù)通信的相關(guān)資料,文章還列出了項(xiàng)目的結(jié)構(gòu),包括微信小程序和h5端的主要文件和組件,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-01-01
vue3.x項(xiàng)目降級(jí)到vue2.7的解決方案
Vue2.7是Vue2.x的最終次要版本,下面這篇文章主要給大家介紹了關(guān)于vue3.x項(xiàng)目降級(jí)到vue2.7的解決方案,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03

