<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>
        *{margin: 0;padding: 0;}
        #div1 ul li {float: left;width: 178px;height: 108px;list-style: none}
        #div1 {width: 712px;height: 102px;margin: 100px auto;
            position:relative;background: red;overflow: hidden }
        #div1 ul {position: absolute;left: 0;top: 0;}
    </style>
    <script>
        window.onload=function (ev) {
            var oDiv=document.getElementById("div1");
            var oul=oDiv.getElementsByTagName("ul")[0];
            var ali=oDiv.getElementsByTagName("li");
            var speed=2;
            oul.innerHTML=oul.innerHTML+oul.innerHTML;
            oul.style.width=ali[0].offsetWidth*ali.length+"px";
            function move () {
                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+speed+"px";
                }
                var time=setInterval(move,30);
            oDiv.onmouseover=function (ev2) {
                clearInterval(time);
            };
            oDiv.onmouseout=function (ev2) {
                time=setInterval(move,30);
            };
            document.getElementsByTagName("a")[0].onclick=function () {
                speed=-2;
            };
            document.getElementsByTagName("a")[1].onclick=function () {
                speed=2;
            }
        }
    </script>

</head>
    <body>
    <a href="javascript:;">向左走</a>
    <a href="javascript:;">向右走</a>

    <div id="div1">
        <ul>
            <li><img src="images/1.jpg" alt=""></li>
            <li><img src="images/2.jpg" alt=""></li>
            <li><img src="images/3.jpg" alt=""></li>
            <li><img src="images/4.jpg" alt=""></li>

        </ul>
    </div>
    </body>

</html>

无缝滚动图片

相关文章: