滾動加載數據

function scrollLoad() {
        var ele = document.getElementById('mainListBlock');
        var isLastPage = false;
        window.isLoading = false;
        var clientHeight = getClientHeight();

        function getScrollTop() {
            return ele.scrollTop;
        }
        function getScrollHeight() {
            return ele.scrollHeight;
        }
        function getClientHeight() {
            var clientHeight = 0;
            if (document.body.clientHeight && document.documentElement.clientHeight) {
                clientHeight = Math.min(document.body.clientHeight, document.documentElement.clientHeight) + 110;
            }
            else {
                clientHeight = Math.max(document.body.clientHeight, document.documentElement.clientHeight) + 110;
            }
            return clientHeight;
        }
        function reachBottom() {
            var scrollTop = getScrollTop();
            var scrollHeight = getScrollHeight();
            /*console.log('頁面頂端滾動距離' + scrollTop);
             console.log('窗口高度' + clientHeight);*/
            //console.log('距離底部距離' + (scrollHeight - scrollTop - clientHeight));

            return (scrollTop + clientHeight == scrollHeight) ? true : false;
        }
        function handleTouch(e) {
            switch(e.type) {
                case 'touchstart':
                    //console.log('touch start');
                    break;
                case 'touchmove':
                    //console.log('touch move');
                    //若是到達頁面低部,則出現提示:鬆開加載更多,
                    /*if (reachBottom()) {
                     e.preventDefault();
                     }*/
                    break;
                case 'scroll':
                    //console.log('scroll');
                    if (reachBottom() && window.pageCount - window.pageNo > 0 && !window.isLoading) {
                        console.log(window.pageCount - window.pageNo);
                        window.pageNo++;
                        //console.log('load more data');
                        window.isLoading = true;//避免重複加載
                        loadGoodsList(window.pageNo);
                    }
                    break;
            }
        }
        ele.addEventListener('touchstart', handleTouch);
        ele.addEventListener('touchmove', handleTouch);
        ele.addEventListener('scroll', handleTouch);
    }
相關文章
相關標籤/搜索