jquery實(shí)現(xiàn)左右無(wú)縫輪播圖

字號(hào):


    這篇文章主要為大家詳細(xì)介紹了基于jquery實(shí)現(xiàn)左右無(wú)縫輪播圖的具體代碼,具有一定的參考價(jià)值,感興趣的朋友可以參考一下
    本文實(shí)例為大家分享了jquery無(wú)縫輪播圖的實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下
    <title>無(wú)縫輪播圖</title>
      <style>
        *{margin: 0;padding:0; }
        ul{list-style: none;}
        .banner{width: 600px;height: 300px;border: 2px solid #ccc;margin: 100px auto;position: relative;overflow: hidden;}
        .img{position: absolute;top: 0;left: 0}
        .img li{float: left;}
        .num{position: absolute;bottom: 10px;width: 100%;text-align: center;font-size: 0;}
        .num li{width: 10px;height: 10px;background:rgba(0,0,0,0.5);display: block;border-radius: 100%;display: inline-block;margin: 0 5px;cursor: pointer;}
        .btn{display: none;}
        .btn span{display: block;width: 50px;height: 100px;background: rgba(0,0,0,0.6);color: #fff;font-size: 40px;line-height: 100px;text-align: center;cursor:pointer;}
        .btn .prev{position: absolute;left: 0;top: 50%;margin-top: -50px;}
        .btn .next{position: absolute;right: 0;top: 50%;margin-top: -50px;}
        .num .active{background-color: #fff;}
      </style>
      <script src="http://apps.bdimg.com/libs/jquery/1.8.3/jquery.min.js"></script>
    </head>
    <body>
    <div>
      <ul>
        <li><a href="#"><img src="img/1.jpg"></a></li>
        <li><a href="#"><img src="img/2.jpg"></a></li>
        <li><a href="#"><img src="img/3.jpg"></a></li>
        <li><a href="#"><img src="img/4.jpg"></a></li>
        <li><a href="#"><img src="img/5.jpg"></a></li>
      </ul>
      <ul></ul> //
      <div>
        <span><</span>
        <span>></span>
      </div>
    </div>
    <script>
      $(function(){
        var i=0;
        var timer=null;
        for (var j = 0; j < $('.img li').length; j++) {  //創(chuàng)建圓點(diǎn)
          $('.num').append('<li></li>')
        }
        $('.num li').first().addClass('active'); //給第一個(gè)圓點(diǎn)添加樣式
        var firstimg=$('.img li').first().clone(); //復(fù)制第一張圖片
        $('.img').append(firstimg).width($('.img li').length*($('.img img').width())); //將第一張圖片放到最后一張圖片后,設(shè)置ul的寬度為圖片張數(shù)*圖片寬度
        // 下一個(gè)按鈕
        $('.next').click(function(){
          i++;
          if (i==$('.img li').length) {
            i=1; //這里不是i=0
            $('.img').css({left:0}); //保證無(wú)縫輪播,設(shè)置left值
          };
          $('.img').stop().animate({left:-i*600},300);
          if (i==$('.img li').length-1) {   //設(shè)置小圓點(diǎn)指示
            $('.num li').eq(0).addClass('active').siblings().removeClass('active');
          }else{
            $('.num li').eq(i).addClass('active').siblings().removeClass('active');
          }
        })
        // 上一個(gè)按鈕
        $('.prev').click(function(){
          i--;
          if (i==-1) {
            i=$('.img li').length-2;
            $('.img').css({left:-($('.img li').length-1)*600});
          }
          $('.img').stop().animate({left:-i*600},300);
          $('.num li').eq(i).addClass('active').siblings().removeClass('active');
        })
        //設(shè)置按鈕的顯示和隱藏
        $('.banner').hover(function(){
          $('.btn').show();
        },function(){
          $('.btn').hide();
        })
        //鼠標(biāo)劃入圓點(diǎn)
        $('.num li').mouseover(function(){
          var _index=$(this).index();
          $('.img').stop().animate({left:-_index*600},150);
          $('.num li').eq(_index).addClass('active').siblings().removeClass('active');
        })
        //定時(shí)器自動(dòng)播放
        timer=setInterval(function(){
          i++;
          if (i==$('.img li').length) {
            i=1;
            $('.img').css({left:0});
          };
          $('.img').stop().animate({left:-i*600},300);
          if (i==$('.img li').length-1) { 
            $('.num li').eq(0).addClass('active').siblings().removeClass('active');
          }else{
            $('.num li').eq(i).addClass('active').siblings().removeClass('active');
          }
        },1000)
        //鼠標(biāo)移入,暫停自動(dòng)播放,移出,開(kāi)始自動(dòng)播放
        $('.banner').hover(function(){ 
          clearInterval(timer);
        },function(){
          timer=setInterval(function(){
          i++;
          if (i==$('.img li').length) {
            i=1;
            $('.img').css({left:0});
          };
          $('.img').stop().animate({left:-i*600},300);
          if (i==$('.img li').length-1) { 
            $('.num li').eq(0).addClass('active').siblings().removeClass('active');
          }else{
            $('.num li').eq(i).addClass('active').siblings().removeClass('active');
          }
        },1000)
        })
      })
    </script>
    以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)Jquery程序設(shè)計(jì)有所幫助。