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); }) }