栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

JS+JQuery实现无缝连接轮播图

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JS+JQuery实现无缝连接轮播图

我之前写过一个简易版的自动+手动轮播图:简易轮播图
但是这个轮播图在切换的时候是没有实现无缝滚动的,每张图片都是单张切换的,而不是滑动。现在用JQuery实现无缝连接的轮播图。

无缝连接的轮播图的原理如下:

代码:





  
  无缝轮播图


  * {
    margin: 0;
    padding: 0;
    list-style: none;
    text-decoration: none;
  }

  #container {
    position: relative;
    
    width: 500px;
    height: 260px;
    margin: 20px auto;
    overflow: hidden;
    
  }

  #container .wrapper {
    position: absolute;
    top: 0;
    left: 0;
    
    width: 3000px;
    height: 100%;
  }

  #container .wrapper li {
    width: 500px;
    height: 100%;
    float: left;
  }

  #container .wrapper li img {
    width: 100%;
    height: 100%;
    vertical-align: middle;
    
  }

  #container .btnLeft,
  #container .btnRight {
    display: none;
    z-index: 999;
    width: 30px;
    height: 30px;
    position: absolute;
    top: 50%;
    margin-top: -15px;
    background-color: #9E9E9E;
    border-radius: 20%;
    opacity: 60%;
    font-size: 20px;
    color: #673ab7;
    text-align: center;
    line-height: 30px;
  }

  #container .btnLeft {
    left: 0;
  }

  #container .btnRight {
    right: 0;
  }

  #container .btnLeft:hover,
  #container .btnRight:hover {
    opacity: 70%;
    cursor: pointer;
  }

  
  #container:hover .btnLeft,
  #container:hover .btnRight {
    display: block;
  }

  
  #container .dots {
    background: rgba(0, 0, 0, .3);
    position: absolute;
    left: 50%;
    bottom: 10px;
    transform: translateX(-50%);
    z-index: 999;
    padding: 4px;
    border-radius: 24px;
  }

  #container .dots li {
    width: 15px;
    height: 15px;
    border-radius: 50%;
    background-color: #9e9e9e;
    float: left;
    
    
    margin: 0 5px;
    cursor: pointer;
  }

  #container .dots li.active {
    background-color: #c74b42;
  }

  .clearfix::after {
    content: "";
    display: block;
    clear: both;
  }



  
  
    
    
      
      
  • < >

    这段代码用单例模式优化一下:

    html部分:test.html

    
    
    
    
      
      无缝轮播图
    
    
      * {
        margin: 0;
        padding: 0;
        list-style: none;
        text-decoration: none;
      }
    
      #container {
        position: relative;
        
        width: 500px;
        height: 260px;
        margin: 20px auto;
        overflow: hidden;
        
      }
    
      #container .wrapper {
        position: absolute;
        top: 0;
        left: 0;
        
        width: 3000px;
        height: 100%;
      }
    
      #container .wrapper li {
        width: 500px;
        height: 100%;
        float: left;
      }
    
      #container .wrapper li img {
        width: 100%;
        height: 100%;
        vertical-align: middle;
        
      }
    
      #container .btnLeft,
      #container .btnRight {
        display: none;
        z-index: 999;
        width: 30px;
        height: 30px;
        position: absolute;
        top: 50%;
        margin-top: -15px;
        background-color: #9E9E9E;
        border-radius: 20%;
        opacity: 60%;
        font-size: 20px;
        color: #673ab7;
        text-align: center;
        line-height: 30px;
      }
    
      #container .btnLeft {
        left: 0;
      }
    
      #container .btnRight {
        right: 0;
      }
    
      #container .btnLeft:hover,
      #container .btnRight:hover {
        opacity: 70%;
        cursor: pointer;
      }
    
      
      #container:hover .btnLeft,
      #container:hover .btnRight {
        display: block;
      }
    
      
      #container .dots {
        background: rgba(0, 0, 0, .3);
        position: absolute;
        left: 50%;
        bottom: 10px;
        transform: translateX(-50%);
        z-index: 999;
        padding: 4px;
        border-radius: 24px;
      }
    
      #container .dots li {
        width: 15px;
        height: 15px;
        border-radius: 50%;
        background-color: #9e9e9e;
        float: left;
        
        
        margin: 0 5px;
        cursor: pointer;
      }
    
      #container .dots li.active {
        background-color: #c74b42;
      }
    
      .clearfix::after {
        content: "";
        display: block;
        clear: both;
      }
    
    
    
      
      
        
        
          
          
  • < >

    JS部分:index.js

    function debounce(func, wait, immediate) {
      let timer = null,
        result = null;
      return function anonymous(...args) {
        let context = this,
          now = immediate && !timer;
        clearTimeout(timer);
        timer = setTimeout(() => {
          timer = null;
          !immediate ? (result = func.call(context, ...args)) : null;
        }, wait);
        now ? (result = func.call(context, ...args)) : null;
        return result;
      };
    }
    
    let bannerModule = (function () {
      let $container = $("#container"),
        $wrapper = $container.children(".wrapper"),
        $btnLeft = $container.find(".btnLeft"),
        $btnRight = $container.find(".btnRight"),
        $dots = $container.find(".dots"),
        $dotList = $dots.find("li");
    
      let autoTimer = null,
        interval = 2000,
        imgIndex = 0; //当前轮播的图片索引,默认第一张
      // 自动轮播
      function autoPlay() {
        // 让wrapper向左移动
        imgIndex++;
        
        if (imgIndex > 5) {
          // 上次显示的是克隆的那张,忽略真实的第一张,让其立即跳转到第二张
          $wrapper.css("left", 0);
          imgIndex = 1;
        }
    
        // 匀速向左移动
        // 无动画版:$wrapper.css('transform', 'translate(' + (-imgIndex * 500) + 'px)');
        // 动画版:
        $wrapper.stop().animate({
          left: -imgIndex * 500 //JQ自动补'px'
        }, 300);
    
        showDots();
      }
    
      // 圆点对焦
      function showDots() {
        // 由于不能修改imgIndex的值,所以定义一个临时变量
        let temp = imgIndex;
        temp === 5 ? (temp = 0) : null;
        $dotList.each((index, item) => {
          let $item = $(item);
          if (index === temp) {
     $item.addClass("active");
     return;
          }
          $item.removeClass("active");
        });
      }
    
      //点击圆点
      function clickDots() {
        $dotList.click(debounce(function () {
          let index = $(this).index();
          imgIndex = index;
    
          $wrapper.stop().animate({
     left: -imgIndex * 500 //JQ自动补'px'
          }, 300);
    
          showDots();
        },300,true));
      }
    
      // 左右按键
      function btnClick() {
        $btnLeft.click(function () {
          imgIndex--;
          if (imgIndex < 0) {
     // 上次显示的是真实的第一张,忽略克隆的倒数第一张,让其立即跳转倒数第二张
     $wrapper.css('left', -2500);
     imgIndex = 4;
          }
    
          $wrapper.stop().animate({
     left: -imgIndex * 500 //JQ自动补'px'
          }, 300);
    
          showDots();
        });
    
        // 右键点击:相当于自动轮播
        $btnRight.click(debounce(autoPlay, 300, true));
      }
    
      return {
        init: function () {
          autoTimer = setInterval(autoPlay, interval);
    
          // 鼠标进入/离开轮播区域时停止/开启自动轮播
          $container.on("mouseenter", () => {
     clearInterval(autoTimer);
          });
          $container.on("mouseleave", () => {
     autoTimer = setInterval(autoPlay, interval);
          });
    
          clickDots();
          btnClick();
        },
      };
    })();
    
    bannerModule.init();

    以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

    转载请注明:文章转载自 www.mshxw.com
    本文地址:https://www.mshxw.com/it/67250.html
    我们一直用心在做
    关于我们 文章归档 网站地图 联系我们

    版权所有 (c)2021-2022 MSHXW.COM

    ICP备案号:晋ICP备2021003244-6号