vue中添加滾動載入更多,因為是單頁面所以需要在跳出頁面時候銷毀滾動,要不會出現錯亂。我們在mounted建立滾動,destroyed銷毀滾動。 mounted () { window.addEventListener('scroll', this.handleScroll) }, destroye ...
vue中添加滾動載入更多,因為是單頁面所以需要在跳出頁面時候銷毀滾動,要不會出現錯亂。我們在mounted建立滾動,destroyed銷毀滾動。
mounted () { window.addEventListener('scroll', this.handleScroll) }, destroyed () { window.removeEventListener('scroll', this.handleScroll) },
定義一個函數,在滾動到底部時候使滾動條距離頂部距離與可視區域高度之和等於滾動條總高度,在載入後如果列表長度為0時應該停止載入,要不會出現一直載入的情況
handleScroll () { //變數scrollTop是滾動條滾動時,距離頂部的距離 var scrollTop = document.documentElement.scrollTop||document.body.scrollTop; //變數windowHeight是可視區的高度 var windowHeight = document.documentElement.clientHeight || document.body.clientHeight; //變數scrollHeight是滾動條的總高度 var scrollHeight = document.documentElement.scrollHeight||document.body.scrollHeight; //滾動條到底部的條件 if(scrollTop+windowHeight==scrollHeight &&this.list.length !==0){ this.loadMore() // 載入的列表數據 } }