js制作轮播图(包括动画函数封装、定时器、自定义属性绑定序号等的应用)

1.缓动动画的基本原理

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=, initial-scale=1.0">
    <title>Document</title>
    <style>
      div{
        background-color: pink;
        width: 200px;
        height: 200px;
        position: absolute;
        left: 0;
      }
    </style>
</head>
<body>
  <button>点击才走</button>
    <div></div>
    <script>
      var div=document.querySelector('div');
     
      function animate(obj,target,change){
        //change=function()
        clearInterval(obj.time);
      obj.time=setInterval(function() {
        var step=Math.ceil((target-obj.offsetLeft)/10)//往上取整
        if(obj.offsetLeft==target){
          clearInterval(obj.time);
          if(change){
            change(); //调用函数,动画结束之后才执行,回调函数
          }
        }
        obj.style.left = obj.offsetLeft + step + 'px';
      },30)
      }
      
    var btn=document.querySelector('button');
    btn.addEventListener('click',function(){
      animate(div,500,function(){  //实参可以传递函数给实参
        div.style.backgroundColor='purple';
      });
    })
    </script>
</body>
</html>

2.缓动动画的应用1——侧边画出栏的制作

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        span{
            width: 50px;
            height: 50px;
            background-color: pink;
            text-align: center;
            line-height: 50px;
            z-index: 2;
            float: left;
            position: absolute;
            right: 0;
        }
        .que{
            height: 50px;
            width: 150px;
            text-align: center;
            line-height: 50px;
            background-color: pink;
            z-index: 1;
            float: left;
            position: absolute;
            right: -100px;
        }
        .box{
            width: 200px;
            height: 50px;
            position: absolute;
            right: 0;
        }
    </style>
</head>
<body>
    <div class="box">
        <span>⬅</span>
        <div class="que">问题反馈</div>
    </div>
    <script>
        var span=document.querySelector('span');
        var wenti=document.querySelector('.que');
        span.addEventListener('mouseenter',function(){
            animate(wenti,0,function(){
                span.innerHTML='➡';
            })
        })
        span.addEventListener('mouseleave',function(){
            animate(wenti,150)
        })
        function animate(obj,target,change){
        //change=function()
        clearInterval(obj.time);
        obj.time=setInterval(function() {
        var step=Math.ceil((target-obj.offsetLeft)/10)//往上取整
        if(obj.offsetLeft==target){
          clearInterval(obj.time);
          if(change){
            change(); //调用函数,动画结束之后才执行,回调函数
          }
        }
        obj.style.left = obj.offsetLeft + step + 'px';
      },30)
      }
    </script>
</body>
</html>

3.缓动动画的应用2——轮播图

html和css部分 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>轮播图</title>
    <style>
        .lunbo li a img{
    width: 720px;
    height: 455px;
}
.lunbo li{
    float: left;
}
.lunbo{
    position: absolute;
    top: 0;
    left: 0;
    margin: 0;
    padding: 0;
    width: 600%;
    /* 父盒子比ul小,一行显示不完,所以不能浮动,必须设置ul的宽度足够宽 */
}
.focus{
    position: relative;
    width: 720px;
    height: 455px;
    background-color: purple;
    margin: 100px auto;
    overflow: hidden;
}
.arrow-l,.arrow-r{
    position: absolute;
    top: 50%;
    margin-top: -20px;
    width: 24px;
    height: 40px;
    background: rgba(0, 0, 0, .3);
    text-align: center;
    line-height: 40px;
    color: #fff;
    font-family: 'icomoon';
    font-size: 18px;
    display: none;
    z-index: 2;
}
.arrow-r{
    right: 0;
}
.circle li{
    float: left;
    width: 8px;
    height: 8px;
    border: 2px solid rgba(255, 255, 255, 0.5);
    border-radius: 50%;
    margin: 0 3px;
    cursor: pointer;
}
.circle{
    position: absolute;
    bottom: 10px;
    left: 300px;
}
.focus img{
    width: 720px;
    height: 455px;
}
ul li{
    list-style: none;
}
.current{
    background-color: #fff;
}
    </style>
</head>
<script src="./JS.js"></script>
<body>
            <div class="focus f1">
                <!-- 左侧按钮 -->
                <a href="javascript:;" class="arrow-l">&lt;</a>
                <!-- 右侧按钮 -->
                <a href="javascript:;" class="arrow-r">&gt;</a>
                <!-- 核心滚动区域 -->
                <ul class="lunbo">
                    <li>
                        <a href="#"><img src="./imgs/img1.jpg" alt="" class="image"></a>
                    </li>
                    <li>
                        <a href="#"><img src="./imgs/img2.jpg" alt=""></a>
                    </li>
                    <li>
                        <a href="#"><img src="./imgs/img3.jpg" alt=""></a>
                    </li>
                    <li>
                        <a href="#"><img src="./imgs/img4.jpg" alt=""></a>
                    </li>
                </ul>
                <!-- 下面的小圆圈 -->
                <ul class="circle">
                </ul>
            </div>

</body>
</html>

 js部分

window.onload = function() {
    // 移动动画函数
    function animate(obj,target,change){
        // change=function()
        clearInterval(obj.time);
      obj.time=setInterval(function() {
        // var step=Math.ceil((target-obj.offsetLeft)/10)//往上取整
        var step=(target-obj.offsetLeft)/10;
        step=step>0? Math.ceil(step):Math.floor(step);//大于0则向上取整,小于0则向下取整
        if(obj.offsetLeft==target){
          clearInterval(obj.time);
          if(change){
            change(); //调用函数,动画结束之后才执行,回调函数
          }
        }
        obj.style.left = obj.offsetLeft + step + 'px';
      },20)
      }
    var arrow_l=document.querySelector('.arrow-l');
    var arrow_r=document.querySelector('.arrow-r');
    var focus=document.querySelector('.focus');
    var focuswidth=focus.offsetWidth;
    //鼠标经过就显示和隐藏
    focus.addEventListener('mouseenter',function(){
        arrow_l.style.display='block';
        arrow_r.style.display='block';
        clearInterval(timer);
    })
    focus.addEventListener('mouseleave',function(){
        arrow_l.style.display='none';
        arrow_r.style.display='none';
        timer=setInterval(function(){
            //手动调用点击事件(直接调用右侧的点击事件)
            arrow_r.click();
        },2000)
    })
    //动态生成小圆圈
    var lunbo=focus.querySelector('.lunbo');
    var circle=focus.querySelector('.circle');
    // var index;
    for(var i=0;i<lunbo.children.length;i++){
        var li=document.createElement('li');
        // 自定义属性
        li.setAttribute('index',i);
        circle.appendChild(li);
        //排他思想绑定点击事件
        li.addEventListener('click',function(){
            for(var i=0;i<circle.children.length;i++){
               circle.children[i].className='';
            //    circle.children[i].index=i;
        }
        this.className='current';
        var index=this.getAttribute('index');
        //当点击了li就要把当前的index值拿给num
        num=index;
        //当点击了li就要把当前的index值拿给circle1
        circle1=index;
        // 调用函数
        animate(lunbo,-index*focuswidth);
        })
    }
    circle.children[0].className='current';
    //右侧箭头点击图片移动开始
    // 将第一个小li克隆一份放到轮播的最后
    var first=lunbo.children[0].cloneNode(true);
    lunbo.appendChild(first);
    var num=0;
    var circle1=0;//控制小圆圈的播放
    arrow_r.addEventListener('click',function(){
        if(num==4){
            lunbo.style.left=0;
            num=0;
        }
        num++;
        animate(lunbo,-num*focuswidth);
        circle1++;
        //如果circle1==4 说明走到了最后我们克隆的这张照片了 我们就复原
        if(circle1==4){
            circle1=0;
        }
        //先清除其余小圆圈的current类名
        for(var i=0;i<circle.children.length;i++){
            circle.children[i].className='';
        }
        //留下当前的小圆圈的current类名
        circle.children[circle1].className='current';
})
    //左侧箭头点击图片移动开始
        arrow_l.addEventListener('click',function(){
        if(num==0){
            lunbo.style.left=-(lunbo.children.length-1)*focuswidth+'px';
            num=lunbo.children.length-1;
        }
        num--;
        animate(lunbo,-num*focuswidth);
        circle1--;
        //如果circle1<0 说明走到了第一张图片 
        if(circle1<0){
            circle1=circle.children.length-1;
        }
        //先清除其余小圆圈的current类名
        for(var i=0;i<circle.children.length;i++){
            circle.children[i].className='';
        }
        //留下当前的小圆圈的current类名
        circle.children[circle1].className='current';
})
        //自动播放功能
        var timer=setInterval(function(){
         //手动调用点击事件(直接调用右侧的点击事件)
         arrow_r.click();
        },2000)
        
}

 

在写轮播图的时候遇到一个问题,做鼠标移动和离开的时候左右箭头则显示和隐藏,但是控制台一直报错,卡了两天,本来以为是因为js读取文档顺序的问题就一直在这个问题上纠结,最后才发先原来是因为类名抄写错误了,所以友友们写代码的时候一定要仔细呀,这么个小小的问题就花了大量的时间去看到底哪里出问题了

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值