码迷,mamicode.com
首页 > 其他好文 > 详细

滚动加载数据

时间:2015-09-24 19:06:19      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:

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

 

滚动加载数据

标签:

原文地址:http://www.cnblogs.com/flyfly/p/4836212.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!