js手写轮播

html部分

<!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>
</head>
<body>
    <div id="outer">
        <ul id="imgList">
            <li>
                <img src="img/1.webp" alt="" srcset="">
            </li>
            <li>
                <img src="img/2.webp" alt="" srcset="">
            </li>
            <li>
                <img src="img/3.webp" alt="" srcset="">
            </li>
            <li>
                <img src="img/4.webp" alt="" srcset="">
            </li>
            <li>
                <img src="img/5.webp" alt="" srcset="">
            </li>
            <li>
                <img src="img/1.webp" alt="" srcset="">
            </li>

        </ul>

        <div id="navDiv">
            <a href="javascript:;"></a>
            <a href="javascript:;"></a>
            <a href="javascript:;"></a>
            <a href="javascript:;"></a>
            <a href="javascript:;"></a>
        </div>
    </div>

</body>
</html>

css部分

<style>
        *{
            margin: 0;
            padding: 0;
        }

        #outer {
            width: 520px;
            height: 333px;
            margin: 50px auto;
            background-color: aquamarine;
            padding: 10px 0;
            position: relative;
            overflow: hidden;
        }

        #imgList {
            list-style: none;
            width: 2600px;
            position: absolute;
            left: 0;
        }

        #imgList li {
            float: left;
            margin: 0 10px;
            /* width: 500px;
            height: 333px; */
        }

        img {
            width: 500px;
            height: 333px;
        }

        #navDiv {
            position: absolute;
            bottom: 15px;
            /* (520-25*5)/2 */
            left: 197px

        }

        #navDiv a:hover {
            background-color: darkorchid;
        }

        #navDiv a {
            float: left;
            background-color: black;
            width: 15px;
            height: 15px;
            margin: 0 5px;
            opacity: 0.5;
            filter: alpha(opacity=50);
        }

    </style>

js部分

<script>
        function getStyle(obj, name) {
            if(window.getComputedStyle) {
                return getComputedStyle(obj, null)[name];
            } else {
                return obj.currentStyle[name];
            }
        }

        // var timer;

        // obj要执行动画的对象  speed移动的速度() target目标 attr要执行动画的样式 
        // callback:回调函数,这个函数将会在动画执行完毕以后执行
        function move(obj, attr, target, speed, callback) {
            clearInterval(obj.timer)
            var current =  parseInt(getStyle(obj, attr));
            if(current > target) {
                speed = -speed
            }

            // 向执行动画的对象种添加一个timer属性,用来保存它自己的定时器标识

            obj.timer=setInterval(function() {
                var oldValue =  parseInt(getStyle(obj, attr));
                console.log(oldValue)
                var newValue = oldValue + speed;
                console.log(newValue)

                // 向左移动时,需要判断newValue是否小于target
                // 向右移动时,需要判断newValue是否大于target
                if((speed<0 && newValue < target) || (speed >0 && newValue > target)) {
                    newValue = target
                }
                
                obj.style[attr] = newValue + 'px';

                if(newValue === target) {
                    clearInterval(obj.timer)
                    callback && callback()
                }
                
            },30)

        }
        window.onload=function() {
            var imgList = document.getElementById("imgList");

            var imgArr = document.getElementsByTagName('img');
        
            imgList.style.width = 520*imgArr.length+'px';

            // 设置导航按钮居中
            var navDiv = document.getElementById('navDiv');
            var outer = document.getElementById('outer');

            navDiv.style.left = (outer.offsetWidth - navDiv.offsetWidth)/2+'px';

            // 设置显示图片的索引
            var index = 0;

            var allA = document.getElementsByTagName('a');

            // 设置默认选中的效果
            allA[index].style.backgroundColor = 'darkorchid';

            for(var i=0;i<allA.length;i++) {

                // 为每一个超链接都添加一个num属性
                allA[i].num = i;


                allA[i].onclick = function() {
                    // 关闭自动切换的定时器
                    clearInterval(timer);
                    console.log(this)
                    console.log(this.num)
                    index = this.num;

                    // 切换图片
                    // imgList.style.left = -520*index + 'px';

                    move(imgList, "left", -520*index, 50, function() {
                        console.log('move');
                        autoChange();
                    })

                    setA();
                }
            }

            // 自动切换图片
            autoChange()

            function setA() {
                // 判断当前索引是否是最后一张图片
                if(index >= imgArr.length-1) {
                    // 则将index设置为0
                    index = 0;
                    // 此时显示的是最后一张,而最后一张图片和第一张是一摸一样
                    // 通过css将最后一张切换成第一张
                    imgList.style.left = 0;
                }

                for(var i=0; i<allA.length;i++) {
                    allA[i].style.backgroundColor = '';
                    console.log(allA[i].style);
                }
                allA[index].style.backgroundColor = 'darkorchid';

            }

            var timer;

            function autoChange() {
                timer = setInterval(function() {
                    // 使索引自增
                    index++;
                    // 判断index值
                    index %=imgArr.length;
                    move(imgList, 'left', -520*index,50,function() {
                        // 修改导航点
                        setA();
                    })

                },1500)
            }

        }
    </script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值