猛然发现习惯了 vue,原生JS的能力直线下降,复习一波,替换图片地址后,cv可用。
<!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>
* {
padding: 0;
margin: 0;
list-style: none;
}
.banner {
width: 300px;
height: 200px;
border: 1px solid black;
margin: 50px auto;
position: relative;
cursor: pointer;
}
/* 五张图片的样式 */
.banner ul li {
position: absolute;
left: 0;
top: 0;
opacity: 0;
}
/* 五个圆圈的样式 */
.banner ul li.show {
opacity: 1;
}
.banner ol {
position: absolute;
left: 50px;
bottom: 10px;
display: flex;
}
.banner ol li {
width: 20px;
height: 20px;
background-color: blue;
border-radius: 50%;
margin-left: 15px;
cursor: pointer;
text-indent: -999em; /*text-indent 属性规定文本块中首行文本的缩进。*/
}
/* 给轮到的按钮不一样的颜色 */
.banner ol li.active {
background-color: orange;
}
/* 左右按钮 */
.left,
.right {
position: absolute;
text-decoration: none;
font-size: 50px;
color: #fff;
top: 30%;
display: none;
}
.left {
left: 20px;
}
.right {
right: 20px;
}
img {
width: 300px;
height: 200px;
}
</style>
</head>
<body>
<div class="banner">
<!-- 主体图片 -->
<ul>
<li class="show">
<img src="img/img1.jpeg" alt="" />
</li>
<li>
<img src="img/img2.webp" alt="" />
</li>
<li>
<img src="img/img3.webp" alt="" />
</li>
<li>
<img src="img/img4.jpeg" alt="" />
</li>
<li>
<img src="img/img5.jpeg" alt="" />
</li>
</ul>
<!-- 5个圆圈 -->
<ol>
<li class="active">1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ol>
<!-- 左右箭头 -->
<a href="#" class="left"><</a>
<a href="#" class="right">></a>
</div>
</body>
</html>
<script>
// 1.获取元素
const banner = document.querySelector(".banner");
const left = document.querySelector(".left");
const right = document.querySelector(".right");
const picLi = document.querySelectorAll(".banner ul li"); //获取全部的图片
const btnLi = document.querySelectorAll(".banner ol li"); //获取底部的五个圈
let timer = null;
// 2.利用索引进行匹配操作
let index = 0; //定义一个index用来存放索引
for (let i = 0; i < btnLi.length; i++) {
// (1)给圆圈添加事件
btnLi[i].onclick = function () {
index = i;
// 动态改变圆圈的类名
for (let j = 0; j < btnLi.length; j++) {
btnLi[j].className = "";
picLi[j].className = "";
}
// 点击的圆圈添加类名
btnLi[index].className = "active";
picLi[index].className = "show";
};
}
// (2)鼠标移入出现箭头,移出箭头消失
banner.onmouseover = function () {
left.style.display = "block";
right.style.display = "block";
//停止定时器
// clearInterval(timer);
};
banner.onmouseout = function () {
left.style.display = "none";
right.style.display = "none";
//继续定时器
};
//(3)给左右箭头添加事件
right.onclick = function () {
index++;
if (index > btnLi.length - 1) {
index = 0;
}
for (let j = 0; j < btnLi.length; j++) {
btnLi[j].className = "";
picLi[j].className = "";
}
btnLi[index].className = "active";
picLi[index].className = "show";
};
left.onclick = function () {
index--;
if (index < 0) {
index = btnLi.length - 1;
}
for (let j = 0; j < btnLi.length; j++) {
btnLi[j].className = "";
picLi[j].className = "";
}
btnLi[index].className = "active";
picLi[index].className = "show";
};
// (4)添加自动轮播,每个3s自动触发right事件
timer = setInterval(function () {
right.onclick();
}, 3000);
</script>