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>
  • 相关阅读:
    解释DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci
    MySQL性能优化
    MySQL中的binlog相关命令和恢复技巧
    保障MySQL安全的14个最佳方法
    MySQL忘记root密码的解决方案
    MySQL利用binlog来恢复数据库
    MySQL命令mysqldump参数大全
    MySQL REPLACE替换输出
    MySQL -A不预读数据库信息(use dbname 更快)
    MySQL 慢查询配置
  • 原文地址:https://www.cnblogs.com/520yh/p/12869430.html
Copyright © 2011-2022 走看看