VUE 實現監聽滾動事件,實現數據懶加載


methods: {
    // 獲取滾動條當前的位置
    getScrollTop() {
        let scrollTop = 0
        if (document.documentElement && document.documentElement.scrollTop) {
            scrollTop = document.documentElement.scrollTop
        } else if (document.body) {
            scrollTop = document.body.scrollTop
        }
        return scrollTop
    },
    // 獲取當前可視范圍的高度
    getClientHeight() {
        let clientHeight = 0
        if (document.body.clientHeight && document.documentElement.clientHeight) {
            clientHeight = Math.min(document.body.clientHeight, document.documentElement.clientHeight)
        } else {
            clientHeight = Math.max(document.body.clientHeight, document.documentElement.clientHeight)
        }
        return clientHeight
    },

    // 獲取文檔完整的高度
    getScrollHeight() {
        return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight)
    },
    // 滾動事件觸發下拉加載
    onScroll() {
        if (this.getScrollHeight() - this.getClientHeight() - this.getScrollTop() <= 0) {
            if (this.status <= 5) {
                this.status++;
                // 調用請求函數
                this.axios.get('url'
                ).then(data => {
                    console.log(data)
                });
            } 
        }
    },
}

監聽事件

mounted() {
    this.$nextTick(function () { // 解決視圖渲染,數據未更新
        window.addEventListener('scroll', this.onScroll); 
    })
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM