<!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>轮播图</title>
<style>
* {
margin: 0;
padding: 0;
}
.bg {
width: 1280px;
height: 850px;
margin: 0px auto;
background-color: orange;
position: relative;
}
.scroll {
width: inherit;
height: inherit;
display: inline-block;
}
.dots {
cursor: pointer;
position: absolute;
bottom: 0%;
left: 50%;
transform: translate(-50%, 0);
display: flex;
justify-content: center;
align-items: flex-end;
width: 200px;
height: 60px;
}
.dots span {
display: inline-block;
margin-left: 10px;
margin-bottom: 10px;
width: 10px;
height: 10px;
border: 2px solid #fff;
border-radius: 100%;
background-color: rgba(255, 255, 255, 0.2);
}
.left,
.right {
user-select: none;
color: #c7c7c7;
text-align: center;
position: absolute;
top: 50%;
transform: translate(0, -50%);
font-size: 3em;
display: inline-block;
width: 150px;
height: 100px;
line-height: 100px;
background-color: #fff;
opacity: .5;
}
.left:hover,
.right:hover {
background-color: #f2f2f2;
}
.left {
right: 0px;
}
.right {
left: 0px;
}
span.active {
background-color: #04AA6D;
}
.bg .scroll img:not(.current) {
display: none;
}
.current {
display: block;
float: left;
width: inherit;
height: inherit;
}
</style>
</head>
<body>
<div class="bg">
<div class="scroll">
<img class="current" src="images/1.jpg" data-id="0" alt="">
<img src="images/2.jpg" data-id="1" alt="">
<img src="images/3.jpg" data-id="2" alt="">
<img src="images/4.jpg" data-id="3" alt="">
</div>
<div class="left">></div>
<div class="right"><</div>
<div class="dots">
<span class="active" data-id="0"></span>
<span data-id="1"></span>
<span data-id="2"></span>
<span data-id="3"></span>
</div>
</div>
</body>
</html>
<script type="text/javascript">
// 轮播图的实现主要通过js操作css样,即为图片和小圆点添加激活的样式
//获取图片
const img = document.getElementsByTagName("img");
//获取左按钮
const btnL = document.getElementsByClassName("left");
//获取右按钮
const btnR = document.getElementsByClassName("right");
// 获取小圆点
const dots = document.getElementsByTagName("span");
//定义变量记录当前在播放第几张图片,默认第零张开始
let index = 0;
//定义下一张函数
function next() {
// 取消自动播放下一张
clearInterval(autoNext);
// 如果当前是最后一张图片返回第一张,或者播放下一张
if (index == 3) {
// 把index改为第一张图片的索引
index = 0;
//调用激活图片函数
setActive(index);
} else {
// 把index改为下一张图片的索引
index++;
//调用激活图片函数
setActive(index);
}
//两秒中后不再手动点击下一张再开启自动播放
autoNext = setInterval(() => {
next();
}, 2000);
}
//定义上一张函数
function back() {
// 取消自动播放下一张
clearInterval(autoNext);
// 如果当前是第一张图片返回最后一张,或者播放下一张
if (index == 0) {
// 把index改为第三张图片的索引
index = 3;
//调用激活图片函数
setActive(index)
} else {
// 把index改为下一张图片的索引
index--;
//调用激活图片函数
setActive(index);
}
//两秒中后不再手动点击上一张再开启自动播放
autoNext = setInterval(() => {
next();
}, 2000);
}
//左按钮监听点击事件
btnL[0].addEventListener("click", next)
//右按钮监听点击事件
btnR[0].addEventListener("click", back)
//自动播放下一张
let autoNext = setInterval(() => {
//调用next函数播放下一张
next();
}, 2000);
//启用addDots函数
addDots();
//监听小圆点点击
function addDots(){
// 为每个小圆点添加监听事件
for (let i = 0; i < dots.length; i++) {
dots[i].addEventListener('click', () => {
//停止自动播放
clearInterval(autoNext);
// 获取点击哪一个小圆点
index = dots[i].getAttribute('data-id');
//调用激活图片函数
setActive(index);
//两秒中后无点击操作再次开启自动播放
autoNext = setInterval(() => {
next();
}, 2000);
})
}
}
//激活图片函数
function setActive(e) {
//清空当前激活的图片,为了方便所以遍历清空每一个图片和小圆点的active样式和current样式
for (let i = 0; i < dots.length; i++) {
//去掉active样式
dots[i].classList.remove('active');
// 去掉current样式
img[i].classList.remove('current');
}
// 为需要激活的图片和小圆点添加current样式和active样式
img[e].className = "current";
dots[e].className = "active";
}
</script>