首页 > 代码库 > 用JS实现轮播

用JS实现轮播

<!doctype html>

  <html>

    <head>

      <title>  </title>

      <style type="text/css">

        #box{

            width:950px;

            height:500px;

            overflow:hidden;

            position:relative;

            left:0;

            top:0;

          }

        #box #mylist{

            position:absolute;

            left:0;

            top:0

 

             list-style-type: none;
               }
        #box ul li{
            float: left;
              }
  

        </style>

    </head>

      <body>
        <div id="box">
          <ul id="mylist">
             <li><a><img src=http://www.mamicode.com/"../images/slider-1.jpg"></a></li>
             <li><a><img src=http://www.mamicode.com/"../images/slider-2.jpg"></a></li>
             <li><a><img src=http://www.mamicode.com/"../images/slider-3.jpg"></a></li>
          </ul>
       </div>

    <script type="text/javascript" >

        var timer=setInterval("hello()",2000);
        var mylist=document.getElementById("mylist");
        var num=document.getElementById("mylist").getElementsByTagName("li");
        var now=0;
        var left=0;
        function hello(){
        // body...
        // document.write("ddd"+now);
        if (left<=-(num.length-1)*950) {
        now=0;
        left=-now*950;
        mylist.style.left=left+"px";
        now=now+1;
        }else{
        left=-now*950;
        mylist.style.left=left+"px";
        now=now+1;
        }

        }

    </script>
</body>
</html>

用JS实现轮播