輪播

一、輪播的實現原理以及抽象出哪些函數(or接口)供使用?

  • 輪播的實現原理
  • 把需要展示的圖片放到同一行。
  • 創建一個可視化窗口,窗口之外的區域隱藏。
  • 在第一張圖片的前面克隆最后一個圖片元素,在最后一個圖片后面克隆第一個元素。
  • 利用jQuery動畫以及定位就能實現無限循環的輪播。
  • 抽象出來的函數
  • play(playPre()、playNext())
  • setBullet()(點擊下面的小橫線或者圖片的縮略圖進行播放)

二、實現左右滾動無限循環輪播效果。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>無限輪播</title>
  <style>
    *{
      margin: 0;
      padding: 0;
    }
    li{
      list-style: none;
    }
    a{
      text-decoration: none;
    }
    .clearfix:after{
      content: '';
      display: block;
      clear: both;
    }
    .rotation{
      position: relative;
      width: 600px;
      height: 350px;
      overflow: hidden;
      margin: 30px auto;
      text-align: center;
    }
    .rotation .img-ct{
      position: absolute;
      /*width: 2400px;*/
      /*寬度用js動態來寫,不寫死。圖片排成一行*/
      height: 350px;
    }
    .rotation .img-ct>li{
      float: left;
    }
    .rotation img{
      width: 600px;
      height: 350px;
    }
    .rotation .btn{
      position: absolute;
      top: 50%;
      transform: translate(-50%);
      border-radius: 50%;
      display: inline-block;
      width: 40px;
      height: 40px;
      color: #fff;
      font-size: 25px;
      line-height: 40px;
      background: #4E443C;
      opacity: 0.7;
    }
    .rotation .btn:hover{
      opacity: .4;
    }
    .rotation .btn-pre{
      left: 40px;
    }
    .rotation .btn-next{
      right: 0px;
    }
    .rotation .slide{
      position: absolute;
      left: 50%;
      transform: translate(-50%);
      bottom: 10px;
      text-align: center;
    }
    .rotation .slide>li{
      height: 5px;
      width: 20px;
      background: #fff;
      display: inline-block;
      border-radius: 5px;
      cursor: pointer;
    }
    .rotation .slide .active{
      background: #333;
      opacity: .7;
    }
  </style>
</head>
<body>
  <div class="rotation">
    <ul class="img-ct clearfix">
      <li key="0"><a href="#">![](http://upload-images.jianshu.io/upload_images/2244513-a92235746683a588.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a></li>
      <li key="1"><a href="#">![](http://upload-images.jianshu.io/upload_images/2244513-35e2fa735b281270.JPG?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a></li>
      <li key="2"><a href="#">![](http://upload-images.jianshu.io/upload_images/2244513-d365633c19c63548.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a></li>
      <li key="3"><a href="#">![](http://upload-images.jianshu.io/upload_images/2244513-fd192a1a394f80f4.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)</a></li>
    </ul>
    <a class="btn btn-pre" href="#"><</a>
    <a class="btn btn-next" href="#">></a>
    <ul class="slide">
      <li class="active"></li>
      <li></li>
      <li></li>
      <li></li>
    </ul>
  </div>

  <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
  <script>
    var $imgCt = $('.rotation .img-ct'),
        $slide = $('.slide'),
        $btnPre = $('.rotation .btn-pre'),
        $btnNext = $('.rotation .btn-next');
    var imgLen = $imgCt.children().length;  // 4
    var key = 0;
    var isAnimate = false;

    var $firstImg = $imgCt.find('li').first(),
        $lastImg = $imgCt.find('li').last();

    $imgCt.append($firstImg.clone());
    $imgCt.prepend($lastImg.clone());

    $imgCt.width((imgLen+2)*$firstImg.width());
    $imgCt.css('left', 0-$firstImg.width());

    // 點擊左右圖標輪播
    $btnPre.on('click', function(e) {
      e.preventDefault();
      playPre();
      // console.log(key);
    })
    $btnNext.on('click', function(e) {
      e.preventDefault();
      playNext();
      // console.log(key);
    })

    // 點擊下面的小橫線輪播
    $slide.find('li').on('click', function(e) {
      e.preventDefault();
      var clickIndex = $(this).index();
      if(clickIndex < key) {
        playPre(clickIndex);
      }else if(clickIndex > key){
        playNext(clickIndex);
      }else{
        return;
      }
    })

    function playPre(index) {
      if(isAnimate) {
        return;
      }
      isAnimate = true;
      switch (typeof index)
      {
        case 'undefined':
          $imgCt.animate({
            left: '+=600px', // 別寫成'+= 600px'
          }, function() {
            key--;
            if(key < 0) {
              $imgCt.css('left', 0-(imgLen*$firstImg.width()));
              key = imgLen -1;
            }
            setSlide();
            isAnimate = false;
          })
          break;
        case 'number':
          clickSlide(index);
          break;
      }
    }

    function playNext(index) {
      if(isAnimate) {
        return;
      }
      isAnimate = true;
      switch (typeof index)
      {
        case 'undefined':
          $imgCt.animate({
            left: '-=600px',
          }, function() {
            key ++;
            if(key == imgLen) {
              $imgCt.css('left', '-600px');
              key = 0;
            }
            setSlide();
            isAnimate = false;
          })
          break;
        case 'number':
          clickSlide(index);
          break;
      }

      // setSlide();  動畫是異步的,放在這里執行的時候,key還沒有改變
      // isAnimate = false;  動畫是異步的!!!!!等動畫完成之后再置為false
    }

    function clickSlide(index) {
      var leftOffset= -(index+1)*$firstImg.width();
      $imgCt.animate({
        'left': leftOffset,
      }, function() {
        key = index;
        setSlide();
        isAnimate = false;
      })
    }
    function setSlide() {
      $('.rotation .slide').find('li').removeClass('active').eq(key).addClass('active');
      console.log('key:' + key);
    }

    // 代碼寫的不是很美,但是基本功能都實現了,有待改善。
  </script>
</body>
</html>

三、實現一個漸變輪播效果效果范例

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>漸變效果的輪播</title>
  <style>
    *{
      margin: 0;
      padding: 0;
    }
    li{
      list-style: none;
    }
    a{
      text-decoration: none;
    }
    .clearfix:after{
      content: '';
      display: block;
      clear: both;
    }
    .carousel{
      position: relative;
      width: 890px;
      height: 500px;
      overflow: hidden;
      margin: 30px auto 0 auto;
    }
    .carousel .img-ct{
      position: relative;
      height: 500px;
    }
    .carousel .img-ct>li{
      position: absolute;
      display: none;
    }
    .carousel .img-ct img{
      width: 890px;
    }
    .carousel .btn{
      position: absolute;
      top: 50%;
      transform: translate(-50%);
      border-radius: 50%;
      display: inline-block;
      width: 40px;
      height: 40px;
      color: #fff;
      font-size: 25px;
      line-height: 40px;
      background: #4E443C;
      opacity: 0.7;
      text-align: center;
    }
    .carousel .btn:hover{
      opacity: .4;
    }
    .carousel .btn-before{
      left: 40px;
    }
    .carousel .btn-after{
      right: 0px;
    }

    .thubms{
      margin: 0 auto 30px auto;
      width: 870px;
      height: 80px;
    }
    .thubms .sample{
      height: 80px;
      padding: 10px 0;
      margin: 0 -10px;
      background: #000;
      /*opacity: .4;*/
    }
    .thubms .sample li{
      width: 100px;
      height: 80px;
      float: left;
      margin-left: 10px;
      opacity: 0.4;
      cursor: pointer;
    }
    .thubms .sample li:hover{
      opacity: 1;
    }
    .thubms .sample li img{
      width: 100px;
      height: 80px;
      /*opacity: 0.4;*/
    }
    .thubms .sample .selected{
      opacity: 1 !important;
      /*background: '';*/
    }
  </style>
</head>
<body>
  <div class="carousel clearfix">
    <ul class="img-ct clearfix">
      <li key="0"><a href="#">![](https://pic.500px.me/picurl/vcg63adcf0f48713cd18ae7a92b6c8f6f65?code=f7806b6971148251)</a></li>
      <li key="1"><a href="#">![](https://pic.500px.me/picurl/vcg03b1d9923fac7256c81c17958d4f882e?code=9e888779c4bbdd03)</a></li>
      <li key="2"><a href="#">![](https://pic.500px.me/picurl/vcgf41533b901031336c81c17958d4f882e?code=d9316f73ac0a8ff9)</a></li>
      <li key="3"><a href="#">![](https://pic.500px.me/picurl/vcge8dea73c36c31fdcf5d2686eec1068d1?code=35d9b41a28537e80)</a></li>
      <li key="4"><a href="#">![](https://pic.500px.me/picurl/vcg9b106378ed05cb76e4f983e555d5b23b?code=aec5da3e19ed284c)</a></li>
      <li key="5"><a href="#">![](https://pic.500px.me/picurl/vcgbdb54ca31dfb63298e4319f4fa182909?code=5af55580275e7104)</a></li>
      <li key="6"><a href="#">![](https://pic.500px.me/picurl/vcg92a58021db74f0e5f5d2686eec1068d1?code=ed786d5c917143ad)</a></li>
      <li key="7"><a href="#">![](https://pic.500px.me/picurl/vcg3f74a43608dbf06b8ae7a92b6c8f6f65?code=7f964635407a8312)</a></li>
    </ul>
    <a class="btn btn-before" href="#"><</a>
    <a class="btn btn-after" href="#">></a>
  </div>
  <div class="thubms">
    <ul class="sample clearfix">
      <li class="selected">![](https://pic.500px.me/picurl/vcg63adcf0f48713cd18ae7a92b6c8f6f65?code=f7806b6971148251)</li>
      <li>![](https://pic.500px.me/picurl/vcg03b1d9923fac7256c81c17958d4f882e?code=9e888779c4bbdd03)</li>
      <li>![](https://pic.500px.me/picurl/vcgf41533b901031336c81c17958d4f882e?code=d9316f73ac0a8ff9)</li>
      <li>![](https://pic.500px.me/picurl/vcge8dea73c36c31fdcf5d2686eec1068d1?code=35d9b41a28537e80)</li>
      <li>![](https://pic.500px.me/picurl/vcg9b106378ed05cb76e4f983e555d5b23b?code=aec5da3e19ed284c)</li>
      <li>![](https://pic.500px.me/picurl/vcgbdb54ca31dfb63298e4319f4fa182909?code=5af55580275e7104)</li>
      <li>![](https://pic.500px.me/picurl/vcg92a58021db74f0e5f5d2686eec1068d1?code=ed786d5c917143ad)</li>
      <li>![](https://pic.500px.me/picurl/vcg3f74a43608dbf06b8ae7a92b6c8f6f65?code=7f964635407a8312)</li>
    </ul>
  </div>

  <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
  <script>
    var $carousel = $('.carousel .img-ct').children(),
        $btnBefore = $('.btn-before'),
        $btnAfter = $('.btn-after'),
        $thubms = $('.thubms .sample').children(),
        thubmsNumber = $('.thubms .sample').children().length;
    var currentKey = 0;
    var playing = false;
    console.log($carousel);
    play(0);
    autoPlay();
    $btnBefore.on('click', playBefore);
    $btnAfter.on('click', playAfter);
    $thubms.on('click', function() {
      var clickIndex = $(this).index();
      play(clickIndex);
    })

    function autoPlay() {
      var beginPlay = setInterval("playAfter()", 3000);
    }

    function playBefore() {
      play((thubmsNumber + currentKey - 1)%thubmsNumber);
    }

    function playAfter() {
      console.log(currentKey+1);
      console.log(thubmsNumber);
      console.log((currentKey+1)%thubmsNumber);
      play((currentKey+1)%thubmsNumber);
      console.log(currentKey);
    }

    function play(index) {
      // if(playing) return;
      console.log(index);
      if(!playing) {
        playing = true;
        $carousel.eq(currentKey).fadeOut(400);
        $carousel.eq(index).fadeIn(400, function() {
          playing = false; // 動畫異步
        });
        currentKey = index;
        setSample(index, currentKey);
      }
    }

    function setSample(currentKey) {
      console.log(currentKey);
      $thubms.removeClass('selected').eq(currentKey).addClass('selected');
    }
  </script>
</body>
</html>
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,316評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,481評論 3 415
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,241評論 0 374
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,939評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,697評論 6 409
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,182評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,247評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,406評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,933評論 1 334
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,772評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,973評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,516評論 5 359
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,209評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,638評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,866評論 1 285
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,644評論 3 391
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,953評論 2 373

推薦閱讀更多精彩內容

  • 題目1: 輪播的實現原理是怎樣的?如果讓你來實現,你會抽象出哪些函數(or接口)供使用?(比如 play()) 左...
    cctosuper閱讀 255評論 0 0
  • 1: 輪播的實現原理是怎樣的?如果讓你來實現,你會抽象出哪些函數(or接口)供使用?(比如 play()) 一種左...
    曉風殘月1994閱讀 436評論 0 0
  • ** 1、 輪播的實現原理是怎樣的?如果讓你來實現,你會抽象出哪些函數(or接口)供使用?(比如 play())*...
    饑人谷_阿靖閱讀 230評論 0 0
  • 實現如下輪播效果(漸變輪播)task27-1 一個頁面有3個輪播task-27-2 實現如下無限循環滾動輪播效果t...
    JunVincetHuo閱讀 798評論 0 2
  • 輪播的實現原理是怎樣的?如果讓你來實現,你會抽象出哪些函數(or接口)供使用? 輪播的實現原理:把需要展示的圖片放...
    RookieD閱讀 124評論 0 0