JS实现普通轮播图特效

yizhihongxing

JS实现普通轮播图特效主要包含以下步骤:

  1. 利用CSS实现轮播图的基本布局,例如轮播图容器和图片容器的样式设置等。需要注意的是,轮播图容器一般为固定宽度,图片容器需要设置为横向排列并且宽度为轮播图容器的n倍(其中n为图片数量)。
.carousel {
  width: 600px; /* 容器宽度为600像素 */
  overflow: hidden; /* 超出部分隐藏 */
}

.carousel .imgContainer {
  display: flex; /* 图片容器横向排列 */
  width: 2400px; /* 图片容器宽度为600×4=2400像素 */
}
  1. 利用JS获取轮播图容器、图片容器和每张图片的宽度。同时,为了实现无缝轮播,需要在图片容器的首尾各复制一次图片,并在滚动到复制的图片时瞬间跳转到相应的原始图片上。
const carouselEl = document.querySelector('.carousel');
const imgContainerEl = document.querySelector('.imgContainer');
const imgWidth = document.querySelector('.imgContainer img').offsetWidth;
imgContainerEl.innerHTML += imgContainerEl.innerHTML; // 在首尾各复制一次图片
let currentIndex = 0; // 当前图片的索引
  1. 实现图片的无缝滚动。每次将图片容器向左滚动一个图片的宽度,并根据当前的索引值更新图片容器的left值。在滚动到复制的图片时,瞬间跳转到相应的原始图片上。
function slide() {
  currentIndex++;
  // 当滚动到复制的图片时,瞬间跳转到相应的原始图片上
  if (currentIndex === imgContainerEl.children.length - 1) {
    currentIndex = 1;
    imgContainerEl.style.left = 0;
  }
  const left = -1 * currentIndex * imgWidth + 'px';
  imgContainerEl.style.left = left;
}

setInterval(slide, 2000); // 每两秒滚动一次
  1. 实现轮播图的自动播放。在定时器中实现图片的滚动,并根据当前的索引值更新图片容器的left值,从而实现轮播图的滚动效果。
function slide() {
  currentIndex++; // 更新当前索引值
  // 当滚动到复制的最后一张图片时,瞬间跳转到相应的第一张原始图片上
  if (currentIndex === imgContainerEl.children.length - 1) {
    currentIndex = 1;
    imgContainerEl.style.left = 0;
  }
  const left = -1 * currentIndex * imgWidth + 'px';
  imgContainerEl.style.left = left;
}

let timer = setInterval(slide, 2000); // 每两秒滚动一次

// 鼠标悬停时停止自动播放
carouselEl.addEventListener('mouseenter', function() {
  clearInterval(timer);
});

// 鼠标离开时恢复自动播放
carouselEl.addEventListener('mouseleave', function() {
  timer = setInterval(slide, 2000);
});

示例1:用JS实现简单的轮播图特效

<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <title>JS实现普通轮播图特效</title>
  <style>
    .carousel {
      width: 600px;
      overflow: hidden;
    }

    .carousel .imgContainer {
      display: flex;
      width: 2400px;
    }

    .carousel .imgContainer img {
      width: 600px;
      height: 300px;
    }
  </style>
</head>

<body>
  <div class="carousel">
    <div class="imgContainer">
      <img src="https://picsum.photos/600/300?random=1">
      <img src="https://picsum.photos/600/300?random=2">
      <img src="https://picsum.photos/600/300?random=3">
      <img src="https://picsum.photos/600/300?random=4">
    </div>
  </div>
  <script>
    const carouselEl = document.querySelector('.carousel');
    const imgContainerEl = document.querySelector('.imgContainer');
    const imgWidth = document.querySelector('.imgContainer img').offsetWidth;
    imgContainerEl.innerHTML += imgContainerEl.innerHTML;
    let currentIndex = 0;

    function slide() {
      currentIndex++;
      if (currentIndex === imgContainerEl.children.length - 1) {
        currentIndex = 1;
        imgContainerEl.style.left = 0;
      }
      const left = -1 * currentIndex * imgWidth + 'px';
      imgContainerEl.style.left = left;
    }

    let timer = setInterval(slide, 2000);

    carouselEl.addEventListener('mouseenter', function() {
      clearInterval(timer);
    });

    carouselEl.addEventListener('mouseleave', function() {
      timer = setInterval(slide, 2000);
    });
  </script>
</body>

</html>

示例2:用JS实现可控制的轮播图特效

<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <title>JS实现普通轮播图特效</title>
  <style>
    .carousel {
      width: 600px;
      overflow: hidden;
      position: relative;
    }

    .carousel .imgContainer {
      display: flex;
      width: 2400px;
    }

    .carousel .imgContainer img {
      width: 600px;
      height: 300px;
    }

    .carousel .prev,
    .carousel .next {
      display: block;
      position: absolute;
      top: 50%;
      transform: translateY(-50%);
      width: 50px;
      height: 50px;
      line-height: 50px;
      text-align: center;
      font-size: 24px;
      color: #fff;
      background-color: rgba(0, 0, 0, 0.5);
      cursor: pointer;
    }

    .carousel .prev {
      left: 0;
    }

    .carousel .next {
      right: 0;
    }
  </style>
</head>

<body>
  <div class="carousel">
    <div class="imgContainer">
      <img src="https://picsum.photos/600/300?random=1">
      <img src="https://picsum.photos/600/300?random=2">
      <img src="https://picsum.photos/600/300?random=3">
      <img src="https://picsum.photos/600/300?random=4">
    </div>
    <span class="prev">&lt;</span>
    <span class="next">&gt;</span>
  </div>
  <script>
    const carouselEl = document.querySelector('.carousel');
    const imgContainerEl = document.querySelector('.imgContainer');
    const imgWidth = document.querySelector('.imgContainer img').offsetWidth;
    imgContainerEl.innerHTML += imgContainerEl.innerHTML;
    let currentIndex = 0;

    function slide() {
      currentIndex++;
      if (currentIndex === imgContainerEl.children.length - 1) {
        currentIndex = 1;
        imgContainerEl.style.left = 0;
      }
      const left = -1 * currentIndex * imgWidth + 'px';
      imgContainerEl.style.left = left;
    }

    let timer = setInterval(slide, 2000);

    carouselEl.addEventListener('mouseenter', function() {
      clearInterval(timer);
    });

    carouselEl.addEventListener('mouseleave', function() {
      timer = setInterval(slide, 2000);
    });

    const prevEl = document.querySelector('.prev');
    const nextEl = document.querySelector('.next');
    prevEl.addEventListener('click', function() {
      clearInterval(timer);
      currentIndex--;
      if (currentIndex === -1) {
        currentIndex = imgContainerEl.children.length - 3;
        imgContainerEl.style.left = -1 * (currentIndex + 1) * imgWidth + 'px';
      }
      const left = -1 * currentIndex * imgWidth + 'px';
      imgContainerEl.style.left = left;
      timer = setInterval(slide, 2000);
    });

    nextEl.addEventListener('click', function() {
      clearInterval(timer);
      slide();
      timer = setInterval(slide, 2000);
    });
  </script>
</body>

</html>

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:JS实现普通轮播图特效 - Python技术站

(0)
上一篇 2023年6月10日
下一篇 2023年6月10日

相关文章

  • 教大家轻松制作Bootstrap漂亮表格(table)

    教大家轻松制作Bootstrap漂亮表格(table)攻略 Bootstrap表格的基本用法 Bootstrap是一个流行的前端框架,最大的好处就是可以轻松制作漂亮的网页元素,其中也包含了表格(table)。下面是Bootstrap表格的基本用法: <table class="table"> <thead> &lt…

    css 2023年6月10日
    00
  • css将两个元素水平对齐的方法(兼容IE8)

    实现将两个元素水平对齐可以使用flex布局和float布局两种方法,其中float布局在兼容性方面比较好,可以兼容IE8及以上版本的浏览器。 方法一:使用float布局 首先需要给需要对齐的元素设置浮动属性,再使用text-align属性设置其父元素的文本对齐方式为居中,这样就可以实现两个元素的水平居中对齐。 你可以参考下面的示例代码: <div cl…

    css 2023年6月10日
    00
  • 钉钉怎么导入外部的excel表格数据?

    钉钉是一款功能强大的企业管理软件,它不仅可以帮助企业高效沟通、快速决策、高效协作,还支持多种数据导入方式。下面是详细讲解如何导入外部的Excel表格数据的完整攻略: 步骤一:打开钉钉应用 在手机或电脑上打开钉钉应用,并进入需要导入外部Excel表格数据的群聊或普通聊天窗口。 步骤二:创建数据表 点击右下角的“+”号,选择“新建表格”,进入数据表编辑页面。 步…

    css 2023年6月10日
    00
  • CSS教程:导致一些问题的overflow

    CSS教程:导致一些问题的overflow 在CSS中,overflow属性被用于确定容器应如何处理溢出内容。当容器内的内容超出容器的尺寸时,该属性的值将决定用户是否可以滚动内容,或隐藏超出的部分。 然而,overflow属性可能导致一些问题。在本教程中,我们将介绍这些问题以及如何解决它们。 问题1: 块剪切 overflow:hidden属性可能导致内容被…

    css 2023年6月9日
    00
  • div+css详解定位与定位应用

    什么是定位? 定位是CSS的一个重要概念,指的是元素在页面中的准确位置。网页制作离不开定位,可以通过定位实现各种效果,如盒子居中、导航菜单等。CSS有三种定位方式: 静态定位(position: static):元素默认值就是静态定位,元素会根据文档流从上到下自动排列。 相对定位(position: relative):相对定位指元素相对于自己原来的位置进行…

    css 2023年6月11日
    00
  • css 解决英文字符与阿位伯数字自动换行

    为了解决英文字符与阿拉伯数字的自动换行问题,我们可以使用word-wrap和word-break属性来实现。word-wrap属性定义了在何处断开单词或者长单词换行;word-break属性则定义了在何处断开一个单词来实现自动换行。 具体来说,我们可以使用以下代码来实现自动换行: word-wrap: break-word; // 当单词太长时,自动换行,防…

    css 2023年6月10日
    00
  • Bootstrap每天必学之按钮

    Bootstrap每天必学之按钮 Bootstrap是一个广受欢迎的前端开发框架,提供了一套可重用的UI组件,使开发者能够快速构建漂亮且高效的网站和应用程序。按钮是Bootstrap中最基本的组件之一,本文将为您介绍Bootstrap按钮的完整攻略。 Bootstrap按钮的基本用法 Bootstrap按钮有多种样式,可以通过不同的类名进行选择和应用。最基本…

    css 2023年6月11日
    00
  • Bootstrap打造一个左侧折叠菜单的系统模板(一)

    我来详细讲解一下”Bootstrap打造一个左侧折叠菜单的系统模板(一)”的完整攻略。此文章的攻略包含以下三部分: 1. 准备工作 在编写左侧折叠菜单前,需要先引入Bootstrap框架。打开网页http://getbootstrap.com/并按照引导指示下载所需文件即可。同时,该模板基于jQuery,所以也需要在页面中引入jQuery库: <!–…

    css 2023年6月9日
    00
合作推广
合作推广
分享本页
返回顶部