在以前的前端刀耕火種時(shí)代要實(shí)現(xiàn)滾動(dòng)加載更多想要大家都是很快實(shí)現(xiàn)了,在vue會(huì)有一點(diǎn)麻煩,最近自己研究了一下,做了一個(gè)簡單的demo,供大家參考:
成都創(chuàng)新互聯(lián)公司2013年至今,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目做網(wǎng)站、網(wǎng)站制作網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元宜豐做網(wǎng)站,已為上家服務(wù),為宜豐各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18982081108
<template> <div> <ul> <li v-for="item in articles"> <h3>{{item.title}}</h3> <img :src="item.images" alt=""> </li> </ul> </div> </template> <script> import axios from 'axios'; export default{ data(){ return { articles : [] } }, mounted(){ // 緩存指針 let _this = this; // 設(shè)置一個(gè)開關(guān)來避免重負(fù)請(qǐng)求數(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); }); // 注冊(cè)scroll事件并監(jiān)聽 window.addEventListener('scroll',function(){ // console.log(document.documentElement.clientHeight+'-----------'+window.innerHeight); // 可視區(qū)域高度 // console.log(document.body.scrollTop); // 滾動(dòng)高度 // console.log(document.body.offsetHeight); // 文檔高度 // 判斷是否滾動(dòng)到底部 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,就會(huì)反應(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)然目前只是一個(gè)demo,還有更好的解決辦法大家自行補(bǔ)充。
以上這篇vue中實(shí)現(xiàn)滾動(dòng)加載更多的示例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持創(chuàng)新互聯(lián)。
網(wǎng)頁標(biāo)題:vue中實(shí)現(xiàn)滾動(dòng)加載更多的示例
路徑分享:http://aaarwkj.com/article32/iigjpc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、面包屑導(dǎo)航、虛擬主機(jī)、靜態(tài)網(wǎng)站、App開發(fā)、全網(wǎng)營銷推廣
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)