JavaScript实现六种网页图片轮播效果详解
本文详细解析如何使用JavaScript实现包含左右切换按钮、动态小圆点、无缝滚动及自动播放功能的图片轮播组件。通过HTML布局与CSS定位构建基础结构,利用DOM操作动态生成导航元素,结合动画函数实现平滑过渡,并处理首尾图片无缝衔接逻辑,最终完成一个功能完整的网页轮播图效果。
在网页设计中,轮播图是一种常见且实用的展示形式。为了演示其实现原理,我们首先准备五张示例图片作为素材。
功能需求:
当鼠标悬停在轮播图区域时,左右导航按钮显示;鼠标移开后,按钮自动隐藏。
点击右侧按钮,图片向左切换一张,左侧按钮逻辑相同。
图片切换时,下方对应的小圆点指示器需同步高亮显示。
点击任意小圆点,可直接跳转到对应的图片页面。
当鼠标未进入轮播区域时,图片应自动进行循环播放。
当鼠标进入轮播区域时,自动播放功能暂停。
页面布局结构如下所示:

接下来逐步实现各项功能。
首先创建HTML页面结构。
实现流程如下:先创建一个外层大容器,并设置相对定位以便内部元素绝对定位。将图片放入无序列表(ul)中,由于需要横向排列,给图片添加float:left属性。因初始ul宽度不足导致图片换行,需手动调整ul宽度。接着创建另一个无序列表用于放置小圆点,通过绝对定位将其置于大容器下方,实现点击跳转效果。最后,通过绝对定位将左右箭头按钮放置在容器两侧合适位置,并隐藏容器溢出内容。
<!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>
<link rel="stylesheet" href="index.css" rel="external nofollow" >
</head>
<body>
<div class="box">
<a href="" class = 'left jiantou'><</a>
<a href="" class = 'right jiantou'>></a>
<ul class = 'pic'>
<li>
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="./images/1.jpg" alt=""></a>
</li>
<li>
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="./images/2.jpg" alt=""></a>
</li>
<li>
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="./images/3.jpg" alt=""></a>
</li>
<li>
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="./images/4.jpg" alt=""></a>
</li>
<li>
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><img src="./images/5.jpg" alt=""></a>
</li>
</ul>
<ul class="lis">
<li></li>
<li class = 'selected'></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
</body>
</html>
css文件
*{
margin: 0;
padding: 0;
}
li{
list-style: none;
}
.box{
position: relative;
overflow: hidden;
margin: 100px auto;
width: 520px;
height: 280px;
background-color: red;
}
.jiantou{
font-size: 24px;
text-decoration: none;
display: block;
text-align: center;
width: 20px;
height: 30px;
line-height: 30px;
background: rgba(158, 154, 154, 0.7);
color: white;
z-index: 999;
}
.left{
position: absolute;
top: 125px;
left: 0px;
border-top-right-radius: 15px;
border-bottom-right-radius: 15px;
}
.right{
position: absolute;
top:125px;
left: 500px;
border-top-left-radius: 15px;
border-bottom-left-radius: 15px;
}
img{
width: 520px;
height: 280px;
}
.box .pic{
width: 600%;
}
.pic li {
float: left;
}
.lis{
position: absolute;
bottom: 15px;
left: 50%;
margin-left: -35px;
width: 70px;
height:13px;
border-radius: 7px;
background: rgba(158, 154, 154, 0.7);
}
.lis li {
float: left;
width: 8px;
height: 8px;
margin: 3px;
border-radius: 50%;
background-color: #fff;
}
.lis .selected{
background-color: cyan;
}
此时页面效果为:

接下来实现JavaScript部分的功能。
1、当鼠标经过轮播图模块,左右按钮显示,离开隐藏左右按钮。
首先通过display:none初始隐藏两个箭头按钮。获取左右按钮及大容器元素,并为大容器添加mouseenter和mouseleave事件监听。代码如下:
var left = document.querySelector('.left');
var right = document.querySelector('.right');
var box = document.querySelector('.box');
box.addEventListener('mouseenter',function(){
left.style.display = 'block';
right.style.display = 'block';
})
box.addEventListener('mouseleave',function(){
left.style.display = 'none';
right.style.display = 'none';
})
实现效果为:

2、动态生成小圆圈
先删除所有静态的小圆圈li元素,如图所示:

小圆圈的数量应与图片数量一致。核心思路是获取ul内li的数量(即图片张数),通过循环动态创建li节点(createElement('li'))并插入到ul中(appendChild)。注意,第一个小圆圈需添加selected类。
实现代码为:
var pic = document.querySelector('.pic');
var lis = document.querySelector('.lis');
for(var i = 0;i<pic.children.length;i++){
var li = document.createElement('li');
lis.appendChild(li);
}
lis.children[0].className = 'selected';
实现效果为:

3、点击小圆圈,小圆圈变色
在生成小li的同时,利用排他思想为其添加点击事件。点击某个小圆圈时,使其添加selected类,其余小圆圈移除selected类。
代码为:
li.addEventListener('click',function(){
for(var i =0;i<lis.children.length;i++){
lis.childern[i].className = '';
}
this.className = 'selected';
})
实现效果为:

4、点击小圆圈滚动图片
此处使用之前封装好的动画函数,需确保index.js中引用该动画函数的js文件位于上方。由于动画函数仅适用于定位元素,需给pic对应的ul添加定位。根据规律,ul滚动的距离等于小圆圈索引号乘以图片宽度(向左滚动为负值)。因此,需在生成小圆圈时设置自定义属性以获取索引号。
先在生成li的时候设置索引:
li.setAttribute('index',i);
然后在给li添加点击事件的时候获得索引,并得到图片宽度,调用动画函数:
li.addEventListener('click',function(){
var boxWidth = box.offsetWidth;
//获得索引号
var index = this.getAttribute('index');
for(var i = 0;i<lis.children.length;i++){
lis.children[i].className = '';
}
this.className = 'selected';
animate(pic,-index*boxWidth)
})
实现效果为:

5、点击右侧按钮一次,就让图片滚动一张。
可直接给右侧按钮添加点击事件,声明变量num,每次点击自增1,滚动距离为num乘以图片宽度。
var num = 0;
right.addEventListener('click',function(){
num++;
animate(pic,-num*boxWidth);
})
实现效果为:

点击右侧按钮切换图片时,若到达最后一张会显示初始背景,影响美观。通过图片无缝滚动原理解决:将.pic列表中的第一个li通过cloneNode(true)克隆并追加到列表末尾。在js中给num添加判断,当num等于列表元素个数减1时,将ul移动距离重置为0,并将num设为0。
代码为:
var first = pic.children[0].cloneNode(true);
pic.appendChild(first);
//右侧按钮的功能
var num = 0;
right.addEventListener('click',function(){
if(num == pic.children.length-1){
pic.style.left = 0;
num = 0;
}
num++;
animate(pic,-num*boxWidth);
})
实现效果为:

成功实现。
6、点击右侧按钮, 小圆圈跟随变化
需声明全局变量circle,每次点击右侧按钮时自增1。因左侧按钮也需此效果,故设为全局变量。图片有5张,小圆圈仅4个,需添加判断:若circle等于4,则重置为0。
var num = 0;
var circle = 0;
right.addEventListener('click',function(){
if(num == pic.children.length-1){
pic.style.left = 0;
num = 0;
}
num++;
animate(pic,-num*boxWidth);
circle++;
if(circle == lis.children.length){
circle = 0;
}
for(var i =0;i<lis.children.length;i++){
lis.children[i].className = '';
}
lis.children[circle].className = 'selected';
})
运行结果为:

但是这样还是有点小问题的,当我们让小圆圈和右侧按钮结合起来使用的时候,就会发现错误,如下:

当我们点击小圆点时,也可以跳转到相应的图片页,但当我们继续点击右侧按钮时,就会发现下面的小圆点和对应的图片不对应了。这是我们我们的右侧按钮点击事件时通过变量num来控制的,而num变量和小圆圈的点击事件没有任何关系,所以就存在差异。
我们的解决方案就是每次点击小li时,获得它的索引号,然后把num和circle的值改为得到的索引号的值,代码为:
var index = this.getAttribute('index');
num = index;
circle = index;
实现效果为:

7、左侧按钮功能制作
和右侧按钮做法类似,不在赘述,代码如下:
left.addEventListener('click',function(){
if(num == 0){
num = pic.children.length-1;
pic.style.left = -num*boxWidth+'px';
}
num--;
animate(pic,-num*boxWidth);
circle--;
if(circle <0){
circle = lis.children.length-1;
}
for(var i =0;i<lis.children.length;i++){
lis.children[i].className = '';
}
lis.children[circle].className = 'selected';
})
实现效果为:

8、自动播放功能
添加定时器实现自动播放,逻辑类似于点击右侧按钮,此时使用手动调用右侧按钮点击事件right.click()。鼠标经过图片区域停止定时器,离开则开启定时器。
var timer = this.setInterval(function(){
right.click();
},2000)
实现效果为:

完整index.js的代码为:
window.addEventListener('load',function(){
var left = document.querySelector('.left');
var right = document.querySelector('.right');
var box = document.querySelector('.box');
box.addEventListener('mouseenter',function(){
left.style.display = 'block';
right.style.display = 'block';
})
box.addEventListener('mouseleave',function(){
left.style.display = 'none';
right.style.display = 'none';
})
var pic = document.querySelector('.pic');
var lis = document.querySelector('.lis');
var boxWidth = box.offsetWidth;
for(var i = 0;i<pic.children.length;i++){
var li = document.createElement('li');
lis.appendChild(li);
//设置索引号
li.setAttribute('index',i);
li.addEventListener('click',function(){
//获得索引号
var index = this.getAttribute('index');
num = index;
circle = index;
for(var i = 0;i<lis.children.length;i++){
lis.children[i].className = '';
}
this.className = 'selected';
animate(pic,-index*boxWidth)
})
}
lis.children[0].className = 'selected';
//克隆第一个li
var first = pic.children[0].cloneNode(true);
pic.appendChild(first);
var num = 0;
var circle = 0;
//右侧按钮的功能
right.addEventListener('click',function(){
if(num == pic.children.length-1){
pic.style.left = 0;
num = 0;
}
num++;
animate(pic,-num*boxWidth);
circle++;
if(circle == lis.children.length){
circle = 0;
}
for(var i =0;i<lis.children.length;i++){
lis.children[i].className = '';
}
lis.children[circle].className = 'selected';
})
//左侧按钮功能
left.addEventListener('click',function(){
if(num == 0){
num = pic.children.length-1;
pic.style.left = -num*boxWidth+'px';
}
num--;
animate(pic,-num*boxWidth);
circle--;
if(circle <0){
circle = lis.children.length-1;
}
for(var i =0;i<lis.children.length;i++){
lis.children[i].className = '';
}
lis.children[circle].className = 'selected';
})
var timer = this.setInterval(function(){
right.click();
},2000)
})
至此,我们轮播图的全部功能都实现了,你学会了吗?
以上就是JavaScript实现六种网页图片轮播效果详解的详细内容,更多关于JavaScript网页图片轮播的资料请关注本站其它相关文章!
移动端 H5 页面怎么用 JS 模拟关闭当前窗口效果
2026-11-05 18:17:11
D3.js 处理大规模数据点的性能优化策略
2026-11-05 15:37:25
JS实现网页摄像头实时扫码:结合getUserMedia与jsQR教程
2026-11-05 15:27:28
D3.js v5 迁移到 v7 的 API 变更与注意事项
2026-11-05 11:57:17
使用 animate.js 实现多元素协同的序列动画
2026-11-04 22:27:53
