JavaScript无缝连接轮播图

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        ul {
            list-style: none;

        }

        img {
            vertical-align: top;
        }

        /*取消图片底部3像素距离*/
        .box {
            width: 300px;
            height: 200px;
            margin: 100px auto;
            background-color: pink;
            border: 1px solid red;
            position: relative;
            overflow: hidden;
        }

        .box ul li {
            float: left;
        }

        .box ul {
            width: 1500px;
            position: absolute;
            left: 0;
            top: 0;
        }
    </style>
</head>
<body>
<div class="box" id="screen">
    <ul>
        <li><img src="imagess/01.jpg" alt=""/></li>
        <li><img src="imagess/02.jpg" alt=""/></li>
        <li><img src="imagess/03.jpg" alt=""/></li>
        <li><img src="imagess/04.jpg" alt=""/></li>
        <li><img src="imagess/01.jpg" alt=""/></li>
    </ul>
</div>
<script src="common.js"></script>
<script>
    //获取当前的left值
    var left = my$("screen").firstElementChild.offsetLeft;
    //定义函数当left大于1200时则移动,小于的时候则从0开始
    function f1() {
        left -= 10;
        if (left > -1200) {
            my$("screen").firstElementChild.style.left = left + "px";
        } else {
            left = 0;
            my$("screen").firstElementChild.style.left = left + "px";
        }
    }
    //创建定时器图片移动
    var timeId = setInterval(f1, 10);
    //注册鼠标进入事件,鼠标进入的时候清理定时器
    my$("screen").onmouseover = function () {
        clearInterval(timeId);
    };
    //注册鼠标离开时间,鼠标离开时创建定时器
    my$("screen").onmouseout = function () {
        timeId = setInterval(f1, 10);
    };
</script>

</body>
</html>

 

posted @ 2018-08-07 10:38  {颜逸}  阅读(479)  评论(0编辑  收藏  举报