JavaScript 实现经典轮播图效果完整示例
作者:拾光时间:2026-10-22 20:47:37浏览:154
本文提供了一段使用 JavaScript 实现经典轮播图效果的完整代码示例。内容涵盖 HTML 结构、CSS 样式布局以及核心的 JS 逻辑,包括图片索引切换、自动播放与手动点击交互等功能。适合前端开发者参考学习,可直接运行测试并应用于实际项目中。
JavaScript 实现经典轮播图效果完整示例
本文详细介绍如何使用 JavaScript 实现一个经典的轮播图效果,并提供完整的代码供读者参考和借鉴。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
*{
margin:0;
padding:0;
list-style: none;
}
.box{
width: 665px;
height: 362px;
border: solid;
margin: 100px auto;
position:relative ;
}
.left{
width: 50px;
height: 50px;
border: solid white;
border-radius: 50%;
position: absolute;
line-height: 50px;
text-align: center;
font-size: 50px;
left:0px;
top:180px;
cursor: pointer;
color: white;
}
.right {
width: 50px;
height: 50px;
border: solid white;
border-radius: 50%;
position: absolute;
line-height: 50px;
text-align: center;
font-size: 50px;
right: 0px;
top: 180px;
cursor: pointer;
color: white;
}
ul{
width: 400px;
height: 50px;
margin:307px 188px;
position: absolute;
left:30px;
top:24px;
}
li{
width: 30px;
height: 30px;
border: solid 1px white;
border-radius: 50%;
float: left;
cursor: pointer;
line-height: 30px;
text-align: center;
color: white;
}
img{
display: none;
width: 665px;
height: 362px;
}
.act{display: block;
}
.active{
background: black;
}
</style>
</head>
<body>
<div class="box" id="box">
<img src="img/1.jpg" alt="" class="act">
<img src="img/2.jpg" alt="">
<img src="img/3.jpg" alt="">
<img src="img/4.jpg" alt="">
<img src="img/5.jpg" alt="">
<img src="img/6.jpg" alt="">
<img src="img/7.jpg" alt="">
<img src="img/8.jpg" alt="">
<div class="left" id="left"> < </div>
<div class="right" id="right"> > </div>
<ul>
<li class="active">1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
</ul>
</div>
<script>
window.onload=function () {
var arrLi=document.getElementsByTagName("li");
var arrImg=document.getElementsByTagName("img");
var oLeft=document.getElementById("left");
var oRight=document.getElementById("right");
var oBox=document.getElementById("box");
var num=0;
f=setInterval(abc,1000);
oBox.onmouseover=function () {
clearInterval(f)
}
oBox.onmouseout=function () {
f=setInterval(abc,1000);
}
for(x=0;x<arrLi.length;x++) {
arrLi[x].index=x;
arrLi[x].onmouseover=function () {
for(j=0;j<arrLi.length;j++){
arrLi[j].className="";
arrImg[j].className="";
}
this.className="active";
arrImg[this.index].className="act";
}
}
oLeft.onclick=function () {
num=num-1;
if(num<0){
num=arrImg.length-1
}
for(j=0;j<arrImg.length;j++){
arrImg[j].className="";
arrLi[j].className="";
}arrImg[num].className="act";
arrLi[num].className="active";
}
oRight.onclick=abc
function abc() {
num=num+1;
if(num>arrImg.length-1){
num=0
}
for(j=0;j<arrImg.length;j++){
arrImg[j].className="";
arrLi[j].className="";
}arrImg[num].className="act";
arrLi[num].className="active";
}
}
</script>
</body>
</html>
以下是本机测试运行的实际效果展示:

如果您对 JavaScript 相关技术感兴趣,还可以查阅本站的其他专题文章,例如《JavaScript图片操作技巧大全》、《JavaScript切换特效与技巧总结》、《JavaScript运动效果与技巧汇总》、《JavaScript动画特效与技巧汇总》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》以及《JavaScript数学运算用法总结》。
希望本文提供的代码示例能对您的 JavaScript 程序设计工作有所帮助。
作者最新文章
JS处理含Emoji或中文的字符串转数组乱码问题
2026-11-05 16:12:28
JS怎么使用展开运算符合并对象
2026-11-05 14:42:18
ES6+环境下判断JS对象为空的简洁写法
2026-11-05 12:12:23
怎么通过replacer函数自定义JS中JSON的格式化输出
2026-11-05 11:19:44
JS正则表达式基础:字面量与RegExp构造函数的区别及常用元字符
2026-11-05 11:16:58
