老婆们

<!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;
        }
        
        #cont {
            width: 600px;
            height: 300px;
            background: royalblue;
            position: relative;
            left: 50%;
            transform: translate(-50%);
            overflow: hidden;
        }
        
        #cont ul {
            width: 600px;
            height: 200px;
            margin-top: 50px;
            position: absolute;
        }
        
        #cont ul li {
            width: 150px;
            height: 150px;
            float: left;
            list-style: none;
        }
        
        img {
            width: 150px;
            height: 150px;
        }
        
        .a {
            width: 100px;
            height: 30px;
            position: relative;
            left: 50%;
            transform: translate(-50%);
        }
    </style>
    <script>
        window.onload = function() {
            let cont = document.getElementById('cont');
            let ul = document.getElementsByTagName('ul')[0];
            let li = document.getElementsByTagName('li');
            ul.innerHTML += ul.innerHTML;
            ul.style.width = li[0].offsetWidth * li.length + 'px';

            let LEFT = document.getElementById('left');
            let RIGHT = document.getElementById('right');
            let OFF = document.getElementById('off');
            let timeL = null
            let timeR = null

            function Left() {
                if (ul.offsetLeft <= -li[0].offsetWidth * li.length / 2) {
                    ul.style.left = 0 + 'px';
                } else {
                    ul.style.left = ul.offsetLeft - 15 + 'px';
                }
            }

            function Right() {
                if (ul.offsetLeft == 0) {
                    ul.style.left = -li[0].offsetWidth * li.length / 2 + 'px'
                } else {
                    ul.style.left = ul.offsetLeft + 15 + 'px'
                }
            }


            LEFT.onclick = function() {
                clearInterval(timeR, timeL)
                timeL = setInterval(() => {
                    Left()
                }, 100);
            }

            RIGHT.onclick = function() {
                clearInterval(timeL, timeR)
                timeR = setInterval(() => {
                    Right()
                }, 100);
            }
            OFF.onclick = function() {
                clearInterval(timeR)
                clearInterval(timeL)
            }
        }
    </script>
</head>

<body>
    <div id="cont">
        <ul>
            <li>
                <img src="1.jpg">
            </li>
            <li>
                <img src="2.jpg">
            </li>
            <li>
                <img src="3.jpg">
            </li>
            <li>
                <img src="4.jpg">
            </li>
        </ul>
    </div>
    <div class="a">
        <input type="button" value="右" id="left">
        <input type="button" value="暂停" id="off">
        <input type="button" value="左" id="right">
    </div>
</body>

</html>

最后修改:2021 年 08 月 31 日 09 : 50 PM
如果觉得我的文章对你有用,请随意赞赏