js css3 轮播图原理+鼠标移入+鼠标移出 停止 继续运细案例

<!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>

    <style>
        * {
            padding: 0;
            margin: 0;
        }
        
        #box {
            width: 1000px;
            height: 200px;
            margin: 50px auto;
            border: 1px solid #000;
            margin-top: 100px;
            background-color: slategray;
            overflow: hidden;
        }
        
        #ula {
            list-style: none;
            width: 8000px;
            position: relative;
        }
        
        #box ul li {
            float: left;
            margin-right: 10px;
        }
    </style>


    <div id="box">
        <!-- ul 也有宽度和高度 -->
        <ul id="ula">
            <li><img src="img/1.jpg" alt=""></li>
            <li><img src="img/2.jpg" alt=""></li>
            <li><img src="img/3.jpg" alt=""></li>
            <li><img src="img/4.jpg" alt=""></li>
            <li><img src="img/5.jpg" alt=""></li>
        </ul>
    </div>

    <script>
        //无缝轮播图
        var box = document.getElementById('box');
        var ula = document.getElementById('ula');

        //+5个li 列表
        ula.innerHTML += ula.innerHTML;

        //给left 标识符
        var left = 0;

        move();

        //全局变量。
        var setval;

        function move() {

            clearInterval(setval);

            setval = setInterval(function() {
                left -= 2;
                if (left <= -1100) {
                    left = 0;
                }
                //给ula ul 给他一个left 的px 尺寸。
                ula.style.left = left + 'px';
            }, 20);
        }


        box.onmouseenter = function() {
            clearInterval(setval);
        }

        box.onmouseleave = function() {
            move();
        }
    </script>


</body>

</html>

发表评论

您的电子邮箱地址不会被公开。