js手写轮播图--滑动轮播(缓步动画)

<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>
    <link rel="stylesheet" href="轮播.css">
    <script src="animate.js"></script>

</head>


<body>
    <div>
        <ul>
            <li><img src="1.jpeg" alt=""></li>
            <li><img src="2.jpeg" alt=""></li>
            <li><img src="3.jpeg" alt=""></li>
            <li><img src="4.jpeg" alt=""></li>
        </ul>
        <img src="../向左.png" alt="" id="back">
        <img src="../向右.png" alt="" id="next">
        <ol></ol>
    </div>

    <script src="轮播.js"></script>
</body>

/****************缓步动画函数******************* */
function animate (obj, target, callback) {
    // 清除以前的定时器
    clearInterval(obj.timer);
    obj.timer = setInterval(function () {
        // 步长写到定时器  步长公式:(目标值-现在的位置)/10
        var step = (target - obj.offsetLeft) / 10;
        // 把步长改为整数,不要出现小数问题
        step = step > 0 ? Math.ceil(step) : Math.floor(step);
        if (obj.offsetLeft == target) {
            // 停止定时器
            clearInterval(obj.timer);
            // 动画结束后调用,例:节流阀!!!
            if (callback) {
                callback();
            }
        }
        // 把每次加1这个步长改变为一个慢慢变小的值 
        obj.style.left = obj.offsetLeft + step + 'px';
    }, 10)
}
//按钮
var back = document.getElementById('back');
var next = document.getElementById('next');
// 标签
var ul = document.querySelector('ul');
var ol = document.querySelector('ol');
// var ulLis = ul.querySelectorAll('li');

//图片宽度
var imgWidth = ul.getElementsByTagName('img')[0].offsetWidth;

//记录图片对应脚标
var countN = 0;
//记录小圆圈对应脚标
var countC = 0;
//生成小圆圈
for (var i = 0; i < ul.children.length; i++) {
    var li = document.createElement('li');
    ol.appendChild(li);
}

//第一个圆圈选中
ol.children[0].className = 'orange';
//点击小圆圈
for (let i = 0; i < ol.children.length; i++) {
    ol.children[i].addEventListener('click', function () {
        //变色
        for (var j = 0; j < ol.children.length; j++) {
            ol.children[j].className = '';
        }
        this.className = 'orange';
        // 对应图片
        countN = i;
        countC = i;
        animate(ul, -i * imgWidth);

    })
}

//第一张图片复制到最后
var first = ul.children[0].cloneNode(true);
ul.appendChild(first);

//下一个按钮点击事件
next.addEventListener('click', function () {
    countN++;
    // countN = countN >= ul.children.length ? 0 : countN;
    if (countN >= ul.children.length) {
        countN = 1;
        ul.style.left = 0;
        animate(ul, -countN * imgWidth);

    }
    animate(ul, -countN * imgWidth);


    countC++;
    countC = countC >= ol.children.length ? 0 : countC;
    for (var j = 0; j < ol.children.length; j++) {
        ol.children[j].className = '';
    }
    ol.children[countC].className = 'orange';
})

back.addEventListener('click', function () {
    countN--;
    // countN = countN < 0 ? ul.children.length : countN;
    if (countN < 0) {
        countN = ul.children.length - 2;
        ul.style.left = -(ul.children.length - 1) * imgWidth + 'px';
        animate(ul, -countN * imgWidth);
    }
    animate(ul, -countN * imgWidth);


    countC--;
    countC = countC < 0 ? ol.children.length - 1 : countC;
    for (var j = 0; j < ol.children.length; j++) {
        ol.children[j].className = '';
    }
    ol.children[countC].className = 'orange';
})

//自动播放
var interval = setInterval(function () {
    //调用下一张按钮点击事件
    next.click();
}, 1000)

//鼠标经过清除计时器
var div = document.querySelector('div');
div.addEventListener('mouseenter', function () {
    clearInterval(interval);

})
//离开
div.addEventListener('mouseleave', function () {
    interval = setInterval(function () {
        next.click();
    }, 1000)
})




*{
    margin: 0 auto;
    padding: 0;
}
div{
    width: 400px;
    height: 200px;
    position: relative;
    overflow: hidden;
    margin-top: 100px;
}
div:hover img{
    display: block;
}
div ul{
    list-style: none;
    width: 1000%;
    position: absolute;
    top: 0;
}
div>img{
    z-index: 10;
    display: none;
}
div>img:hover{
    cursor: pointer;
}
div #back{
    position: absolute;
    left: 0;
    top: 50%;
    transform: translateY(-50%);
}
div #next{
    position: absolute;
    right: 0;
    top: 50%;
    transform: translateY(-50%);
}
div ul img{
    width: 400px;
    height: 200px;
    float: left;
}
div ol{
    list-style: none;
    position: absolute;
    bottom: 4px;
    left: 50%;
    transform: translateX(-50%);
}
div ol li{
    width: 10px;
    height: 10px;
    float: left;
    margin-left: 8px;
    border-radius: 5px;
    border: 2px solid orange;
}
div ol li:hover{
    cursor: pointer;
}
.orange{
    background-color: orange;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CoolPomelo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值