滚动加载数据

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

 

posted @ 2015-09-24 18:35  冰狐2009  阅读(221)  评论(0编辑  收藏  举报