vue底部加載更多的實(shí)例代碼
更新時(shí)間:2018年06月29日 10:59:42 作者:CH--
本文通過實(shí)例代碼給大家介紹了vue底部加載更多,代碼簡單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
要實(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)事件,檢測滾動(dòng)到頁面底部
window.addEventListener('scroll', this.scrollBottom)
},
methods: {
scrollBottom() {
// 滾動(dòng)到頁面底部時(shí),請求前一天的文章內(nèi)容
if (((window.screen.height + document.body.scrollTop) > (document.body.clientHeight)) && this.REQUIRE) {
// 請求的數(shù)據(jù)未加載完成時(shí),滾動(dò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'];
// 請求的數(shù)據(jù)加載完成后,再次滾動(dòng)到底部時(shí),允許請求前一天數(shù)據(jù)
this.$nextTick(() => {
this.REQUIRE = true;
this.loading = false;
});
}).catch(() => {
this.tips = '連接失敗,請稍后重試';
// 請求失敗時(shí),將 REQUIRE 置為 true,滾動(dòng)到底部時(shí),再次請求
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í)例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
vue+echarts5實(shí)現(xiàn)中國地圖的示例代碼
本文主要介紹了vue+echarts5實(shí)現(xiàn)中國地圖的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
解讀element ui el-row標(biāo)簽中的gutter用法
這篇文章主要介紹了解讀element ui el-row標(biāo)簽中的gutter用法,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
vue3.0找不到模塊“./App.vue”或其相應(yīng)的類型聲明(多種情況分析)
這篇文章主要介紹了vue3.0找不到模塊“./App.vue”或其相應(yīng)的類型聲明,報(bào)錯(cuò)原因是typescript?只能理解?.ts?文件,無法理解?.vue文件,本文通過多種情況分析給大家詳細(xì)講解,需要的朋友可以參考下2023-01-01
Vue Autocomplete 自動(dòng)完成功能簡單示例
這篇文章主要介紹了Vue Autocomplete 自動(dòng)完成功能,結(jié)合簡單示例形式分析了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端的主要文件和組件,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-01-01

