// 通常寫法
window.onscroll = function (e) { /* 判斷是否滾到最下面 */ /* 如果已經滾到最下面則執行某個操作 */ var e =e || window.event; // 為了兼容谷歌和火狐 document.body.scrollTop是谷歌上的 /* 滾動條的垂直位置 */ var scrolltop = document.documentElement.scrollTop||document.body.scrollTop; /* 整個頁面的正文高度 */ var scrollHeight = document.documentElement.scrollHeight||document.body.scrollHeight; /* 可見區域高度 */ var clientHeight = document.documentElement.clientHeight||document.body.clientHeight; /* 當scrolltop加clientHeight 等於scrollHeight */ if(scrollHeight === (scrolltop+clientHeight)){ loadmore(); } }
// 特定區域元素
handleScroll() { // -160 去掉非可視List內容區高度 var scrollTop = this.answerRef.scrollTop; // 滾動條溢出高度 var windowHeight = (document.documentElement.clientHeight - 160) || (document.body.clientHeight - 160); // 可視窗口高度 var scrollHeight = this.answerRef.scrollHeight; // 滾動內容高度 // console.log(scrollTop, windowHeight, scrollHeight) if (scrollTop + windowHeight == scrollHeight) {this.pageInfo.pageNum++; return false; } },
this.$nextTick(() => {
this.answerRef = this.$refs.answer;
window.addEventListener("scroll", this.handleScroll, true);
})