码迷,mamicode.com
首页 > 移动开发 > 详细

移动端整屏滑动的实现

时间:2017-03-14 23:58:19      阅读:468      评论:0      收藏:0      [点我收藏+]

标签:efault   classname   执行   change   大于   round   获取   默认   end   

基本思路:

1)检测手指滑动方向:获取手指抬起时的位置,减去手指按下时的位置,得正即为向下滑动了

2)手指抬起后,向对应反向操作改变当前页的位置

具体代码如下:

html

<div id="wrap">
    <div id="page01" class="pages">第一屏</div>
    <div id="page02" class="pages">第二屏</div>
    <div id="page03" class="pages">第三屏</div>
    <div id="page04" class="pages">第四屏</div>
</div>
<div id="dots">
    <span class="now"></span><span class=""></span><span class=""></span><span class=""></span>
</div>

css

*{
            margin:0;
            padding:0;
        }
        body{
            overflow: hidden;
        }
        #wrap > div{
            width: 10rem;
            position: absolute;
            left: 0;
            top: 0;
            background: #fff;
            transition: all 0.3s ease;
        }
        #dots{
            position: fixed;
            right: 5px;
            top: 40%;
            z-index: 9;
        }
        #dots span{
            display: block;
            height: 0.2rem;
            width: 0.2rem;
            border: 1px solid #000;
            border-radius: 50%;
            margin-bottom: 3px;
        }
        #dots .now{
            background: #555;
        }

js分为两块

第一,设置html标签的font-size,以便设置rem的基数 (放在页面头部)

document.getElementsByTagName("html")[0].style.fontSize = window.innerWidth/10 + "px";

第二,具体的滑动操作代码

        window.onload = function(){
            var oDiv = document.getElementById("wrap");
            var aPages = oDiv.getElementsByClassName("pages");
            var aDots = document.getElementById("dots").getElementsByTagName("span");
            var winH = window.innerHeight;
            var tTime = 1;
            

            //设置每页的高度和zindex值
            for(var i=0; i<aPages.length; i++){
                aPages[i].style.height = winH + "px";
                aPages[i].style.zIndex = 1;
            }
            aPages[0].style.zIndex = 3;
            aPages[1].style.zIndex = 2;
            oDiv.style.height = winH + "px";

            //手指拖动事件(去除默认动作)
            document.addEventListener("touchmove",function(e){
                e.preventDefault();
            });

            var YStart = 0;
            var iNow = 0;
            //手指按下
            oDiv.addEventListener("touchstart",function(e){
                YStart = e.changedTouches[0].clientY;
            });
            //手指移动
            oDiv.addEventListener("touchmove",function(e){

                disY = e.changedTouches[0].clientY-YStart; //向下滑正,向上滑负
            });

            //手指离开
            oDiv.addEventListener("touchend",function(e){
                
                disY = e.changedTouches[0].clientY-YStart; //向下滑正,向上滑负
                if(Math.abs(disY)>winH/20){  //只有当滑动距离大于了一定值得时候,才执行切换
                    if(disY<0){
                        iNow++;
                        if(iNow>=aDots.length){
                            iNow = 0;
                        }
                        aPages[0].style.transform = "translateY("+ -winH +"px)";
                        doSlide();
                    }else{
                        iNow--;
                        if(iNow<0){
                            iNow = aDots.length-1;
                        }
                        aPages[0].style.transform = "translateY("+ winH +"px)";
                        doSlide("up");
                    }
                }
            });

            function doSlide(upflag){
                for(var i=0;i<aDots.length;i++){
                    aDots[i].className = "";
                }
                aDots[iNow].className = "now";
                if(upflag){
                        //向上滑
                        aPages[3].style.zIndex = 2;
                        aPages[1].style.zIndex = 1;
                        oDiv.insertBefore(aPages[3],aPages[1]);
                        setTimeout(function(){
                            aPages[1].style.transform = "translateY(0px)";
                            aPages[1].style.zIndex = 2;
                            aPages[0].style.zIndex = 3;
                        },300)
                }else{
                    setTimeout(function(){
                        aPages[0].style.transform = "translateY(0px)";
                        aPages[0].style.zIndex = 1;
                        aPages[1].style.zIndex = 3;
                        aPages[2].style.zIndex = 2;
                        oDiv.appendChild(aPages[0]);
                    },300)
                }
                
            }


        }

 

移动端整屏滑动的实现

标签:efault   classname   执行   change   大于   round   获取   默认   end   

原文地址:http://www.cnblogs.com/chengduxiaoc/p/6551417.html

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