欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JavaScript实现移动端轮播效果

程序员文章站 2022-07-05 20:24:10
这个轮播代码不是我自己所写,是偶然游览一个简友的主页看到的,今天刚看了事件,决定来逐行分析一下这个代码。首先,移动端与电脑端不同的是移动端只能通过触摸和手势来发生行为,所以...

这个轮播代码不是我自己所写,是偶然游览一个简友的主页看到的,今天刚看了事件,决定来逐行分析一下这个代码。首先,移动端与电脑端不同的是移动端只能通过触摸和手势来发生行为,所以我们要用到js中的与触摸操作有关的新事件。其实我们轮播的原理和以前相同,都是通过改变元素的位置来控制图片的出现,但让我们不是很上手的,是那些用于跟踪触摸的属性。

下面,我简单介绍一下与触摸相关的知识

触摸事件

  • touchstart:当手指触摸屏幕时触发,一只手指放在屏幕上也会触发
  • touchmove:当手指在屏幕上滑动时连续触发。在这个事件发生期间可以用preventdefault()可以阻止滚动。
  • touchend:手指从屏幕移开时触发。
  • touchcancel:当系统停止跟踪触发时触发。

上面这几个事件都会冒泡,也都可以取消。

属性

  • touches:表示当前跟踪的触摸操作的touch对象的数组
  • targettouches:特定于事件目标的touch对象的数组。(简单点可以理解为手指触摸屏幕的位置)
  • changetouches表示自上次触摸以来发生了什么改变的touch对象的数组。(手指离开的位置)

每个touch对象包括下列属性

  • clientx:触摸目标在视口中的x坐标
  • clienty:触摸目标在视口中的y坐标
  • identifier:标识触摸的唯一id
  • pagex:触摸目标在页面中的x坐标
  • pagey:触摸目标在页面中的y坐标
  • screenx:触摸目标在页面中的x坐标
  • screeny:触摸目标在页面中的y坐标
  • target:触摸的dom节点目标

代码如下

<!doctype html>
<html>
<head>
 <title>移动端轮播</title>
 <style type="text/css">
  * {
   margin: 0;
   padding: 0;
  }
  #box {
   width: 980px;
   overflow: hidden;
   position: relative;
   margin: 0 auto;
  }
  #box ul {
   width: 4900px;
   height: 100px;
   position: relative;
   transform: translatex(-980px);
   
  }
  #box ul li{
   width: 980px;
   height: 100px;
   float: left;
   list-style-type: none;
   text-align: center;
   color: #fff;
   line-height: 100px;
   font-size: 30px;
  }
 </style>
 <script type="text/javascript">
  window.onload = function() {
   var box = document.getelementbyid('box');
   var oul = document.getelementsbytagname('ul')[0];
   var ali = oul.children;//这样就可以把所有li标签获取到了
   var inow = 1;
   var x = -inow*ali[0].offsetwidth;//li标签的位置
   var bready = true;
   oul.addeventlistener('touchstart',function(ev){

    //当手指触摸屏幕的时候触发事件,让li的位置随手指滑动而改变
    if(bready == false){return;}
    bready = false; 
      
    var downx = ev.targettouches[0].pagex; //记录触摸位置 
    var disx = downx - x;//下一个li要移动到的位置
    function fnmove(ev) {
     x = downx-disx;
     oul.style.transform = 'translate3d('+x+'px,0,0)';
    }
    function fnend(ev){
     var upx = ev.changedtouches[0].pagex;//离开的位置
     if(math.abs(upx - downx)>50){
      //左边移动
      if(upx - downx<0){
       inow++;
       if(inow==ali.length){inow=ali.length-1;}
      }else{
      //右边移动
       inow--;
       if(inow==-1){inow=0;}
      }
     }
     x = -inow*ali[0].offsetwidth;
     oul.style.transform = 'translate3d('+x+'px,0,0)';
     oul.style.transition = '200ms all ease';
     function tend(){
      if(inow==4){
       inow=1;
      }
      if(inow==0){inow=3;}
      oul.style.transition = 'none'
      x = -inow*ali[0].offsetwidth;
      oul.style.transform = 'translate3d('+x+'px,0,0)';
      bready = true;
     } 
     oul.addeventlistener('transitionend',tend,false);
     //释放内存
     document.removeeventlistener('touchend',fnend,false);
     document.removeeventlistener('touchmove',fnmove,false);

    }
    document.addeventlistener('touchmove',fnmove,false);
    document.addeventlistener('touchend',fnend,false);
    //阻止默认事件
    ev.preventdefault();
   },false);

  }
 </script>
</head>
<body>
 <div id="box"> 
 <ul>
  <li style="background:green">3</li>
  <li style="background:orange">1</li>
  <li style="background:yellow">2</li>
  <li style="background:green">3</li>
  <li style="background:orange">1</li>
 </ul>
 </div> 
</body>
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。