vue 原生添加滾動加載更多


   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() // 加載的列表數據
            }
        }

  


免責聲明!

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



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