JavaScript实现无缝轮播图的示例代码
花费一个下午从0到1实现的轮播图,当然还有很多需要改进的地方(欢迎提出需要改进的地方),等我再努力努力,将其封装成一个组件。
上效果
一、实现过程
1)首先实现基本布局
<div class="carousel-container"> //图片列表 <div class="carousel-list"></div> //上一张 <div class="carousel-arrow carousel-arrow-left"><</div> //下一张 <div class="carousel-arrow carousel-arrow-right">></div> //导航点 <div class="indicator"> <span class="active"></span> <span></span> <span></span> </div> </div>
2)主要样式
简单布局样式就不说了,主要讲如何将图片横向排列起来
先给容器设置相对定位,通过overflow将超出部分隐藏
.carousel-container { position: relative; width: 500px; height: 300px; /* overflow: hidden; */ background-color: #ccc; }
然后图片列表设置相对定位和flex盒子,这样每一个滑块就横向排列成一排了
.carousel-container .carousel-list { position: relative; display: flex; height: 100%; width: 100%; }
左右滑动按钮通过绝对定位+transform的方式移动到两边,导航点也是一样,就不一一详说了
二、如何实现无缝呢 (重点来了)
思路:
1、先实现向后滚动无缝连接,将最后一张复制一份放到最前面,当滚动到最后一张时,再次滚动,将要滚动到第一张时,先取消过渡transition,瞬间跳到最前面复制的那张上,然后继续运行动画到第一张,这样看起来就无缝了
2、向前滚动无缝连接,思路同上,复制第一张图片放到最后,当滚动到第一张,再次滚动时,瞬间跳到最后复制的那张图片上,继续滚动到轮播图的最后一张上。
主要代码
先获取到dom元素,currentIndex是当前轮播到的图片下标
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,复制图片
// 复制第一张放最后,最后一张图片放第一张之前 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%)' } //执行一下 init()
实现到任意一张图片的方法
function moveTo(index) { doms.carouselList.style.transform = `translateX(-${index * 100}%)` doms.carouselList.style.transition = '.5s' // 去掉导航点选中效果 let active = document.querySelector('.indicator span.active') active.classList.remove('active') // 添加选中效果 doms.indicator[index].classList.add('active') currentIndex = index }
给导航点绑定点击跳转事件
// 给导航点添加事件 doms.indicator.forEach((item, i) => { item.onclick = function () { moveTo(i); } })
给前后按钮绑上执行事件,判断边界图片,及时取消过渡效果,瞬间跳到复制的图片位置,调用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() }
最后使用定时器调用nertSlide方法就实现自动播放了
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"><</div> <div class="carousel-arrow carousel-arrow-right">></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' // 去掉导航点选中效果 let active = document.querySelector('.indicator span.active') active.classList.remove('active') // 添加选中效果 doms.indicator[index].classList.add('active') currentIndex = index } // 给导航点添加事件 doms.indicator.forEach((item, i) => { item.onclick = function () { moveTo(i); } }) // 复制第一张放最后,最后一张图片放第一张之前 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>
到此这篇关于JavaScript实现无缝轮播图的示例代码的文章就介绍到这了,更多相关JavaScript无缝轮播图内容请搜索猪先飞以前的文章或继续浏览下面的相关文章希望大家以后多多支持猪先飞!
原文出处:https://juejin.cn/post/7117266653695442957
相关文章
使用PHP+JavaScript将HTML页面转换为图片的实例分享
这篇文章主要介绍了使用PHP+JavaScript将HTML元素转换为图片的实例分享,文后结果的截图只能体现出替换的字体,也不能说将静态页面转为图片可以加快加载,只是这种做法比较interesting XD需要的朋友可以参考下...2016-04-19- 在昨天的《Javascript权威指南》学习笔记之十:ECMAScript 5 增强的对象模型一文中,对于一段代码的调试出现了一个奇怪现象,现将源代码贴在下面: 复制代码 代码如下: <script type="text/javascript"> function Person(){}...2014-05-31
- 最近做一个小项目不可避免的需要前端脚本与后台进行交互。由于是在asp.net中实现,故问题演化成asp.net中jiavascript与后台c#如何进行交互。...2020-06-25
- 复制代码 代码如下: //element:需要添加新样式的元素,value:新的样式 function addClass(element, value ){ if (!element.className){ element.className = value; }else { newClassName = element.className; newClas...2014-05-31
- 在javascritp中,不一定只有对象方法的上下文中才有this, 全局函数调用和其他的几种不同的上下文中也有this指代。 它可以是全局对象、当前对象或者任意对象,这完全取决于函数的调用方式。JavaScript 中函数的调用有以下...2015-03-15
- 有一道js面试题,题目是这样的:下列代码的执行结果是什么,为什么? 复制代码 代码如下: var i, j, k; for (i=0, j=0; i<10, j<6; i++, j++) { k = i+j; } document.write(k); 答案是显示10,这道题主要考察JavaScript的逗...2015-03-15
- 今天小编在这里就来给photoshop的这一款软件的使用者们来说一说制作菱形编织图案的无缝纹理背景的教程,各位想知道具体制作方法的,那么下面就来跟着小编一起看一看吧。...2016-09-14
- 事件触发器从字面意思上可以很好的理解,就是用来触发事件的,但是有些没有用过的朋友可能就会迷惑了,事件不是通常都由用户在页面上的实际操作来触发的吗?这个观点不完全正确,因为有些事件必须由程序来实现,如自定义事件,jQue...2014-06-07
- 首先,我想到的是另建一个结果数组,用来存储原始数组中不重复的数据。遍历原始数组依次跟结果数组中的元素进行比较,检测是否重复。于是乎,我写出了如下代码A: Array.prototype.clearRepetitionA = function(){ var resul...2015-11-08
- 1、ActiveX向Javascript传参 复制代码 代码如下: <script language="javascript" for="objectname" event="fun1(arg)"> fun2(arg); </script> objectname为ActiveX控件名,通过<object>标签里的id属性设定,如下; 复制...2014-06-07
- 这篇文章主要介绍了Javascript类型转换的规则实例解析,涉及到javascript类型转换相关知识,对本文感兴趣的朋友一起学习吧...2016-02-27
- 通过 HTML DOM,可访问 JavaScript HTML 文档的所有元素。 HTML DOM (文档对象模型) 当网页被加载时,浏览器会创建页面的文档对象模型(Document Object Model)。 HTML DOM 模型被构造为对象的树: 通过可编程的对象模型,Java...2015-10-23
- Window有navigator对象让我们得知浏览器的全部信息.我们可以利用一系列的API函数得知浏览器的信息.JavaScript代码如下:function message(){ txt = "<p>浏览器代码名: " + navigator.appCodeName + "</p>";txt+= "<p>...2015-11-24
- 虽然ES6都还没真正发布,但已经有用ES6重写的程序了,各种关于ES789的提议已经开始了,这你敢信。潮流不是我等大众所能追赶的。潮流虽然太快,但我们不停下学习的步伐,就不会被潮流丢下的,下面来领略下ES6中新特性,一堵新生代JS...2015-11-24
- 神马是“解释器模式”?先翻开《GOF》看看Definition:给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子。在开篇之前还是要科普几个概念: 抽象语法树: 解释器模式并未解释如...2014-06-07
- ---恢复内容开始---1.location.href.....(1)self.loction.href="http://www.cnblogs.com/url" window.location.href="http://www.cnblogs.com/url" 以上两个用法相同均为在当前页面打开URL页面 (2)this.locati...2015-10-30
- tab切换在网页中很常见,故最近总结了4种实现方法。 首先,写出tab的框架,加上最简单的样式,代码如下: <!DOCTYPE html> <html> <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><style> *{ pa...2015-11-08
- 这篇文章主要介绍了JavaScript预解析,对象的的相关资料,小编觉得这篇文章写的还不错,需要的朋友可以参考下,希望能够给你带来帮助...2021-11-10
- 这篇文章主要为大家介绍了JavaScript设计模式中的装饰者模式,对JavaScript设计模式感兴趣的小伙伴们可以参考一下...2016-01-21
基于JavaScript如何实现私有成员的语法特征及私有成员的实现方式
前言在面向对象的编程范式中,封装都是必不可少的一个概念,而在诸如 Java,C++等传统的面向对象的语言中, 私有成员是实现封装的一个重要途径。但在 JavaScript 中,确没有在语法特性上对私有成员提供支持, 这也使得开发人员使...2015-10-30