实现页面下拉刷新的几个方法

原生JS实现:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>下拉刷新</title>
<meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no"/>
<style type="text/css">
 .list li {
         list-style-type:none;
         line-height:50px;
         height:50px;
         font-size:1.5em;
         border-bottom:1px solid #6BA5E7;
     }
     .list ul:hover{
        cursor:pointer;
    }
</style>
</head>
<body>
<h1>下拉刷新</h1>
<div class="list">
    <div id="touchloader" style="text-align: center;display: none;">
        Loading.....
    </div>
    <ul id="touchlist" style="position: relative;top:0px;">
        <li>111111111</li>
        <li>222222222</li>
        <li>333333333</li>
        <li>44444444</li>
        <li>555555555</li>
    </ul>
</div>
<script type="text/javascript">
      var list = document.getElementById("touchlist"),
            loader = document.getElementById("touchloader"),
            isTouched = false,
            isMoved = false;
        // list列表距离body的距离
        var prevY = parseInt(list.offsetTop);
        // list绝对定位的高度
        var cssY = list.style.top;
        cssY = parseInt(cssY.substring(0, cssY.length - 2));
        //添加手机触摸事件
        list.addEventListener("touchstart", function (e) {
            isTouched = true;
            //初始化触摸的位置
            prevY = e.changedTouches[0].clientY;
            //添加 css3 效果
            list.style.transition = "";
            e.preventDefault();
        }, false);
        list.addEventListener("touchend", function (e) {
            // 取消向上划屏是的触摸事件
            isTouched = false;
            // 如果列表向下拉了,向上放回去有个css3的过渡效果
            list.style.transition = "top 1s";
            if (isMoved) {
                //显示加载的元素
                loader.style.display = "block";
                loadNewData();
            }
            list.style.top = cssY + 'px';
            isMoved = false;
            e.preventDefault();
        }, false);
        list.addEventListener("touchmove", function (e) {
            if (isTouched) {
                if (e.changedTouches[0].clientY > prevY) {
                    var change = e.changedTouches[0].clientY - prevY;
                    list.style.top = cssY + change + 'px';
                    isMoved = true;
                }
            }
            e.preventDefault();
        }, false);
        //绑定鼠标事件让在电脑浏览器里也能用
        list.addEventListener("mousedown", function (e) {
            isTouched = true;
            prevY = e.clientY;
            list.style.transition = "";
            e.preventDefault();
        }, false);
        list.addEventListener("mouseup", function (e) {
            isTouched = false;
            list.style.transition = "top 1s";
            if (isMoved) {
                loader.style.display = "block";
                loadNewData();
            }
            list.style.top = cssY + 'px';
            isMoved = false;
            e.preventDefault();
        }, false);
        list.addEventListener("mousemove", function (e) {
            if (isTouched) {
                if (e.clientY > prevY) {
                    var change = e.clientY - prevY;
                    list.style.top = cssY + change + 'px';
                    isMoved = true;
                }
            }
            e.preventDefault();
        }, false);
        function loadNewData() {
            setTimeout(function () {
                list.innerHTML = '<li>新加的元素</li><li>new user 2</li>' + list.innerHTML;
                loader.style.display = "none";
            }, 1000);
            /**
            * do what ever to get data here
            */
        }
  </script>
</body>
</html>

 

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
<title>js 模拟手机页面文件的下拉刷新</title>
<style type="text/css">
    #slideDown{margin-top: 0;width: 100%;}
         #slideDown1,#slideDown2{width: 100%;height: 70px;;background: #e9f4f7;display: none;}
         #slideDown1{height: 20px;}
         #slideDown1>p,#slideDown2>p{margin: 20px auto;text-align:center;font-size: 14px;color: #37bbf5;}
</style>
</head>
<body>
    <div id="content">
        <div id="slideDown">
            <div id="slideDown1">
                <p>松开刷新</p>
            </div>
            <div id="slideDown2">
                <p>正在刷新 ...</p>
            </div>
        </div>
        <div class="myContent">
            <ul>
                <li>item1 -- item1 -- item1</li>
                <li>item2 -- item2 -- item2</li>
                <li>item3 -- item3 -- item3</li>
                <li>item4 -- item4 -- item4</li>
                <li>item5 -- item5 -- item5</li>
                <li>item6 -- item6 -- item6</li>
                <li>item7 -- item7 -- item7</li>
            </ul>
        </div>
    </div>
    <script type="text/javascript">
    //第一步:下拉过程
    function slideDownStep1(dist){  // dist 下滑的距离,用以拉长背景模拟拉伸效果
        var slideDown1 = document.getElementById("slideDown1"),
            slideDown2 = document.getElementById("slideDown2");
        slideDown2.style.display = "none";
        slideDown1.style.display = "block";
        slideDown1.style.height = (parseInt("20px") - dist) + "px";
    }
    //第二步:下拉,然后松开,
    function slideDownStep2(){ 
        var slideDown1 = document.getElementById("slideDown1"),
            slideDown2 = document.getElementById("slideDown2");
        slideDown1.style.display = "none";
        slideDown1.style.height = "20px";
        slideDown2.style.display = "block";
        //刷新数据
        //location.reload();
    }
    //第三步:刷新完成,回归之前状态
    function slideDownStep3(){ 
        var slideDown1 = document.getElementById("slideDown1"),
            slideDown2 = document.getElementById("slideDown2");
        slideDown1.style.display = "none";
        slideDown2.style.display = "none";
    }

    //下滑刷新调用
    k_touch("content","y");
    //contentId表示对其进行事件绑定,way==>x表示水平方向的操作,y表示竖直方向的操作
    function k_touch(contentId,way){ 
        var _start = 0,
            _end = 0,
            _content = document.getElementById(contentId);
        _content.addEventListener("touchstart",touchStart,false);
        _content.addEventListener("touchmove",touchMove,false);
        _content.addEventListener("touchend",touchEnd,false);
        function touchStart(event){ 
            //var touch = event.touches[0]; //这种获取也可以,但已不推荐使用

            var touch = event.targetTouches[0];
            if(way == "x"){ 
                _start = touch.pageX;
            }else{ 
                _start = touch.pageY;
            }
        }
        function touchMove(event){ 
            var touch = event.targetTouches[0];
            if(way == "x"){ 
                _end = (_start - touch.pageX);
            }else{ 
                _end = (_start - touch.pageY);
                //下滑才执行操作
                if(_end < 0){
                    slideDownStep1(_end);
                }
            }

        }
        function touchEnd(event){ 
            if(_end >0){ 
                console.log("左滑或上滑  "+_end);
            }else{ 
                console.log("右滑或下滑"+_end);
                slideDownStep2();
                //刷新成功则
                //模拟刷新成功进入第三步
                setTimeout(function(){ 
                    slideDownStep3();
                },2500);
            }
        }
    }
    </script>
</body>
</html>

 

posted @ 2015-09-11 14:36  赵小磊  阅读(2073)  评论(0)    收藏  举报
回到头部