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è)屬性的介紹就可以看出來(lái),滾動(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ù)問(wèn)題',
'description': '撒的劃分空間撒電話費(fèi)看見愛上對(duì)方見客戶速度快解放哈薩克接電話發(fā)生的劃分空間是的哈副科級(jí)哈師大空間劃分可接受的后方可摳腳大漢房間卡收到貨放假多少',
'status': '0',
'status_value': '未解決',
'level': '0203',
'level_value': '高',
'content': '過(guò)好幾個(gè)號(hào)',
'userid': 'lxzx_hdsx',
'create_time': 1480296174000,
'images': null
})
self.questionListData.push({
'id': 'D679611152737E675984D7681BC94F16',
'type': '0101',
'type_value': '需求問(wèn)題',
'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': '頁(yè)面問(wèn)題',
'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ì)問(wèn)題',
'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ù)問(wèn)題',
'description': '數(shù)據(jù)列表滾動(dòng)條問(wèn)題',
'status': '0',
'status_value': '未解決',
'level': '0202',
'level_value': '中',
'content': '數(shù)據(jù)列表在數(shù)據(jù)條數(shù)比較多的情況下無(wú)滾動(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測(cè)試1</span>
<span>測(cè)試2</span>
<span>測(cè)試3</span>
<span>測(cè)試4</span>
<span>測(cè)試5</span>
<span>測(cè)試6</span>
<span>測(cè)試7</span>
<span>測(cè)試8</span>
<span>測(cè)試9</span>
<span>測(cè)試10</span>
<span>測(cè)試11</span>
</li>
</ul>
</div>
js:
var html = '' //距下邊界長(zhǎng)度/單位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);
}
});
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
示例vue 的keep-alive緩存功能的實(shí)現(xiàn)
這篇文章主要介紹了示例vue 的keep-alive緩存功能的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
Vue 實(shí)現(xiàn)把表單form數(shù)據(jù) 轉(zhuǎn)化成json格式的數(shù)據(jù)
今天小編就為大家分享一篇Vue 實(shí)現(xiàn)把表單form數(shù)據(jù) 轉(zhuǎn)化成json格式的數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10
vue 解決遍歷對(duì)象顯示的順序不對(duì)問(wèn)題
今天小編就為大家分享一篇vue 解決遍歷對(duì)象顯示的順序不對(duì)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
vue項(xiàng)目input標(biāo)簽checkbox,change和click綁定事件的區(qū)別說(shuō)明
這篇文章主要介紹了vue項(xiàng)目input標(biāo)簽checkbox,change和click綁定事件的區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08

