无缝轮播的实现及思路

7 篇文章 0 订阅
1 篇文章 0 订阅

不多说了 直接上代码 ,图片需要自己添加

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;

        }


        i {
            display: inline-block;
            height: 40px;
            width: 40px;

            text-align: center;
            line-height: 40px;

            font-size: 30px;
            background: #000;
            cursor: pointer;
            line-height: 40px;
            text-align: center;
            font-weight: bold;
            font-size: 30px;
            color: #fff;
            opacity: 0.3;
            border: 1px solid red;
        }

        .wraper {
            width: 730px;
            height: 454px;
            margin: 0 auto;
            padding: 5px;
            border: 1px solid black;
            position: relative;
        }

        .box {
            width: 730px;
            height: 454px;
            position: relative;
            overflow: hidden;

        }

        ul {
            display: inline-block;
            width: 1000%;
            position: absolute;
            top: 0;
            left: 0;

        }

        ul li {
            float: left;
            list-style: none;
        }

        .square {
            position: absolute;
            right: 10px;
            bottom: 10px;
        }

        span {
            display: inline-block;
            width: 16px;
            height: 16px;
            background-color: #fff;
            text-align: center;
            line-height: 16px;
            cursor: pointer;
        }

        .current {
            background-color: orangered;
            color: #fff;
        }

        #focusD {
            display: none;
        }
    </style>
</head>

<body>
    <section>


        <div class="wraper">
            <i id="left" style=" position: absolute;left: -43px;top: 50%;">&lt;</i>
            <i id="right" style=" position: absolute; right: -43px;top: 50%;">&gt;</i>
            <div class="box">

                <ul>
                    <li><a href="#"><img src="./images/1.jpg" alt=""></a></li>
                    <li><a href="#"><img src="./images/2.jpg" alt=""></a></li>
                    <li><a href="#"><img src="./images/3.jpg" alt=""></a></li>
                    <li><a href="#"><img src="./images/4.jpg" alt=""></a></li>
                    <li><a href="#"><img src="./images/5.jpg" alt=""></a></li>
                    <li><a href="#"><img src="./images/6.jpg" alt=""></a></li>
                </ul>

                <div class="square">
                    <span class="current">1</span>
                    <span>2</span>
                    <span>3</span>
                    <span>4</span>
                    <span>5</span>
                    <span>6</span>

                </div>

            </div>

        </div>
    </section>
    <script>
        var spans = document.querySelectorAll(".square>span")

        var ul = document.getElementsByTagName("ul")[0]
        //获取相框
        var box = document.getElementsByClassName("box")[0];
        //获取相框的宽度
        var boxWidth = box.offsetWidth;

        //为每一个span注册一个鼠标进入事件
        for (i = 0; i < spans.length; i++) {



            spans[i].setAttribute("index", i)
            spans[i].onmousemove = function () {
                for (j = 0; j < spans.length; j++) {
                    spans[j].className = ""
                }

                this.className = "current"
                var index = this.getAttribute("index")
                animate(ul, -boxWidth * index)
            }
        }

        //设置焦点轮播
        var left = document.getElementById("left");
        var right = document.getElementById("right");
        //分别注册点击事件
        var count = 0;  //计数器
        right.onclick = function () {
            count++
            if (count > 5) {
                count = 0;
                animate(ul, -boxWidth * count)//调用下面封装好的函数

            }
            for (j = 0; j < spans.length; j++) {
                spans[j].className = ""
            }

            spans[count].className = "current"

            animate(ul, -boxWidth * count)//调用下面封装好的函数

        }



        var res = spans.length;  //计数器
        left.onclick = function () {
            res--
            if (res < 0) {
                res = spans.length;


            }
            for (j = 0; j < spans.length; j++) {
                spans[j].className = ""
            }

            spans[res].className = "current"

            animate(ul, -boxWidth * res)//这里调用下面的函数
        }




        //设置任意的一个元素,移动到指定的目标位置
        function animate(element, target) {
            clearInterval(element.timeId);
            //定时器的id值存储到对象的一个属性中
            element.timeId = setInterval(function () {
                //获取元素的当前的位置,数字类型
                var current = element.offsetLeft;
                //每次移动的距离
                var step = 10;
                step = current < target ? step : -step;
                //当前移动到位置
                current += step;
                if (Math.abs(current - target) > Math.abs(step)) {
                    element.style.left = current + "px";
                } else {
                    //清理定时器
                    clearInterval(element.timeId);
                    //直接到达目标
                    element.style.left = target + "px";
                }
            }, 20);
        }

    </script>
</body>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值