最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaScript實(shí)現(xiàn)無縫輪播圖的示例代碼

 更新時(shí)間:2022年07月08日 11:35:19   作者:不當(dāng)菜菜  
這篇文章主要為大家詳細(xì)介紹了如何利用JavaScript語言實(shí)現(xiàn)無縫輪播功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

花費(fèi)一個下午從0到1實(shí)現(xiàn)的輪播圖,當(dāng)然還有很多需要改進(jìn)的地方(歡迎提出需要改進(jìn)的地方),等我再努力努力,將其封裝成一個組件。

上效果

一、實(shí)現(xiàn)過程

1)首先實(shí)現(xiàn)基本布局

 <div class="carousel-container">
    //圖片列表
    <div class="carousel-list"></div>
    //上一張
    <div class="carousel-arrow carousel-arrow-left">&lt</div>
    //下一張
    <div class="carousel-arrow carousel-arrow-right">&gt</div>
    //導(dǎo)航點(diǎn)
    <div class="indicator">
      <span class="active"></span>
      <span></span>
      <span></span>
    </div>
  </div>

2)主要樣式

簡單布局樣式就不說了,主要講如何將圖片橫向排列起來

先給容器設(shè)置相對定位,通過overflow將超出部分隱藏

.carousel-container {
      position: relative;
      width: 500px;
      height: 300px;
      /* overflow: hidden; */
      background-color: #ccc;
 }

然后圖片列表設(shè)置相對定位和flex盒子,這樣每一個滑塊就橫向排列成一排了

.carousel-container .carousel-list {
      position: relative;
      display: flex;
      height: 100%;
      width: 100%;
 }

左右滑動按鈕通過絕對定位+transform的方式移動到兩邊,導(dǎo)航點(diǎn)也是一樣,就不一一詳說了

二、如何實(shí)現(xiàn)無縫呢 (重點(diǎn)來了)

思路:

1、先實(shí)現(xiàn)向后滾動無縫連接,將最后一張復(fù)制一份放到最前面,當(dāng)滾動到最后一張時(shí),再次滾動,將要滾動到第一張時(shí),先取消過渡transition,瞬間跳到最前面復(fù)制的那張上,然后繼續(xù)運(yùn)行動畫到第一張,這樣看起來就無縫了

2、向前滾動無縫連接,思路同上,復(fù)制第一張圖片放到最后,當(dāng)滾動到第一張,再次滾動時(shí),瞬間跳到最后復(fù)制的那張圖片上,繼續(xù)滾動到輪播圖的最后一張上。

主要代碼

先獲取到dom元素,currentIndex是當(dāng)前輪播到的圖片下標(biāo)

let currentIndex = 0;
const doms = {
      carouselList: document.querySelector('.carousel-list'),
      arrowLeft: document.querySelector('.carousel-arrow-left'),
      arrowRight: document.querySelector('.carousel-arrow-right'),
      indicator: document.querySelectorAll('.indicator span')
    }

先初始化dom,復(fù)制圖片

// 復(fù)制第一張放最后,最后一張圖片放第一張之前
    function init() {
      let lastImg = doms.carouselList.lastElementChild.cloneNode(true)
      let firstImg = doms.carouselList.firstElementChild.cloneNode(true)

      doms.carouselList.appendChild(firstImg)
      doms.carouselList.insertBefore(lastImg, doms.carouselList.firstElementChild)
      lastImg.style.position = 'absolute'
      lastImg.style.transform = 'translateX(-100%)'
    }
    //執(zhí)行一下
    init()

實(shí)現(xiàn)到任意一張圖片的方法

function moveTo(index) {
      doms.carouselList.style.transform = `translateX(-${index * 100}%)`
      doms.carouselList.style.transition = '.5s'

      // 去掉導(dǎo)航點(diǎn)選中效果
      let active = document.querySelector('.indicator span.active')
      active.classList.remove('active')
      // 添加選中效果
      doms.indicator[index].classList.add('active')
      currentIndex = index
    }

給導(dǎo)航點(diǎn)綁定點(diǎn)擊跳轉(zhuǎn)事件

// 給導(dǎo)航點(diǎn)添加事件
    doms.indicator.forEach((item, i) => {
      item.onclick = function () {
        moveTo(i);
      }
    })

給前后按鈕綁上執(zhí)行事件,判斷邊界圖片,及時(shí)取消過渡效果,瞬間跳到復(fù)制的圖片位置,調(diào)用moveTo到第一張或最后一張圖片上。

let indicatorLength = doms.indicator.length;
    function preSlide() {
      if (currentIndex === 0) {
        doms.carouselList.style.transition = 'none'
        doms.carouselList.style.transform = `translateX(-${indicatorLength * 100}%)`
        doms.carouselList.clientHeight
        moveTo(indicatorLength - 1)
      } else {
        moveTo(currentIndex - 1)
      }
    }
    function nextSlide() {
      if (currentIndex === doms.indicator.length - 1) {
        doms.carouselList.style.transition = 'none'
        doms.carouselList.style.transform = 'translateX(100%)'
        doms.carouselList.clientHeight
        moveTo(0)
      } else {
        moveTo(currentIndex + 1)
      }
    }

    doms.arrowLeft.onclick = function () {
      preSlide();
    }

    doms.arrowRight.onclick = function () {
      nextSlide()
    }

最后使用定時(shí)器調(diào)用nertSlide方法就實(shí)現(xiàn)自動播放了

function start(time = 2000) {
      setInterval(() => {
        nextSlide()
      }, time)
    }
    start()

完整代碼

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    .carousel-container {
      margin: 0 auto;
      position: relative;
      width: 500px;
      height: 300px;
      /* overflow: hidden; */
      background-color: #ccc;
    }

    .carousel-container .carousel-list {
      position: relative;
      display: flex;
      height: 100%;
      width: 100%;
    }

    .carousel-container .carousel-list .slide {
      flex: 0 0 100%;
      height: 100%;
      width: 100%;
    }

    .slide a {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100%;
      width: 100%;
    }

    .slide a img {
      width: 100%;
    }

    .carousel-container .carousel-arrow {
      display: none;
      position: absolute;
      width: 36px;
      height: 36px;
      border-radius: 50%;
      color: white;
      text-align: center;
      line-height: 36px;
      cursor: pointer;

      background-color: rgba(31, 45, 61, .2);
    }

    .carousel-container:hover .carousel-arrow {
      display: block;
    }

    .carousel-container:hover .carousel-arrow:hover {
      background-color: rgba(31, 45, 61, .4);
    }

    .carousel-container .carousel-arrow-left {
      top: 50%;
      left: 2%;
      transform: translateY(-50%);
    }

    .carousel-container .carousel-arrow-right {
      top: 50%;
      right: 2%;
      transform: translateY(-50%);
    }

    .carousel-container .indicator {
      display: flex;
      position: absolute;
      left: 50%;
      top: 90%;
      transform: translateX(-50%)
    }

    .carousel-container .indicator span {
      margin: 2px 5px;
      padding: 3px;
      width: 5px;
      height: 5px;
      border: 1px solid white;
      border-radius: 5px;
    }

    .active {
      background-color: #fff;
    }
  </style>
</head>


<body>
  <div class="carousel-container">
    <div class="carousel-list">
      <div class="slide">
        <a href="">
          <img
            src="https://ts4.cn.mm.bing.net/th?id=OIP-C.kB-Ovasi0GW67-rmwnAcwAHaEo&w=316&h=197&c=8&rs=1&qlt=90&o=6&dpr=1.25&pid=3.1&rm=2">
        </a>
      </div>
      <div class="slide">
        <a href="">
          <img
            src="https://ts1.cn.mm.bing.net/th?id=OIP-C.QPH1IBosDYBqaU3O6wV3YAHaEo&w=316&h=197&c=8&rs=1&qlt=90&o=6&dpr=1.25&pid=3.1&rm=2"></a>
      </div>
      <div class="slide">
        <a href="">
          <img
            src="https://ts2.cn.mm.bing.net/th?id=OIP-C.P3NSGTdAYdyqy5zJpb5QXQHaEo&w=316&h=197&c=8&rs=1&qlt=90&o=6&dpr=1.25&pid=3.1&rm=2"
            alt=""></a>
      </div>
    </div>
    <div class="carousel-arrow carousel-arrow-left">&lt</div>
    <div class="carousel-arrow carousel-arrow-right">&gt</div>
    <div class="indicator">
      <span class="active"></span>
      <span></span>
      <span></span>
    </div>
  </div>
</body>

<script>
  window.onload = function () {
    const doms = {
      carouselList: document.querySelector('.carousel-list'),
      arrowLeft: document.querySelector('.carousel-arrow-left'),
      arrowRight: document.querySelector('.carousel-arrow-right'),
      indicator: document.querySelectorAll('.indicator span')
    }
    let currentIndex = 0;

    function moveTo(index) {
      doms.carouselList.style.transform = `translateX(-${index * 100}%)`
      doms.carouselList.style.transition = '.5s'

      // 去掉導(dǎo)航點(diǎn)選中效果
      let active = document.querySelector('.indicator span.active')
      active.classList.remove('active')
      // 添加選中效果
      doms.indicator[index].classList.add('active')
      currentIndex = index
    }

    // 給導(dǎo)航點(diǎn)添加事件
    doms.indicator.forEach((item, i) => {
      item.onclick = function () {
        moveTo(i);
      }
    })
    // 復(fù)制第一張放最后,最后一張圖片放第一張之前
    function init() {
      let lastImg = doms.carouselList.lastElementChild.cloneNode(true)
      let firstImg = doms.carouselList.firstElementChild.cloneNode(true)

      doms.carouselList.appendChild(firstImg)
      doms.carouselList.insertBefore(lastImg, doms.carouselList.firstElementChild)
      lastImg.style.position = 'absolute'
      lastImg.style.transform = 'translateX(-100%)'
    }

    let indicatorLength = doms.indicator.length;
    function preSlide() {
      if (currentIndex === 0) {
        doms.carouselList.style.transition = 'none'
        doms.carouselList.style.transform = `translateX(-${indicatorLength * 100}%)`
        doms.carouselList.clientHeight
        moveTo(indicatorLength - 1)
      } else {
        moveTo(currentIndex - 1)
      }
    }
    function nextSlide() {
      if (currentIndex === doms.indicator.length - 1) {
        doms.carouselList.style.transition = 'none'
        doms.carouselList.style.transform = 'translateX(100%)'
        doms.carouselList.clientHeight
        moveTo(0)
      } else {
        moveTo(currentIndex + 1)
      }
    }

    doms.arrowLeft.onclick = function () {
      preSlide();
    }

    doms.arrowRight.onclick = function () {
      nextSlide()
    }

    function start(time = 2000) {
      setInterval(() => {
        nextSlide()
      }, time)
    }
    start()
    init()

  }
</script>

</html>

到此這篇關(guān)于JavaScript實(shí)現(xiàn)無縫輪播圖的示例代碼的文章就介紹到這了,更多相關(guān)JavaScript無縫輪播圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • javascript操縱OGNL標(biāo)簽示例代碼

    javascript操縱OGNL標(biāo)簽示例代碼

    這篇文章主要介紹了javascript 怎么操縱OGNL標(biāo)簽,需要的朋友可以參考下
    2014-06-06
  • 原生js實(shí)現(xiàn)表格循環(huán)滾動

    原生js實(shí)現(xiàn)表格循環(huán)滾動

    這篇文章主要為大家詳細(xì)介紹了原生js實(shí)現(xiàn)表格循環(huán)滾動,每次滾動一行停頓,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • 原生JS實(shí)現(xiàn)響應(yīng)式瀑布流布局

    原生JS實(shí)現(xiàn)響應(yīng)式瀑布流布局

    本文給大家分享的是使用原生的javascript實(shí)現(xiàn)的響應(yīng)式的瀑布流布局的方法和源碼,非常的實(shí)用,有需要的小伙伴可以參考下。
    2015-04-04
  • 基于JS實(shí)現(xiàn)移動端訪問PC端頁面時(shí)跳轉(zhuǎn)到對應(yīng)的移動端網(wǎng)頁

    基于JS實(shí)現(xiàn)移動端訪問PC端頁面時(shí)跳轉(zhuǎn)到對應(yīng)的移動端網(wǎng)頁

    不想通過CSS自適應(yīng)在PC端和移動端分別顯示不同的樣式,那么只能通過在移動端訪問PC端網(wǎng)頁時(shí)跳轉(zhuǎn)到對應(yīng)的移動端網(wǎng)頁了,那么怎么跳轉(zhuǎn)呢,網(wǎng)上也有很多文章說明,以下實(shí)現(xiàn)思路經(jīng)過小編測試過,需要的朋友可以參考下
    2016-04-04
  • javascript彈出頁面回傳值的方法

    javascript彈出頁面回傳值的方法

    這篇文章主要介紹了javascript彈出頁面回傳值的方法,實(shí)例分析了由a1.html彈出的b1.html回傳值的實(shí)現(xiàn)方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-01-01
  • 使用cropper.js裁剪頭像的實(shí)例代碼

    使用cropper.js裁剪頭像的實(shí)例代碼

    最近小編在做一個裁剪頭像的功能,下面小編把使用cropper.js裁剪頭像的實(shí)例代碼分享到腳本之家平臺,需要朋友參考下吧
    2017-09-09
  • 原生js實(shí)現(xiàn)秒表計(jì)時(shí)器功能

    原生js實(shí)現(xiàn)秒表計(jì)時(shí)器功能

    這篇文章主要為大家詳細(xì)介紹了原生js實(shí)現(xiàn)秒表計(jì)時(shí)器功能,可以開始、暫停、清除,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • js tr控制下面的tbody隱藏和顯示

    js tr控制下面的tbody隱藏和顯示

    這個問題弄了我?guī)滋鞗]心思寫程序,問了論壇很多網(wǎng)友才搞定的。
    2008-07-07
  • 基于bootstrap實(shí)現(xiàn)多個下拉框同時(shí)搜索功能

    基于bootstrap實(shí)現(xiàn)多個下拉框同時(shí)搜索功能

    這篇文章主要為大家詳細(xì)介紹了基于bootstrap實(shí)現(xiàn)多個下拉框同時(shí)搜索功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • 全面解析JavaScript 中 null

    全面解析JavaScript 中 null

    null 是一種原始類型,表示有意不包含任何對象值,在這篇文章中,你將學(xué)習(xí)關(guān)于 JavaScript 中的 null 的一切: 它的含義,如何檢測它,null 和 undefined 之間的區(qū)別,以及為什么大量使用 null 會造成代碼維護(hù)困難等,需要的朋友可以參考下
    2022-09-09

最新評論

周至县| 凤城市| 宜良县| 开封市| 洪泽县| 太原市| 大冶市| 马山县| 景东| 平阳县| 丹巴县| 荥阳市| 浑源县| 红河县| 西宁市| 彰化市| 西藏| 封开县| 罗定市| 文昌市| 英山县| 临颍县| 古田县| 勐海县| 邵东县| 和龙市| 安新县| 申扎县| 会昌县| 宁蒗| 梁河县| 定结县| 涿鹿县| 瑞昌市| 儋州市| 临潭县| 蕲春县| 武鸣县| 吴旗县| 临安市| 新建县|