PHP結(jié)合Vue實(shí)現(xiàn)滾動(dòng)底部加載效果
前言
最近的一個(gè)項(xiàng)目手機(jī)端分頁跳轉(zhuǎn)不理想,自己做了一個(gè)滾動(dòng)加載的一個(gè)Demo,下面話不多說了,來一起看看詳細(xì)的介紹吧。
實(shí)現(xiàn)思路
1.獲得滾動(dòng)條到底部的距離 getScrollBottomHeight()
2.綁定滾動(dòng)事件handleScroll() ,handleScroll()判斷滾動(dòng)條到底部距離是否小于設(shè)置的bottomHight,并且增加一個(gè)loading屬性,防止加載時(shí)滑動(dòng)時(shí)多次觸發(fā),造成多次加載
3.Ajax請(qǐng)求load.php,通過Page去查詢獲得當(dāng)前頁數(shù)(page+1)的內(nèi)容
4.將獲取的內(nèi)容,push 到 list中,完成后Vue 自動(dòng)渲染新的列表,loading變?yōu)閒alse
核心Dom結(jié)構(gòu)
<body>
<div id="Content">
<div>
<ul>
<li v-for="l in list">{{l.title}}</li>
<li class="loading" v-if="loading">加載中</li>
</ul>
</div>
</div>
</body>
Javascript代碼
<script>
var v = new Vue({
el: "#Content",
data: {
list: [{title: "使用思維導(dǎo)圖,優(yōu)雅的完成自己的代碼"},
{title: "左滑右滑的樂趣"},
{title: "Spring Cloud(九)高可用的分布式配置中心 Spring Cloud Config 集成 Eureka 服務(wù)q"},
{title: "【MYSQL】業(yè)務(wù)上碰到的SQL問題整理集合"},
{title: "2018年,前端應(yīng)該怎么學(xué)?"},
{title: "前端 ajax 請(qǐng)求的優(yōu)雅方案"},
{title: "SegmentFault 技術(shù)周刊 Vol.39 - 什么!服務(wù)器炸了?"},
{title: "Rokid 開發(fā)板試用,開啟你的嵌入式開發(fā)之旅"},
{title: "我腦中飄來飄去的css魔幻屬性"},
{title: "用python解決mysql視圖導(dǎo)入導(dǎo)出依賴問題"},
{title: "underscore 系列之防沖突與 Utility Functions"},
{title: "基于手淘 flexible 的 Vue 組件:TextScroll -- 文字滾動(dòng)"},
{title: "基于‘BOSS直聘的招聘信息'分析企業(yè)到底需要什么樣的PHP程序員"},
{title: "原生js系列之無限循環(huán)輪播組件"},
{title: "一篇文章了解HTML文檔流(normal flow)"},
{title: "面試官最愛的volatile關(guān)鍵字"},
{title: "Spring Cloud(九)高可用的分布式配置中心 Spring Cloud Config 集成 Eureka 服務(wù)q"},
{title: "【MYSQL】業(yè)務(wù)上碰到的SQL問題整理集合"},
{title: "2018年,前端應(yīng)該怎么學(xué)?"},
{title: "前端 ajax 請(qǐng)求的優(yōu)雅方案"},
{title: "SegmentFault 技術(shù)周刊 Vol.39 - 什么!服務(wù)器炸了?"},
{title: "Rokid 開發(fā)板試用,開啟你的嵌入式開發(fā)之旅"},
{title: "我腦中飄來飄去的css魔幻屬性"},
{title: "用python解決mysql視圖導(dǎo)入導(dǎo)出依賴問題"},
{title: "underscore 系列之防沖突與 Utility Functions"},
{title: "基于手淘 flexible 的 Vue 組件:TextScroll -- 文字滾動(dòng)"},
{title: "基于‘BOSS直聘的招聘信息'分析企業(yè)到底需要什么樣的PHP程序員"},
{title: "原生js系列之無限循環(huán)輪播組件"},
{title: "一篇文章了解HTML文檔流(normal flow)"},
{title: "面試官最愛的volatile關(guān)鍵字"},
{title: "Rokid 開發(fā)板試用,開啟你的嵌入式開發(fā)之旅"}],
page: 5,//總頁數(shù)
nowPage: 1,//本頁
loading: false,//一步加載時(shí)的限制
bottomHight: 50,//滾動(dòng)條到某個(gè)位置才觸發(fā)時(shí)間
},
methods: {
handleScroll: function () {
if (getScrollBottomHeight() <= v.bottomHight && v.nowPage < v.page && v.loading == false) {
v.loading = true
var url = "load.php"
$.ajax({
type: "GET",
url: url,
async: true,
dataType: "json",
success: function (data) {
for (var i = 0; i < data.length; i++) {
v.list.push(data[i])
}
v.nowPage++
v.loading = false
},
})
}
}
},
})
//添加滾動(dòng)事件
window.onload = function () {
window.addEventListener('scroll', v.handleScroll)
}
//滾動(dòng)條到底部的距離
function getScrollBottomHeight() {
return getPageHeight() - getScrollTop() - getWindowHeight();
}
//頁面高度
function getPageHeight() {
return document.querySelector("html").scrollHeight
}
//滾動(dòng)條頂 高度
function getScrollTop() {
var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
if (document.body) {
bodyScrollTop = document.body.scrollTop;
}
if (document.documentElement) {
documentScrollTop = document.documentElement.scrollTop;
}
scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;
return scrollTop;
}
function getWindowHeight() {
var windowHeight = 0;
if (document.compatMode == "CSS1Compat") {
windowHeight = document.documentElement.clientHeight;
} else {
windowHeight = document.body.clientHeight;
}
return windowHeight;
}
</script>
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Fatal error: session_start(): Failed to initialize storage m
這篇文章主要介紹了Fatal error: session_start(): Failed to initialize storage module: files問題解決方法,需要的朋友可以參考下2014-05-05
淺析PHP中Collection 類的設(shè)計(jì)
本篇文章是對(duì)PHP中Collection 類進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
PHP 開發(fā)環(huán)境配置(Zend Server安裝)
運(yùn)行安裝文件(ZendServer-CE-php-5.3.2-5.0.1-Windows_x86.exe)開始安裝,選項(xiàng)請(qǐng)參照我的選擇。2010-04-04
php 數(shù)組動(dòng)態(tài)添加實(shí)現(xiàn)代碼(最土團(tuán)購系統(tǒng)的價(jià)格排序)
最近在實(shí)現(xiàn)最土團(tuán)購系統(tǒng)的價(jià)格排序功能,需要對(duì)$oc數(shù)組進(jìn)行擴(kuò)展,經(jīng)過測(cè)試用下面的方法即可。2011-12-12
php使用ZipArchive提示Fatal error: Class ZipArchive not found in的
這篇文章主要介紹了php使用ZipArchive提示Fatal error: Class ZipArchive not found in的解決方法,是使用ZipArchive時(shí)經(jīng)常會(huì)遇到的問題,需要的朋友可以參考下2014-11-11
thinkphp關(guān)于簡單的權(quán)限判定方法
下面小編就為大家?guī)硪黄猼hinkphp關(guān)于簡單的權(quán)限判定方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04

