vue底部加載更多的實例代碼
更新時間:2018年06月29日 10:59:42 作者:CH--
本文通過實例代碼給大家介紹了vue底部加載更多,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
要實現(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 () {
// 添加滾動事件,檢測滾動到頁面底部
window.addEventListener('scroll', this.scrollBottom)
},
methods: {
scrollBottom() {
// 滾動到頁面底部時,請求前一天的文章內(nèi)容
if (((window.screen.height + document.body.scrollTop) > (document.body.clientHeight)) && this.REQUIRE) {
// 請求的數(shù)據(jù)未加載完成時,滾動到底部不再請求前一天的數(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'];
// 請求的數(shù)據(jù)加載完成后,再次滾動到底部時,允許請求前一天數(shù)據(jù)
this.$nextTick(() => {
this.REQUIRE = true;
this.loading = false;
});
}).catch(() => {
this.tips = '連接失敗,請稍后重試';
// 請求失敗時,將 REQUIRE 置為 true,滾動到底部時,再次請求
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底部加載更多的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Vue3+TypeScript實現(xiàn)Docx/Excel預(yù)覽組件
這篇文章主要為大家詳細介紹了如何使用Vue3+TypeScript實現(xiàn)Docx/Excel預(yù)覽組件,文中的示例代碼講解詳細,有需要的小伙伴可以參考下2024-04-04
Vue3+TS項目中eslint、prettier安裝配置詳細指南
為了更好的統(tǒng)一項目的代碼風格,因此在編寫時就可以使用eslint+prettier,它們不僅能方便代碼編寫,還能避免不必要的錯誤,讓代碼變得更加嚴謹,這篇文章主要給大家介紹了關(guān)于Vue3+TS項目中eslint、prettier安裝配置的相關(guān)資料,需要的朋友可以參考下2024-07-07

