jquery實現左右無縫輪播圖_jquery

來源:互聯網
上載者:User

本文執行個體為大家分享了jquery無縫輪播圖的實現代碼,供大家參考,具體內容如下

 <title>無縫輪播圖</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 class="banner">  <ul class="img">    <li><a href="#"><img src="img/1.jpg" alt="第1張圖片"></a></li>    <li><a href="#"><img src="img/2.jpg" alt="第2張圖片"></a></li>    <li><a href="#"><img src="img/3.jpg" alt="第3張圖片"></a></li>    <li><a href="#"><img src="img/4.jpg" alt="第4張圖片"></a></li>    <li><a href="#"><img src="img/5.jpg" alt="第5張圖片"></a></li>  </ul>  <ul class="num"></ul> //  <div class="btn">    <span class="prev"><</span>    <span class="next">></span>  </div>   </div> <script>   $(function(){     var i=0;    var timer=null;    for (var j = 0; j < $('.img li').length; j++) {  //建立圓點      $('.num').append('<li></li>')    }    $('.num li').first().addClass('active'); //給第一個圓點添加樣式     var firstimg=$('.img li').first().clone(); //複製第一張圖片    $('.img').append(firstimg).width($('.img li').length*($('.img img').width())); //將第一張圖片放到最後一張圖片後,設定ul的寬度為圖片張數*圖片寬度      // 下一個按鈕    $('.next').click(function(){      i++;      if (i==$('.img li').length) {        i=1; //這裡不是i=0        $('.img').css({left:0}); //保證無縫輪播,設定left值      };       $('.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');      }           })     // 上一個按鈕    $('.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');    })     //設定按鈕的顯示和隱藏    $('.banner').hover(function(){      $('.btn').show();    },function(){      $('.btn').hide();    })     //滑鼠劃入圓點    $('.num li').mouseover(function(){      var _index=$(this).index();      $('.img').stop().animate({left:-_index*600},150);      $('.num li').eq(_index).addClass('active').siblings().removeClass('active');    })     //定時器自動播放    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)     //滑鼠移入,暫停自動播放,移出,開始自動播放    $('.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>

以上就是本文的全部內容,希望對大家學習Jquery程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.