cookie的優(yōu)化與購物車實例
719
2023-12-10
相信很多人都見過瀑布流圖片布局,那些圖片是動態(tài)加載出來的,效果很好,對服務(wù)器的壓力相對來說也小了很多,用鼠標(biāo)操作的時候相信都見過這樣的效果:進(jìn)入qq空間,向下拉動空間,到底部時,會動態(tài)加載剩余的說說或者是日志 ,今天我們就來看看他們的實現(xiàn)思路和js控制動態(tài)加載的代碼。
下面的代碼主要是控制滾動條下拉時的加載事件的,無論是加載圖片還是加載記錄數(shù)據(jù) 都可以。
加載jQuery庫后我們可以這樣使用:
$(window).scroll(function () { var scrollTop = $(this).scrollTop(); var scrollHeight = $(document).height(); var windowHeight = $(this).height(); if (scrollTop + windowHeight == scrollHeight) { //此處是滾動條到底部時候觸發(fā)的事件,在這里寫要加載的數(shù)據(jù),或者是拉動滾動條的操作 //var page = Number($("#redgiftNextPage").attr('currentpage')) + 1; //redgiftList(page); //$("#redgiftNextPage").attr('currentpage', page + 1); } });
解析:
判斷滾動條到底部,需要用到DOM的三個屬性值,即scrollTop、clientHeight、scrollHeight。
scrollTop為滾動條在Y軸上的滾動距離。 clientHeight為內(nèi)容可視區(qū)域的高度。 scrollHeight為內(nèi)容可視區(qū)域的高度加上溢出(滾動)的距離。從這個三個屬性的介紹就可以看出來,滾動條到底部的條件即為scrollTop + clientHeight == scrollHeight。
純js我們可以這樣做:
window.onscroll = function() { var scrollTop = document.body.scrollTop; var offsetHeight = document.body.offsetHeight; var scrollHeight = document.body.scrollHeight; if (scrollTop == scrollHeight - offsetHeight) { page++; loadList(page); } }; function loadList(page) { page = page || 1; if (isLoad) { getJSON('/forum.php?mod=hot&page=' + page).then(function(data) { if (data.code == 200) { data = data.data; if (data && Object.keys(data).length > 0) { for (var k in data) { var v = data[k]; detailTemplate = detailTemplate.cloneNode(true); var userInfoObj = detailTemplate.getElementsByClassName('user-info')[0]; userInfoObj.getElementsByClassName('name')[0].innerText = v.author; userInfoObj.getElementsByClassName('avatar')[0].src = v.avatar; userInfoObj.getElementsByClassName('post-time')[0].innerHTML = v.lastpost; detailTemplate.getElementsByClassName('title')[0].innerText = v.subject; detailTemplate.getElementsByClassName('desc')[0].innerText = v.subject; var extInfoObj = detailTemplate.getElementsByClassName('ext-info')[0]; extInfoObj.getElementsByClassName('from')[0].innerText = v.fname; extInfoObj.getElementsByClassName('view-time')[0].innerText = v.views; postListObj.appendChild(detailTemplate); } } else { isLoad = false; } } else { isLoad = false; } }, function(status) { console.log('Something went wrong, status is ' + status); }); } }
#免責(zé)聲明#
本站[綠夏技術(shù)導(dǎo)航]提供的一切軟件、教程和內(nèi)容信息僅限用于學(xué)習(xí)和研究目的;不得將上述內(nèi)容用于商業(yè)或者非法用途,否則,一切后果請用戶自負(fù)。本站信息來自網(wǎng)絡(luò)收集整理,版權(quán)爭議與本站無關(guān)。您必須在下載后的24個小時之內(nèi),從您的電腦或手機中徹底刪除上述內(nèi)容。如果您喜歡該程序或內(nèi)容,請支持正版,購買注冊,得到更好的正版服務(wù)。我們非常重視版權(quán)問題,如有侵權(quán)請郵件[admin@lxwl520.com]與我們聯(lián)系進(jìn)行刪除處理。敬請諒解!