js轮播图实现

之前一直用swiper.js插件写轮播图效果不错,想着自己也写一个练练手

运行

在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>轮播</title>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
        text-decoration: none;
        list-style-type: none;
    }

    .wrapper {
        position: relative;
        width: 600px;
        height: 350px;
        margin: 80px auto;
        overflow: hidden;
    }

    .arrow-1, .arrow-2 {
        position: absolute;
        top: 50%;
        width: 20px;
        height: 20px;
        margin-top: -20px;
        text-align: center;
        line-height: 20px;
        font-size: 12px;
        color: #fff;
        z-index: 1;
        background-color: rgba(0, 0, 0, .3);
    }

    .arrow-1 {
        left: 0;
        border-radius: 0 10px 10px 0;
    }

    .arrow-2 {
        right: 0;
        border-radius: 10px 0 0 10px;
    }

    .circle {
        position: absolute;
        bottom: 15px;
        left: 50%;
        width: 80px;
        height: 14px;
        margin-left: -40px;
        border-radius: 7px;
        background: #CCCCFF;
    }

    .circle li {
        float: left;
        width: 10px;
        height: 10px;
        margin: 2px 5px;
        border-radius: 50%;
        background-color: #fff;

    }

    .circle .current {
        background: cornflowerblue;
    }

    .arrow {
        width: 600%;
        transition: all .8s;
        transform: translateX(-600px);
        overflow: hidden;
    }

    .arrow li {
        float: left;
    }

    .arrow img {
        width: 600px;
        height: 350px;
    }


</style>
<body>
<div class="wrapper">
    <!--左箭头-->
    <a href="javascript: ;" class="arrow-1">&lt;</a>
    <!--右箭头-->
    <a href="javascript: ;" class="arrow-2">&gt;</a>
    <ul class="arrow">
        <li><img src="img/6.jpg"/></li>
        <li><img src="img/7.jpg"/></li>
        <li><img src="img/8.jpg"/></li>
        <li><img src="img/9.jpg"/></li>
    </ul>
    <div class="circle">
        <ul>
            <li class="current"></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div>
</div>

</body>

<script>

    var arrow = document.querySelector('.arrow');
    var circle = document.querySelector('.circle ul');
    var arrow1 = document.querySelector('.arrow-1');
    var arrow2 = document.querySelector('.arrow-2');
    var lis = arrow.children;
    var wrapper = document.querySelector('.wrapper');
    var clis = circle.children;

    //无缝轮播 深度克隆第一张和最后一张图片
    arrow.appendChild(lis[0].cloneNode(true));
    arrow.insertBefore(lis[3].cloneNode(true), lis[0]);

    //给每个li注入索引号
    for (var i = 0; i < lis.length; i++) {
        lis[i].setAttribute("index", i);
    }

    var index = 1;

    //向右滑动函数
    function move_r() {
        clis[index_j(index)].classList.remove('current');
        index++;
        clis[index_j(index)].classList.add('current');
        var w = -index * 600 + 'px';
        arrow.style.transition = 'all .8s';
        arrow.style.transform = 'translateX(' + w + ')';
        arrow.addEventListener('transitionend', function () {
            if (index >= 5) {
                index = 1;
                arrow.style.transition = "none";
                arrow.style.transform = 'translateX(-600px)';
            }
        });

    }

    //下面小原点定位
    function index_j(index) {
        if (index === 0) {
            return 3;
        }
        else if (index === 5) {
            return 0;
        } else {
            return index - 1;
        }
    }

    //向左滑动
    function move_l() {
        clis[index_j(index)].classList.remove('current');
        index--;
        clis[index_j(index)].classList.add('current');
        var w = -index * 600 + 'px';
        arrow.style.transition = 'all .8s';
        arrow.style.transform = 'translateX(' + w + ')';
        arrow.addEventListener('transitionend', function () {
            if (index <= 0) {
                index = 4;
                arrow.style.transition = "none";
                arrow.style.transform = 'translateX(-2400px)';
            }
        });

    }

    //定时器 自动向右滑动
    var timer = setInterval(move_r, 3000);

    //鼠标滑过清除定时器
    wrapper.addEventListener('mouseenter', function () {
        clearInterval(timer);
        timer = null;
    });

    //鼠标移除添加定时器
    wrapper.addEventListener('mouseleave', function () {
        timer = setInterval(move_r, 3000);
    })


    //左右滚动点击事件
    arrow2.addEventListener('click', function () {
        move_r();
    })

    arrow1.addEventListener('click', function () {
        move_l();
    })

    //下面小原点点击事件
    var lastone =0;

    //高级排他思想
    for (var i = 0; i < clis.length; i++) {
        (function (i) {

            clis[i].addEventListener('click', function () {
                var w = -(i+1)*600+'px';
                arrow.style.transform = 'translateX(' + w + ')';
                clis[lastone].classList.remove('current');
                clis[i].classList.add('current');
                lastone = i;
            })

        })(i);
    };


</script>


</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值