js 滾動條滾動到底部時,觸發(fā)加載數(shù)據(jù)

<!DOCTYPE html>
  <html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title></title>
    <style>
        #table_content {
            width: 80%;
            height: 300px;
            border: 1px solid #ccc;
            overflow-y: scroll;
        }
    </style>
</head>
<body>
    <div id="app">
        <div id="table_content" @scroll="scroll">
            <table id="table" border="1">
                <tr v-for="item in list">
                    <td>{{item.id}}</td>
                    <td>{{item.title}}</td>
                </tr>
            </table>
        </div>
    </div>
</body>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script>
    var vm = new Vue({
        el: '#app',
        data: {
            list: [],//列表數(shù)據(jù)
            page: 1,//分頁,默認為第一頁
            status: 1,//狀態(tài),為0不在請求數(shù)據(jù)
        },
        methods: {
            getData() {
                var that = this;
                if(that.status==0){
                    console.log("沒有更多啦");
                    return false;
                }
                $.ajax({
                    url: "http://jsonplaceholder.typicode.com/todos",
                    data: {
                        _page: that.page,
                        _limit: 15
                    },
                    success: function(result) {
                        if (that.page == 1) {
                            that.list = result;
                            setTimeout(function(){
                                that.scroll();
                            });
                        } else {
                            if (result.length > 0) {
                                that.list = that.list.concat(result);
                                setTimeout(function(){
                                    that.scroll();
                                });
                            } else {
                                that.status = 0;//沒有數(shù)據(jù)將狀態(tài)改為0
                                console.log("沒有更多啦");
                            }
                        }
                    }
                });
            },
            scroll() {
                var that = this;
                var scrollHeight = document.getElementById("table_content").scrollHeight;
                var scrollTop = document.getElementById("table_content").scrollTop;
                var clientHeight = document.getElementById("table_content").clientHeight;

                console.log(scrollHeight+"----"+scrollTop+"----"+clientHeight);
                if (scrollHeight - clientHeight == scrollTop) {
                    that.page++;
                    that.getData();
                }
            }
        }
    })
    
    // 首次請求數(shù)據(jù)
    vm.getData();
  </script>
</html>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

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