vue中實現(xiàn)滾動加載更多的示例
更新時間:2017年11月08日 10:24:21 作者:土家稀哥
下面小編就為大家?guī)硪黄獀ue中實現(xiàn)滾動加載更多的示例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
在以前的前端刀耕火種時代要實現(xiàn)滾動加載更多想要大家都是很快實現(xiàn)了,在vue會有一點麻煩,最近自己研究了一下,做了一個簡單的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ù)據(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); // 如果開關打開則加載數(shù)據(jù) if(sw==true){ // 將開關關閉 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,就會反應到視圖中了 JSON.parse(response.data).stories.forEach(function(val,index){ _this.articles.push(val); // console.log(val); }); // 數(shù)據(jù)更新完畢,將開關打開 sw = true; }) .catch(function(error){ console.log(error); }); } } }); } } </script> <style lang="less"> *{ margin:0; padding:0; } li{ list-style:none; } </style>
大致效果如下
當然目前只是一個demo,還有更好的解決辦法大家自行補充。
以上這篇vue中實現(xiàn)滾動加載更多的示例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Vue之beforeEach非登錄不能訪問的實現(xiàn)(代碼親測)
這篇文章主要介紹了Vue之beforeEach非登錄不能訪問的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-07-07vue項目中監(jiān)聽手機物理返回鍵的實現(xiàn)
這篇文章主要介紹了vue項目中監(jiān)聽手機物理返回鍵的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-01-01Vue中transition單個節(jié)點過渡與transition-group列表過渡全過程
這篇文章主要介紹了Vue中transition單個節(jié)點過渡與transition-group列表過渡全過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04