如何在JavaScript中使用JQuery实现一个轮播图效果
创始人
2024-12-27 17:43:11
0次

代码:





  
  无缝轮播图


  * {
    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;
    /*每张图片的宽度和轮播图容器的宽度相等,
    整个图片层长度:500*5=2500,由于克隆了一张,多加一张宽度*/
    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;
    /*可以使用行块盒*/
    /*display: inline-block;*/
    margin: 0 5px;
    cursor: pointer;
  }

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

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



  
  
    
    
      
      
  •       
  •       
  •       
  •       
  •              
  •     
        <
        >
                                      
  •         
  •         
  •         
  •            
      
                 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 === 4) imgIndex = 0; 这样写会导致图片会一下变到第一张,不是无缝滚动       无缝滚动:       1. 把第一张克隆一份放到末尾,wrapper中会比真实的图片层多一张       2. 依然一张张往后滚动,滚动到第5张的时候,继续向后走(imgIndex=6),看到了克隆的第一张,再要向后走的时候,         让其“立即”跳转到真实的第一张(肉眼看不出跳转),然后运动到第二张......       */       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();     }     autoTimer = setInterval(autoPlay, interval);     // 圆点对焦     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');       });     }     // 鼠标进入/离开轮播区域时停止/开启自动轮播     $container.on('mouseenter', () => {       clearInterval(autoTimer);     });     $container.on('mouseleave', () => {       autoTimer = setInterval(autoPlay, interval);     });     // 点击圆点     $dotList.click(function () {       let index = $(this).index();       imgIndex = index;       $wrapper.stop().animate({         left: -imgIndex * 500 //JQ自动补'px'       }, 300);       showDots();     });     // 左键点击     $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(function() {       autoPlay();     });   

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

    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;
        /*每张图片的宽度和轮播图容器的宽度相等,
        整个图片层长度:500*5=2500,由于克隆了一张,多加一张宽度*/
        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;
        /*可以使用行块盒*/
        /*display: inline-block;*/
        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 === 4) imgIndex = 0; 这样写会导致图片会一下变到第一张,不是无缝滚动
            无缝滚动:
            1. 把第一张克隆一份放到末尾,wrapper中会比真实的图片层多一张
            2. 依然一张张往后滚动,滚动到第5张的时候,继续向后走(imgIndex=6),看到了克隆的第一张,再要向后走的时候,
              让其“立即”跳转到真实的第一张(肉眼看不出跳转),然后运动到第二张......
            */
        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();

    上一篇:使用java编写一个贪吃蛇游戏

    下一篇:计算机网络的主要功能有哪些

    相关内容

    热门资讯

    空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
    空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
    格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
    空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
    格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
    品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
    空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
    空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
    中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
    中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...