瀏覽器滾動(dòng)到底部加載更多數(shù)據(jù)

列表頁面加載更多數(shù)據(jù)的實(shí)現(xiàn)辦法如下,根據(jù)個(gè)別情況,還需要考慮加載次數(shù)過多的邏輯處理。

<!DOCTYPE html>
<html>

    <head>
        <meta charset="utf-8">
        <title>滾動(dòng)加載更多</title>
        <style type="text/css">
            body,
            div {
                margin: 0;
                padding: 0;
            }
            
            body {
                width: 100%;
                background-color: #ccc;
                display: flex;
                flex-wrap: wrap;
                justify-content: space-around;
                border: 1px solid gold;
            }
            
            .load_div {
                width: 400px;
                height: 300px;
                border: 1px solid red;
            }
            
            .load_div2 {
                width: 400px;
                height: 300px;
                border: 1px solid green;
            }
        </style>
    </head>

    <body>
        <div class="load_div"></div>
        <div class="load_div"></div>
        <div class="load_div"></div>
        <div class="load_div"></div>
        <div class="load_div"></div>
        <div class="load_div"></div>
        <div class="load_div"></div>
        <div class="load_div"></div>
        <div class="load_div"></div>
    </body>
    <script src="js/jquery-1.11.0.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript">
        var totalHeight = $(document).height(); //整個(gè)文檔高度
        var seeHeight = $(window).height(); //瀏覽器可視窗口高度
        var scrollTop = $(window).scrollTop(); //瀏覽器可視窗口頂端距離網(wǎng)頁頂端的高度

        //添加滾動(dòng)事件
        $(window).scroll(function() {
            scrollTop = $(window).scrollTop();
            totalHeight = $(document).height();
            console.log(scrollTop, seeHeight, totalHeight)
            //滾動(dòng)條+可視高度+50,距離文檔高度差50的時(shí)候就要加載數(shù)據(jù)了
            if(scrollTop + seeHeight + 50 > totalHeight) {
                var htmlText = '<div class="load_div2"></div><div class="load_div2"></div><div class="load_div2"></div>';
                $('body').append(htmlText);
            }
        })
    </script>
</html>

加載過于頻繁的問題,可以參考另一篇文章事件節(jié)流來解決。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容