js编写无缝轮播图效果

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
         *{margin:0;padding:0;}
         .banner{width:1000px;height:350px;margin:50px auto;}
         #list img{width:100%;height:100%;}
         #list li {list-style: none;width:1000px;height:350px;float: left;}
         li .active{z-index: 1;}
         
         #span{width:400px;height:40px;margin:0 auto;}
         #span span{width:40px;height:40px;background:#f99;float:left;display: block;
                   border-radius: 50%;margin-left:30px;}
          #span .active{background:red;}
</style>
</head>
<body>  
       <button id="left">上一张</button>
       <button id="right">下一张</button>
  <div class="banner">
              <ul id="list">
                     <li class="active"><img src="http://img.zcool.cn/community/focus/8b1858f629f100000148f059c79d.jpg" alt=""></li>
                     <li><img src="http://img.zcool.cn/community/focus/dff158f6299000000148f0dd05b7.jpg" alt=""></li>
                     <li><img src="http://img.zcool.cn/community/focus/7a3e58f6c39f00000148f06a7b92.jpg" alt=""></li>
                     <li><img src="http://img.zcool.cn/community/focus/dff158f6299000000148f0dd05b7.jpg" alt=""></li>
                     <li><img src="http://img.zcool.cn/community/focus/995558f62a0900000148f0a9ac3f.jpg" alt=""></li>
                     <li><img src="http://img.zcool.cn/community/focus/8b1858f629f100000148f059c79d.jpg" alt=""></li>
              </ul>
  </div>
  <div id="span">
            <span class="active"></span>
            <span></span>
            <span></span>
            <span></span>
            <span></span>
  </div>
</body>
<script src="js/move.js"></script>
<script>
      function $(select){
      return document.querySelector(select);
      }
        var oList = $("#list");
        var aLi = oList.children ;
        var oSpan = $("#span").children;
        var oLeft = $("#left");
        var oRight = $("#right");
        var index = 0 ;
        oList.style.width  = aLi[0].offsetWidth * aLi.length + "px" ;
        oRight.onclick = function(){  //点击右键切换下一张图
        if(index==aLi.length-1){
        index = 1 ;
        oList.style.marginLeft = 0 ;
        }else{       
        index++;
        }
        movep();

        }

        oLeft.onclick = function(){  //点击左键切换上一张图
        if(index==0){
        index = aLi.length-2 ;
        oList.style.marginLeft = -(aLi.length-1)*aLi[0].offsetWidth +"px";
        }else{       
        index--;
        }
        movep();
        }
        function movep(){   //切图控制函数
        move(oList,{marginLeft:-index*aLi[0].offsetWidth});
        for(var i = 0 ; i < oSpan.length ; i++){
        oSpan[i].className = "" ;
        }
        if(index == aLi.length-1){
        oSpan[0].className = "active" ;
        }else{
        oSpan[index].className = "active" ;
        }
        }
        for(var i = 0 ; i < oSpan.length ; i++){
        oSpan[i].onclick = (function(n){
        return function(){
        index = n ;
        movep();
        }
        })(i)

        }

          var timer = null ;

          timer = setInterval(oLeft.onclick,2000);

           for(var j = 0 ; j < aLi.length ; j++){  //循环内控制鼠标划过按钮体质自动按轮播
            oLeft.onmouseover = oRight.onmouseover = oSpan[j].onmouseover = function(){
              clearInterval(timer);
            }
            oLeft.onmouseout = oRight.onmouseout = oSpan[j].onmouseout = function(){
              timer = setInterval(oLeft.onclick,2000);
            }
           }     
</script>
</html>
    
         
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值