zoukankan      html  css  js  c++  java
  • js实现向左向右无缝轮动

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            #div1 {
                 750px;
                height: 60px;
                margin: 10px auto;
                position: relative;
                background: #cccccc;
                overflow: hidden;
            }
    
            #div1 ul {
                position: absolute;
                top: 0;
                left: 0;
            }
    
            #div1 ul li {
                float: left;
                 200px;
                height: 50px;
                margin-left: 5px;
                list-style: none;
            }
    
            #Right {
                 50px;
                height: 50px;
                background: blue;
            }
    
            #Left {
                 50px;
                height: 50px;
                background: brown;
            }
        </style>
        <script>
            window.onload = function () {
                var oDiv = document.getElementById('div1');//获取div
                var oUl = document.getElementsByTagName('ul')[0];//获取ul
                var aLi = document.getElementsByTagName('li');//获取li
                var speat = 2//设置轮动的变量
                oUl.innerHTML = oUl.innerHTML + oUl.innerHTML;
                oUl.style.width = aLi[0].offsetWidth * aLi.length + 'px'//ul的宽度
                function timer() {
                    //向左轮动的判断
                    if (oUl.offsetLeft < -oUl.offsetWidth / 2) {
                        oUl.style.left = '0';
                    }
                    //向右轮动的判断
                    if (oUl.offsetLeft > 0) {
                        oUl.style.left = -oUl.offsetWidth / 2 + 'px'
                    }
                    oUl.style.left = oUl.offsetLeft + speat + 'px'
                };
                var time = setInterval(timer, 30);
                //鼠标移入停止计时器
                oDiv.onmouseover = function () {
                    clearInterval(time)
                };
                //鼠标移出开始计时器
                oDiv.onmouseout = function () {
                    time = setInterval(timer, 30);
                };
                //点击向左轮动
                document.getElementById('Left').onclick = function () {
                    speat = -2
                }
                //点击向右轮动
                document.getElementById('Right').onclick = function () {
                    speat = 2
                }
            }
        </script>
    </head>
    
    <body>
        <div id="Left"></div>
        <div id="Right"></div>
        <div id="div1">
            <ul>
                <li style="background: red;"></li>
                <li style="background: rebeccapurple;"></li>
                <li style="background: royalblue;"></li>
                <li style="background: salmon;"></li>
            </ul>
        </div>
    
    </body>
    
    </html>
  • 相关阅读:
    四套读写方案
    如何保证ArrayList线程安全
    异常总结<经典例题>
    java.移位运算符
    java反射机制
    面试题:return和finally执行
    Spring_通过注解配置 Bean(1)
    Spring_通过 FactoryBean 配置 Bean
    Spring_通过工厂方法配置 Bean
    Spring_管理 Bean 的生命周期
  • 原文地址:https://www.cnblogs.com/520yh/p/12869430.html
Copyright © 2011-2022 走看看