vue中實(shí)現(xiàn)滾動加載更多的示例
在以前的前端刀耕火種時代要實(shí)現(xiàn)滾動加載更多想要大家都是很快實(shí)現(xiàn)了,在vue會有一點(diǎn)麻煩,最近自己研究了一下,做了一個簡單的demo,供大家參考:
<template>
<div>
<ul>
<li v-for="item in articles">
<h2>{{item.title}}</h2>
<img :src="item.images" alt="">
</li>
</ul>
</div>
</template>
<script>
import axios from 'axios';
export default{
data(){
return {
articles : []
}
},
mounted(){
// 緩存指針
let _this = this;
// 設(shè)置一個開關(guān)來避免重負(fù)請求數(shù)據(jù)
let sw = true;
// 此處使用node做了代理
axios.get('http://localhost:3000/proxy?url=http://news-at.zhihu.com/api/4/news/latest')
.then(function(response){
// console.log(JSON.parse(response.data).stories);
// 將得到的數(shù)據(jù)放到vue中的data
_this.articles = JSON.parse(response.data).stories;
})
.catch(function(error){
console.log(error);
});
// 注冊scroll事件并監(jiān)聽
window.addEventListener('scroll',function(){
// console.log(document.documentElement.clientHeight+'-----------'+window.innerHeight); // 可視區(qū)域高度
// console.log(document.body.scrollTop); // 滾動高度
// console.log(document.body.offsetHeight); // 文檔高度
// 判斷是否滾動到底部
if(document.body.scrollTop + window.innerHeight >= document.body.offsetHeight) {
// console.log(sw);
// 如果開關(guān)打開則加載數(shù)據(jù)
if(sw==true){
// 將開關(guān)關(guān)閉
sw = false;
axios.get('http://localhost:3000/proxy?url=http://news.at.zhihu.com/api/4/news/before/20170608')
.then(function(response){
console.log(JSON.parse(response.data));
// 將新獲取的數(shù)據(jù)push到vue中的data,就會反應(yīng)到視圖中了
JSON.parse(response.data).stories.forEach(function(val,index){
_this.articles.push(val);
// console.log(val);
});
// 數(shù)據(jù)更新完畢,將開關(guān)打開
sw = true;
})
.catch(function(error){
console.log(error);
});
}
}
});
}
}
</script>
<style lang="less">
*{
margin:0;
padding:0;
}
li{
list-style:none;
}
</style>
大致效果如下

當(dāng)然目前只是一個demo,還有更好的解決辦法大家自行補(bǔ)充。
以上這篇vue中實(shí)現(xiàn)滾動加載更多的示例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- vue實(shí)現(xiàn)分頁加載效果
- vue實(shí)現(xiàn)的上拉加載更多數(shù)據(jù)/分頁功能示例
- Vue.js實(shí)現(xiàn)無限加載與分頁功能開發(fā)
- Javascript vue.js表格分頁,ajax異步加載數(shù)據(jù)
- vue.js 表格分頁ajax 異步加載數(shù)據(jù)
- vue element-ui table表格滾動加載方法
- Vue.js 的移動端組件庫mint-ui實(shí)現(xiàn)無限滾動加載更多的方法
- Vue.js上下滾動加載組件的實(shí)例代碼
- Vue 無限滾動加載指令實(shí)現(xiàn)方法
- vue實(shí)現(xiàn)純前端表格滾動分頁加載
相關(guān)文章
Vue3子組件向父組件傳值的兩種實(shí)現(xiàn)方式
近期學(xué)習(xí)vue3的父子組件之間的傳值,發(fā)現(xiàn)跟vue2的并沒有太大的區(qū)別,這篇文章主要給大家介紹了關(guān)于Vue3子組件向父組件傳值的兩種實(shí)現(xiàn)方式,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-04-04
Vue之beforeEach非登錄不能訪問的實(shí)現(xiàn)(代碼親測)
這篇文章主要介紹了Vue之beforeEach非登錄不能訪問的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
vue項(xiàng)目中監(jiān)聽手機(jī)物理返回鍵的實(shí)現(xiàn)
這篇文章主要介紹了vue項(xiàng)目中監(jiān)聽手機(jī)物理返回鍵的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
vue項(xiàng)目中常用解決跨域的方法總結(jié)(CORS和Proxy)
在vue項(xiàng)目中,一般我們會遇到跨域的問題,vue項(xiàng)目中解決跨域是非常簡單的,下面這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目中常用解決跨域的方法,主要解釋CROS和Proxy兩種方式,需要的朋友可以參考下2022-12-12
圖文詳解vue中proto文件的函數(shù)調(diào)用
這篇文章主要給大家介紹了vue中proto文件函數(shù)調(diào)用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2021-08-08
Vuejs中的watch實(shí)例詳解(監(jiān)聽者)
本文通過實(shí)例代碼給大家介紹了Vuejs中的watch,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧2020-01-01
vue3中實(shí)現(xiàn)異步組件的方法實(shí)例
前端開發(fā)經(jīng)常遇到異步的問題,請求函數(shù)、鏈接庫等,下面這篇文章主要給大家介紹了關(guān)于vue3中實(shí)現(xiàn)異步組件的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06
Vue中transition單個節(jié)點(diǎn)過渡與transition-group列表過渡全過程
這篇文章主要介紹了Vue中transition單個節(jié)點(diǎn)過渡與transition-group列表過渡全過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04

