html5 touch事件实现触屏页面上下滑动(一)

2020-04-24 19:07:57易采站长站整理

        </div>  
    </div>  
  
    <script>  
        var startX,//触摸时的坐标   
            startY,   
             x, //滑动的距离   
             y,   
             aboveY=0; //设一个全局变量记录上一次内部块滑动的位置    
  
         var inner=document.getElementById("inner");   
            
            function touchSatrt(e){//触摸   
                e.preventDefault();   
                var touch=e.touches[0];   
                startY = touch.pageY;   //刚触摸时的坐标                 
            }   
  
            function touchMove(e){//滑动             
                 e.preventDefault();           
                 var  touch = e.touches[0];                  
                 y = touch.pageY – startY;//滑动的距离   
                //inner.style.webkitTransform = ‘translate(‘ + 0+ ‘px, ‘ + y + ‘px)’;  //也可以用css3的方式        
                inner.style.top=aboveY+y+"px"; //这一句中的aboveY是inner上次滑动后的位置