基於jQuery實現左右圖片輪播(原理通用),

來源:互聯網
上載者:User

基於jQuery實現左右圖片輪播(原理通用),

本文為大家分享了jQuery實現左右圖片輪播代碼,供大家參考,具體實現內容如下

運行:

重點!!!

實現原理:

通過判斷index值的大小變化來判斷圖片左移還是右移。通過控製圖片的left值,來達到一個輪播的效果。

具體代碼:

<!DOCTYPE html><html>  <head>    <meta charset="utf-8">    <title></title>  </head>  <script src="js/jquery.min.js"></script>  <style>    .banner{      margin:0 auto;      border: 4px dashed black;      width:400px;      height:200px;      position: relative;      overflow:hidden;    }    .banner a{      z-index: 100;      display: block;      width:100%;      height: 100%;      position: absolute;      left:100%;      top:0;    }    .banner .first{      left:0;    }    .banner a img{      width:100%;      height: 100%;    }    .choose{      z-index: 1000;      position: absolute;      left:150px;      top:180px;      width:100px;      height: 10px;    }    .choose span{      margin-right: 15px;      float: left;      display:block;      background: blue;      width:10px;      height: 10px;      border-radius: 10px;    }    .choose span:hover{      background: red;    }    .choose .red{      background: red;    }    .banner .pre,.next{      cursor:pointer;      text-align:center;      border-radius:20px;      display:block;      background:#cccccc;      opacity:0.4;      text-decoration: none;      z-index: 200;      display:block;      width:40px;      height: 40px;      font-size: 40px;      color:red;      position: absolute;      top:80px;    }    .banner .pre{      left:0px    }    .banner .next{      right: 0px;    }  </style>  <body>        <div class="banner">      <!--        這裡為上一頁下一頁點擊按鈕      -->      <span class="pre">-</span>      <span class="next">+</span>      <!--        此處為輪播主體,顏色塊代替。圖片自加      -->      <a href="###" class="first" style="background: pink;"></a>      <a href="###" style="background: blue;"><img src="images/banner1.jpg"/></a>      <a href="###" style="background: greenyellow;"><img src="images/banner2.jpg"/></a>      <a href="###" style="background: deepskyblue;"><img src="images/banner3.jpg"/></a>      <!--        此處為輪播部分下方小點選擇      -->      <div class="choose">        <span class="red"></span>        <span></span>        <span></span>        <span></span>      </div>    </div>        <script>      /*定義兩個變數,儲存當前頁碼和上一頁頁碼*/      var $index=0;      var $exdex=0;      /*小點的滑鼠移入事件,觸發圖片左移還是右移*/      $(".choose span").mouseover(function(){        //擷取當前移入的index值        $index=$(this).index();            //首先讓點的顏色變化,表示選中        $(".choose span").eq($index).addClass("red").siblings().            removeClass("red");        //判斷如果index變小,證明圖片要往左移動。變大則為右移        if($index>$exdex){          next();        }else if($index<$exdex){          pre();        }        //移動完畢將當前index值替換了前頁index        return $exdex=$index;      });      //下一頁的點擊事件。在右移基礎上加了最大index判斷      $(".next").click(function(){        $index++;        if($index>3){          $index=0        }        $(".choose span").eq($index).addClass("red").siblings().            removeClass("red");        next();        return $exdex=$index;      });      //上一頁的點擊事件      $(".pre").click(function(){        $index--;        if($index<0){          $index=3        };        $(".choose span").eq($index).addClass("red").siblings().          removeClass("red");        pre();        return $exdex=$index;      });      //加個定時器,正常輪播      var atime=setInterval(function(){        $(".next").click();            },1000);      //這裡為右移和左移的事件函數。      //右移基本原理就是先讓exdex定位的left左移百分百,而選中的當前頁從螢幕右邊移入,left變為0      function next(){        $(".banner a").eq($index).stop(true,true).            css("left","100%").animate({"left":"0"});        $(".banner a").eq($exdex).stop(true,true).            css("left","0").animate({"left":"-100%"});      }      function pre(){        $(".banner a").eq($index).stop(true,true).          css("left","-100%").animate({"left":"0"});        $(".banner a").eq($exdex).stop(true,true).          css("left","0").animate({"left":"100%"});      }    </script>  </body></html>

以上就是本文的全部內容,協助大家實現一個簡單大方的圖片輪播效果。

您可能感興趣的文章:
  • 基於JQuery的實現圖片輪播效果(焦點圖)
  • 基於jquery的圖片輪播 tab切換組件
  • 12款經典的白富美型—jquery圖片輪播外掛程式—前端開發必備
  • jQuery圖片輪播的具體實現
  • 原生js和jquery實現圖片輪播特效
  • 原生js和jquery實現圖片輪播淡入淡出效果
  • jQuery實現圖片輪播特效代碼分享
  • jQuery圖片輪播滾動切換代碼分享
  • 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.