老师麻烦帮忙看看这样写可以吗?有没有什么可以优化的

来源:6-3 编写跑马灯轮播图特效

一天吃五顿

2022-09-12 17:00:18

<!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>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            position: relative;
            width: 500px;
            height: 300px;
            /* background-color: red; */
            margin: 50px auto;
            overflow: hidden;
        }

        .box ul {
            width: 3000px;
            height: 300px;
            list-style: none;
            position: absolute;
            transition: all .5s ease 0s;
            left: 0;
        }

        .box ul li {
            float: left;
            width: 500px;
            height: 300px;
            background-color: red;
            color: #fff;
            font-size: 40px;
            text-align: center;
            line-height: 300px;
        }

        .g {
            position: absolute;
            top: 50%;
            margin-top: -20px;
            display: block;
            width: 40px;
            height: 40px;
            border-radius: 20px;
            background-color: green;
        }

        .left {
            left: 10px;
        }

        .right {
            right: 10px;
        }
    </style>
</head>

<body>
    <div class="box">
        <ul>
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
        </ul>
        <a href="javascript:;" class="left g"></a>
        <a href="javascript:;" class="right g"></a>
    </div>
    <script>
        //获取元素
        var left = document.querySelector('.left');
        var right = document.querySelector('.right');
        var list = document.querySelector('ul');
        var li_id = 0;
        //克隆第一个小li到最后面;
        var li = list.firstElementChild.cloneNode(true);
        list.appendChild(li);
        //节流阀
        var jl = true;
        //给右按钮绑定事件
        right.addEventListener('click', function () {
            if (!jl) return;
            li_id++;
            list.style.transition = 'all .5s ease 0s'
            list.style.left = -li_id * 500 + 'px';
            if (li_id > 4) {
                setTimeout(function () {
                    list.style.left = 0;
                    li_id = 0;
                    list.style.transition = 'none';
                }, 500);
            }
            //关闭节流阀
            jl = false;
            //500毫秒后打开节流阀
            setTimeout(function () {
                jl = true;
            }, 500)
        })
        //给左按钮添加事件
        left.addEventListener('click', function () {
            if (!jl) return;
            li_id--;
            list.style.transition = 'all .5s ease 0s'
            list.style.left = -li_id * 500 + 'px';
            // if (li_id < 1) {
            //     setTimeout(function () {
            //         li_id = 5;
            //         list.style.left = -li_id * 500 + 'px';
            //         list.style.transition = 'none';
            //     }, 500)
            // }
            //关闭节流阀
            jl = false;
            //500毫秒后开启节流阀
            setTimeout(function () {
                jl = true;
            }, 500)
        })
        //增加一个鼠标按下事件,防止第一次加载直接点击左按钮会出现短暂空白;
        left.addEventListener('mousedown', function () {
            if (li_id < 1) {
                li_id = 5;
                list.style.left = -li_id * 500 + 'px';
                list.style.transition = 'none';
            }
        })
    </script>
</body>

</html>

下载视频          

下载视频          

写回答

1回答

好帮手慕星星

2022-09-13

同学你好,代码实现效果很棒。不需要优化了,祝学习愉快!

0

前端工程师

前端入门如同写字,如果你不知道从哪开始,那就选择前端(含Vue3.x,React17,TS)

20327 学习 · 17877 问题

查看课程