欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS實現(xiàn)多重選項卡切換輪播圖

 更新時間:2021年09月22日 11:17:00   作者:sv_Reg  
這篇文章主要為大家詳細(xì)介紹了JS實現(xiàn)多重選項卡切換輪播圖制作,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

輪播動畫來提升頁面的顏值和交互性能,下面我們將一起學(xué)習(xí)利用html , css和Javascript等前端開發(fā)基礎(chǔ)知識來制作一個簡單的輪播圖。

輪播圖簡介:在一個網(wǎng)站的某一特定模塊,通過電腦上鼠標(biāo)點擊或鼠標(biāo)移入、手機上手指滑動后,可以分別展示不同的圖片,這個模塊就叫做輪播模塊。

(做的不好的地方歡迎各位大佬批評指正,感覺有幫助的同學(xué)麻煩給顆星星哦~)

html布局部分:

<div id="box">
    <div class="scenery pic">
      <img class="show" src="imgs/s2.jpg" alt="scenery">
      <img src="imgs/s3.jpg" alt="scenery">
      <img src="imgs/s1.jpg" alt="scenery">
      <img src="imgs/s5.jpg" alt="scenery">
      <img src="imgs/s4.jpg" alt="scenery">
    </div>
    <div class="car pic">
      <img src="imgs/animal4.jpg" alt="animal">
      <img src="imgs/animal3.jpg" alt="animal">
      <img src="imgs/animal2.jpg" alt="animal">
      <img src="imgs/animal1.jpg" alt="animal">
    </div>
    <div class="entertainment pic">
      <img src="imgs/entertainment1.jpg" alt="entertainment">
      <img src="imgs/entertainment2.jpg" alt="entertainment">
      <img src="imgs/entertainment3.jpg" alt="entertainment">
      <img src="imgs/entertainment4.jpg" alt="entertainment">
      <img src="imgs/entertainment5.jpg" alt="entertainment">
    </div>
    <div class="leftbar">
      <div class="checked">風(fēng)景</div>
      <div>名車</div>
      <div>娛樂</div>
    </div>
    <div class="bottombar">
 
    </div>
</div>

CSS樣式部分:

/* 為了布局方便,容器里大多采用的flex */
#box {
      position: relative;
      width: 460px;
      height: 300px;
      margin: 40px auto;
      border: 1px solid rgb(109, 98, 98);
      user-select: none;
    }
    /* 側(cè)邊欄布局 */
    .leftbar {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      position: absolute;
      top: -1px;
      left: -80px;
      width: 80px;
      height: 100%;
      text-align: center;
      font-size: 20px;
      cursor: pointer;
    }
 
    .leftbar div {
      flex: 1;
      line-height: 100px;
      background-color: cadetblue;
      letter-spacing: 5px;
    }
 
    .leftbar div:nth-child(2) {
      border-top: 1px solid #fff;
      border-bottom: 1px solid #fff;
    }
 
    /* 底部切換按鈕樣式設(shè)計 */
    .bottombar {
      display: flex;
      justify-content: space-between;
      position: absolute;
      bottom: -1px;
      right: -1px;
      z-index: 10;
      width: 200px;
      height: 30px;
      cursor: pointer;
    }
 
    .bottombar div {
      flex: 1;
      line-height: 30px;
      background-color: rgb(232, 233, 235, .5);
      text-align: center;
      font-weight: 700;
    }
 
    .bottombar div~div {
      border-left: 1px solid grey;
    }
 
    img {
      position: absolute;
      display: block;
      width: 460px;
      height: 300px;
    }
 
    .show {
      z-index: 5;
    }
 
    .leftbar .checked,
    .bottombar .checked {
      background-color: rgb(241, 5, 5);
    }

javascript邏輯實現(xiàn)部分:

var Box = document.querySelector('#box'), pic = Box.querySelectorAll('.pic'),
    Idx = 0, index = 0, timer = null,
    ltDiv = Box.querySelector('.leftbar'), btDiv = Box.querySelector('.bottombar'),
    Img = Box.querySelectorAll('img');
 
    function createBtBar(len) {//動態(tài)創(chuàng)建底部切換按鈕
      var str = '';
      for (var i = 0; i < len; i++) {
        str += `<div>${i + 1}</div>`;
      }
      btDiv.innerHTML = str;
      btDiv.children[0].classList.add('checked');
    }
 
    function initialize() {//頁面初始狀態(tài)
      createBtBar(pic[0].children.length);
    }
    initialize();
 
    function clearZindex() {//重置所有圖片的定位層級
      for (var k = 0; k < Img.length; k++) {
        Img[k].classList.remove('show');
      }
    }
 
    ltDiv.addEventListener('click', (e) => {//側(cè)邊欄項目切換
      if (e.target.tagName.toLowerCase() === 'div') {
        for (var j = 0; j < ltDiv.children.length; j++) {
          ltDiv.children[j].classList.remove('checked');
        }
 
        clearZindex();
        Idx = 0;
        index = getEleIdx(e.target);
        ltDiv.children[index].classList.add('checked');
        pic[index].children[0].classList.add('show');
        createBtBar(pic[index].children.length);
      }
    });
 
    btDiv.addEventListener('click', (e) => {//委托監(jiān)聽底部切換按鈕
      if (e.target.tagName.toLowerCase() === 'div') {
        function changePic(callback) {
          btDiv.children[Idx].classList.remove('checked');
 
          clearZindex();
          callback && callback();
          btDiv.children[Idx].classList.add('checked');
          pic[index].children[Idx].classList.add('show');
        }
        changePic(function () {
          Idx = getEleIdx(e.target);
        });
      }
    });
 
    function getEleIdx(item) {//獲取DOM元素下標(biāo)
      var elements = item.parentNode.children;
      for (var i = 0, len = elements.length; i < len; i++) {
        if (item === elements[i]) {
          return i;
        }
      }
    }
 
    function loopShow() {//循環(huán)自動展示
      clearInterval(timer);
      timer = setInterval(function () {
        pic[index].children[Idx].classList.remove('show');
        btDiv.children[Idx].classList.remove('checked');
        Idx += 1;
        if (Idx < 0 || Idx > pic[index].children.length - 1) {
          Idx = 0;
        }
        pic[index].children[Idx].classList.add('show');
        btDiv.children[Idx].classList.add('checked');
      }, 1000);
    }
 
    loopShow();
 
    Box.addEventListener('mouseover', function () {
      clearInterval(timer);//鼠標(biāo)移入展示區(qū)停止輪播
    });
    Box.addEventListener('mouseout', function () {
      loopShow();//鼠標(biāo)移出展示區(qū)自動輪播
    });

具體實現(xiàn)的展示效果入下:

 (Tip: 各位注意自行準(zhǔn)備圖片放到自己的文件夾里哦~)

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • jfinal與bootstrap的登出實戰(zhàn)詳解

    jfinal與bootstrap的登出實戰(zhàn)詳解

    這篇文章主要為大家詳細(xì)介紹了jfinal與bootstrap的登出實戰(zhàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • BootStrap給table表格的每一行添加一個按鈕事件

    BootStrap給table表格的每一行添加一個按鈕事件

    這篇文章主要介紹了BootStrap給table表格的每一行添加一個按鈕事件的相關(guān)資料,需要的朋友可以參考下
    2017-09-09
  • 常用的js驗證和數(shù)據(jù)處理總結(jié)

    常用的js驗證和數(shù)據(jù)處理總結(jié)

    遇到需要對數(shù)據(jù)及表單驗證的,我相信大家都像我一樣,喜歡在網(wǎng)上找相關(guān)的方法,因為自己寫的話,是比較耗時的。今天就給大家分享一下,自己在工作中總結(jié)的一些常用的js。
    2016-08-08
  • js使彈層下面的body禁止?jié)L動

    js使彈層下面的body禁止?jié)L動

    這篇文章介紹了js使彈層下面body禁止?jié)L動的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • 基于JS實現(xiàn)二維碼圖片固定在右下角某處并跟隨滾動條滾動

    基于JS實現(xiàn)二維碼圖片固定在右下角某處并跟隨滾動條滾動

    這篇文章主要介紹了基于JS實現(xiàn)二維碼圖片固定在右下角某處并跟隨滾動條滾動,代碼簡單易懂非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-02-02
  • JavaScript第七種數(shù)據(jù)類型Symbol的用法詳解

    JavaScript第七種數(shù)據(jù)類型Symbol的用法詳解

    Symbol是ES6中引入的一種新的基本數(shù)據(jù)類型,用于表示一個獨一無二的值。它是JavaScript中的第七種數(shù)據(jù)類型。本文將詳細(xì)講講Symbol的使用,需要的可以參考一下
    2022-09-09
  • js通過var定義全局變量與在window對象上直接定義屬性的區(qū)別說明

    js通過var定義全局變量與在window對象上直接定義屬性的區(qū)別說明

    這篇文章主要介紹了js通過var定義全局變量與在window對象上直接定義屬性的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • 小程序中的箭頭函數(shù)的具體使用

    小程序中的箭頭函數(shù)的具體使用

    這篇文章主要介紹了小程序中的箭頭函數(shù)的具體使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • uni-app的pages.json處理方案示例

    uni-app的pages.json處理方案示例

    這篇文章主要為大家介紹了uni-app的pages.json處理方案示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-01-01
  • JavaScript 中級筆記 第三章

    JavaScript 中級筆記 第三章

    接著(2)來講。筆記(2)已經(jīng)講了引用和函數(shù)重載,接下來講解作用域。
    2009-09-09

最新評論