vue2滾動(dòng)條加載更多數(shù)據(jù)實(shí)現(xiàn)代碼
解析:
判斷滾動(dòng)條到底部,需要用到DOM的三個(gè)屬性值,即scrollTop、clientHeight、scrollHeight。
scrollTop為滾動(dòng)條在Y軸上的滾動(dòng)距離。
clientHeight為內(nèi)容可視區(qū)域的高度。
scrollHeight為內(nèi)容可視區(qū)域的高度加上溢出(滾動(dòng))的距離。
從這個(gè)三個(gè)屬性的介紹就可以看出來,滾動(dòng)條到底部的條件即為scrollTop + clientHeight == scrollHeight。(兼容不同的瀏覽器)。
代碼:
1.vue的實(shí)現(xiàn)
html:
<div class="questionList-content-list">
<ul>
<li v-for="item in questionListData" @click="goDetail(item.id)">
{{item.create_time}}
[{{item.level_value}}]
{{item.description}}
{{item.status_value}}
</li>
</ul>
</div>
js:
created () {
var self = this
$(window).scroll(function () {
let scrollTop = $(this).scrollTop()
let scrollHeight = $(document).height()
let windowHeight = $(this).height()
if (scrollTop + windowHeight === scrollHeight) {
self.questionListData.push({
'id': '62564AED8A4FA7CCDBFBD0F9A11C97A8',
'type': '0102',
'type_value': '數(shù)據(jù)問題',
'description': '撒的劃分空間撒電話費(fèi)看見愛上對(duì)方見客戶速度快解放哈薩克接電話發(fā)生的劃分空間是的哈副科級(jí)哈師大空間劃分可接受的后方可摳腳大漢房間卡收到貨放假多少',
'status': '0',
'status_value': '未解決',
'level': '0203',
'level_value': '高',
'content': '過好幾個(gè)號(hào)',
'userid': 'lxzx_hdsx',
'create_time': 1480296174000,
'images': null
})
self.questionListData.push({
'id': 'D679611152737E675984D7681BC94F16',
'type': '0101',
'type_value': '需求問題',
'description': 'a阿斯頓發(fā)生豐盛的范德薩范德薩發(fā)十多個(gè)非官方阿道夫葛根粉v跟下載v',
'status': '0',
'status_value': '未解決',
'level': '0203',
'level_value': '高',
'content': '秩序性支出V型從v',
'userid': 'lxzx_hdsx',
'create_time': 1480296155000,
'images': null
})
self.questionListData.push({
'id': 'B5C61D990F962570C34B8EE607CA1384',
'type': '0104',
'type_value': '頁面問題',
'description': '回復(fù)的文本框和字體有點(diǎn)丑',
'status': '0',
'status_value': '未解決',
'level': '0203',
'level_value': '高',
'content': '回復(fù)的文本框和字體有點(diǎn)丑',
'userid': 'lxzx_hdsx',
'create_time': 1480064620000,
'images': null
})
self.questionListData.push({
'id': '279F9571CB8DC36F1DEA5C8773F1793C',
'type': '0103',
'type_value': '設(shè)計(jì)問題',
'description': '設(shè)計(jì)bug,不應(yīng)該這樣設(shè)計(jì)。',
'status': '0',
'status_value': '未解決',
'level': '0204',
'level_value': '非常高',
'content': '設(shè)計(jì)bug,不應(yīng)該這樣設(shè)計(jì)。你看。',
'userid': 'lxzx_hdsx',
'create_time': 1480064114000,
'images': null
})
self.questionListData.push({
'id': '80E365710CB9157DB24F08C8D2039473',
'type': '0102',
'type_value': '數(shù)據(jù)問題',
'description': '數(shù)據(jù)列表滾動(dòng)條問題',
'status': '0',
'status_value': '未解決',
'level': '0202',
'level_value': '中',
'content': '數(shù)據(jù)列表在數(shù)據(jù)條數(shù)比較多的情況下無滾動(dòng)條',
'userid': 'lxzx_hdsx',
'create_time': 1480034606000,
'images': null
})
console.log(self.questionListData)
}
})
},
因?yàn)関ue2 實(shí)現(xiàn)了m-v雙向綁定,所以這里直接改變for循環(huán)數(shù)據(jù)源即可實(shí)現(xiàn)列表的數(shù)據(jù)刷新;
2: 普通js的實(shí)現(xiàn)
html:
<div id="content" style="height:960px" class="questionList-content-list">
<ul>
<li class="list">
<span測試1</span>
<span>測試2</span>
<span>測試3</span>
<span>測試4</span>
<span>測試5</span>
<span>測試6</span>
<span>測試7</span>
<span>測試8</span>
<span>測試9</span>
<span>測試10</span>
<span>測試11</span>
</li>
</ul>
</div>
js:
var html = '' //距下邊界長度/單位px
$(window).scroll(function () {
var scrollTop = $(this).scrollTop();
var scrollHeight = $(document).height();
var windowHeight = $(this).height();
if (scrollTop + windowHeight == scrollHeight) {
for(let i=0;i<10;i++){
html += '<li>Page: ' + i + ', Data Index: ' + i + ' </li>'
}
$('#content ul').append(html);
}
});
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue中響應(yīng)式系統(tǒng)實(shí)現(xiàn)原理圖文講解
Vue的響應(yīng)式實(shí)現(xiàn)是借助Object.defineProperty通過重寫getter和setter方法來進(jìn)行的數(shù)據(jù)劫持,Vue3通過Proxy代理攔截對(duì)象中任意屬性的變化,通過Reflect反射對(duì)源對(duì)象的屬性進(jìn)行操作,然后再在get里收集依賴在set里派發(fā)更新2023-03-03
vue 保留兩位小數(shù) 不能直接用toFixed(2) 的解決
這篇文章主要介紹了vue 保留兩位小數(shù) 不能直接用toFixed(2) 的解決操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08
Vue+Vant實(shí)現(xiàn)7天日歷展示并在切換日期時(shí)實(shí)時(shí)變換功能
本文介紹了如何利用Vue和Vant框架結(jié)合moment.js插件來實(shí)現(xiàn)一個(gè)7天日歷展示功能,在這個(gè)功能中,用戶可以在切換日期時(shí)看到界面的實(shí)時(shí)變化,此外,文章還提供了代碼實(shí)現(xiàn)和效果測試的詳細(xì)步驟,幫助開發(fā)者能夠順利完成類似的項(xiàng)目開發(fā)2024-10-10
vue3中的elementPlus全局組件中文轉(zhuǎn)換方式
這篇文章主要介紹了vue3中的elementPlus全局組件中文轉(zhuǎn)換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
Vue Echarts實(shí)現(xiàn)柱形圖從右向左滾動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Vue如何利用Echarts實(shí)現(xiàn)柱形圖從右向左滾動(dòng)的效果,文中的示例代碼簡潔易懂,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-05-05
關(guān)于dev-tool安裝方法(手動(dòng)安裝版)
這篇文章主要介紹了關(guān)于dev-tool安裝方法(手動(dòng)安裝版),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
vue3+ts中ref與reactive指定類型實(shí)現(xiàn)示例
這篇文章主要為大家介紹了vue3+ts中ref及reactive如何指定類型的實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06

