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

原生js轮播特效

程序员文章站 2023-11-09 10:23:52
作为一名前端工程师,手写轮播图应该是最基本掌握的技能,以下是我自己原生js写的轮播,欢迎指点批评: 首先css代码 a{text-decoration:no...

作为一名前端工程师,手写轮播图应该是最基本掌握的技能,以下是我自己原生js写的轮播,欢迎指点批评:

原生js轮播特效

首先css代码

a{text-decoration:none;color:#3dbbf5;}
   *{
    margin: 0;
    padding: 0;
   }
   .wrapper{
    width: 400px;
    height: 300px;
    margin: 100px auto;
   }
   #lunbo{
    position: relative;
    overflow: hidden;
   }
   #list{
    position: relative;
    white-space: nowrap; // 这块用行元素模拟,所以才用该属性,块元素可修改这块
   }
   #list span{
    display: inline-block;
    width: 400px;
    height: 300px;
    text-align: center;
    line-height: 300px;
    font-weight: bold;
    font-size: 100px;
    color: #fff;
   }
   #buttons{
    position: absolute;
    bottom: 0;
    text-align: center;
    width: 100%;
    height: 40px;
    line-height: 40px;
   }
   #buttons span{
    display: inline-block;
    width: 15px;
    height: 5px;
    background: #fff;
    margin: 0 10px;
    cursor: pointer;
    transition: all .5s;
   }
   #buttons span.on{
    height: 20px;
   }
   .arrow{
    position: absolute;
    top: 50%;
    transform: translatey(-50%);
    font-size: 80px;
    font-weight: bold;
    color: #fff;
    opacity: .3;
    transition: all .5s;
   }
   .wrapper:hover .arrow{
    opacity: 1;
   }
   #prev{
    left: 10px;
   }
   #next{
    right: 10px;
   }

然后html代码

<div class="wrapper">
   <div id="lunbo">
    <div id="list" style="left: -400px;">
     <span style="background:yellow;">5</span><span style="background: red;">1</span><span style="background:black;">2</span><span style="background:green;">3</span><span style="background:blue;">4</span><span style="background:yellow;">5</span><span style="background: red;">1</span>
    </div>
    <div id="buttons">
     <span index="1" class="on"></span>
     <span index="2"></span>
     <span index="3"></span>
     <span index="4"></span>
     <span index="5"></span>
    </div>
    <a href="javascript:;" id="prev" class="arrow"><</a>
    <a href="javascript:;" id="next" class="arrow">></a>
   </div>
  </div>

最后js代码

window.onload=function () {
   var lunbo = document.getelementbyid("lunbo");
   var list = document.getelementbyid("list");
   var btn = document.getelementbyid("buttons").getelementsbytagname('span');
   var prev = document.getelementbyid("prev");
   var next = document.getelementbyid('next');
   var interval = 3000;
   var timer;
   var index = 1;
   var animated = false;
   for (var i=0;i<btn.length;i++) { //按钮加点击事件
    btn[i].onclick=function () {
     if(this.classname=='on') //如果是状态按钮直接返回节约资源
     {
      return
     };
     var myindex =parseint(this.getattribute('index'));//获取按钮的index属性值
     var offset = -400*(myindex-index); //根据属性值 计算偏移量
     animate(offset)  //轮播动画
     index = myindex; // 改变索引值
     showbtn();   //显示状态按钮
    }
   }
   function showbtn () { 
    for (var i=0;i<btn.length;i++) {
     btn[i].classname=''; 
    }
    btn[index-1].classname='on';
   }
   prev.onclick=function () { //上一页事件
    if (animated) { //如果是动画状态 直接返回解决bug
     return;
    }
    if (index==1) { 
     index =btn.length;
    } else{
     index-=1;
    }
    animate(400); 
    showbtn();
   }
   next.onclick=function () {
    if (animated) {
     return;
    }
    if (index==btn.length) {
     index =1;
    } else{
     index+=1;
    }
    animate(-400);
    showbtn();
   }
   function animate(offset) {
    animated = true; //表示在动画状态
    var newleft = parseint(list.style.left) + offset; //计算新的left值
    var time = 400; //设置动画总时间
    var interval = 10; //动画帧时间
    var speed = offset/(time/interval); //每帧运动距离
    function go () { 
     if ((speed>0 && parseint(list.style.left)<newleft) || (speed<0 && parseint(list.style.left)>newleft)) { //通过条件判断到它是否还要继续进行动画
      list.style.left = parseint(list.style.left) + speed +'px';
      settimeout(go,interval) 
     } else{
      animated = false; //动画状态结束
      list.style.left = newleft + 'px'; //现在的位移
      if (parseint(list.style.left)<-2000) { // 辅助假图
       list.style.left = -400 + 'px';
      } else if( parseint(list.style.left)>-400){
       list.style.left = -2000 + 'px';
      }
     }
    }
    go();
   }
   function play () { 
    timer = settimeout(function () {
     next.onclick();
     play();
    },interval)
   }
   play();
   function stop () {
    cleartimeout(timer);
   }
   lunbo.onmouseover=stop;
   lunbo.onmouseout=play;
  }

以上是所有代码,欢迎指点交流!

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