JS轮播图

<!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>
        a {
            text-decoration: none;
        }

        li {
            list-style: none;
        }

        .box {
            position: relative;
            width: 1236px;
            height: 696px;
            background-color: orange;
            margin: 20px auto;
            box-shadow: 3px 3px 8px rgba(0, 0, 0, .3);
        }

        .box .boxpic {
            position: absolute;
            width: 1236px;
            height: 696px;
            background-color: pink;
            overflow: hidden;
        }

        .box .boxpic div {
            position: absolute;
            top: 0;
            left: 0;
            width: 800%;
        }

        .box .boxpic div img {
            float: left;
            width: 1236px;
        }

        .box .boxright,
        .box .boxleft {
            position: absolute;
            width: 50px;
            height: 50px;
            background-color: rgba(255, 255, 255, .1);
            border-radius: 15%;
        }

        .box .boxright:hover,
        .box .boxleft:hover {
            background-color: rgba(255, 255, 255, .3);
        }

        .box .boxright {
            left: 50%;
            top: 50%;
            margin-left: 558px;
            margin-top: -25px;
        }

        .box .boxleft {
            top: 50%;
            margin-top: -25px;
            margin-left: 10px;
        }

        .box .boxleft img,
        .box .boxright img {
            width: 35px;
            height: 35px;
            margin: 7.5px;
        }

        .box .boxbottom {
            position: absolute;
            height: 50px;
            bottom: 0;
            left: 50%;
            margin-left: -72px;
        }

        .box .boxbottom .point {
            display: inline-block;
            width: 15px;
            height: 15px;
            background-color: rgba(255, 255, 255, .5);
            border-radius: 50%;
            margin: 17.5px 5px;
        }

        .box .boxbottom .current {
            background-color: rgba(255, 255, 255, .8);
        }

        .box .boxbottom .point:hover {
            background-color: rgba(255, 255, 255, .8);
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="boxpic">
            <div>
                <img src="images/bg1.jpg">
                <img src="images/bg2.jpg">
                <img src="images/bg3.jpg">
                <img src="images/bg4.jpg">
            </div>
        </div>
        <div class="boxshadow"></div>
        <a href="#">
            <div class="boxleft"><img src="images/箭头左.png"></div>
        </a>
        <a href="#">
            <div class="boxright"><img src="images/箭头右.png"></div>
        </a>
        <div class="boxbottom">
        </div>
    </div>


    <script>
        // animate函数 元素必须要有定位
        function animate(obj, target, callback) {
            clearInterval(obj.timer);
            obj.timer = setInterval(function () {
                if (obj.offsetLeft == target) {
                    clearInterval(obj.timer);
                    if (callback) {
                        callback();
                    }
                }
                var step = (target - obj.offsetLeft) / 10;
                step = step > 0 ? Math.ceil(step) : Math.floor(step);
                obj.style.left = obj.offsetLeft + step + 'px';
            }, 15);
        }

        //生成小圆圈
        var box = document.querySelector('.box');
        var boxpic = document.querySelector('.boxpic');
        var boxpicDiv = boxpic.querySelector('div');
        var imgs = boxpicDiv.querySelectorAll('img');
        var boxbottom = document.querySelector('.boxbottom');
        for (var i = 0; i < imgs.length; i++) {
            // 创建节点
            var a = document.createElement('a');
            // 插入节点
            boxbottom.appendChild(a);
            var div = document.createElement('div');
            a.appendChild(div);
            div.className = 'point';
            div.setAttribute('index', i);//设置自定义属性 用于获取小圆圈索引号
            //排他思想
            div.addEventListener('click', function () {
                for (var i = 0; i < boxbottom.children.length; i++) {
                    boxbottom.children[i].firstChild.className = 'point';//children用于多个孩子 firstChild、lastChild用于单个孩子
                }
                this.className = 'point current';
                //点击圆点图片滚动
                var index = this.getAttribute('index');
                //防止点击后滚动出现错误
                num = index;
                circle = index;
                animate(boxpicDiv, -(this.getAttribute('index') * imgs[0].offsetWidth));//注:第二个属性值要为负,向左滚动
            });
        }
        //设置默认第一个点
        boxbottom.children[0].firstChild.className = 'point current';

        //图片滚动:小圆圈索引号*图片宽度
        var boxleft = document.querySelector('.boxleft');
        var boxright = document.querySelector('.boxright');
        var num = 0;
        var circle = 0;
        //将第一张图片克隆
        var firstImg = boxpicDiv.children[0].cloneNode(true);
        boxpicDiv.appendChild(firstImg);
        //右侧按钮
        boxright.addEventListener('click', function () {
            if (num == boxbottom.children.length) {
                boxpicDiv.style.left = 0;
                num = 0;
            }
            num++;
            animate(boxpicDiv, -num * imgs[0].offsetWidth);
            //小圆圈随着滚动进行变化
            circle++;
            if (circle == boxbottom.children.length) {
                circle = 0;
            }
            //排他思想
            for (var i = 0; i < imgs.length; i++) {
                boxbottom.children[i].firstChild.className = 'point';
            }
            boxbottom.children[circle].firstChild.className = 'point current';
        });
        //左侧按钮
        boxleft.addEventListener('click', function () {
            if (num == 0) {
                num = boxbottom.children.length;
                boxpicDiv.style.left = -num * imgs[0].offsetWidth + 'px';
            }
            num--;
            animate(boxpicDiv, -num * imgs[0].offsetWidth);
            //小圆圈随着滚动进行变化
            circle--;
            if (circle < 0) {
                circle = boxbottom.children.length - 1;
            }
            //排他思想
            for (var i = 0; i < imgs.length; i++) {
                boxbottom.children[i].firstChild.className = 'point';
            }
            boxbottom.children[circle].firstChild.className = 'point current';
        });
        //自动播放滚动图
        var timer = setInterval(function () {
            //手动调用点击事件
            boxright.click();
        }, 2000);
        //鼠标经过 停止自动播放 鼠标离开开启自动播放
        box.addEventListener('mouseenter', function () {
            clearInterval(timer);
            console.log('经过');
        });
        box.addEventListener('mouseleave', function () {
            timer = setInterval(function () {
                //手动调用点击事件
                boxright.click();
            }, 2000);
            console.log('离开');
        });
    </script>
</body>

</html>

代码下载:js轮播图

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值