透明版的轮播图
<!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;
box-sizing: border-box;
}
.box {
width: 400px;
height: 300px;
margin: 40px auto;
position: relative;
/* overflow: hidden; */
background-color: #ddd;
}
img {
width: 400px;
height: 300px;
}
.box ul li {
width: 400px;
height: 300px;
text-align: center;
line-height: 300px;
font-size: 40px;
font-weight: bold;
border: 1px solid #000;
position: absolute;
top: 0;
opacity: 0;
transition: 2s;
}
.box ul .on {
opacity: 1;
}
.box ol {
position: absolute;
bottom: 0;
display: flex;
justify-content: center;
}
.box ol li {
width: 20px;
height: 20px;
border-radius: 50%;
border: 1px solid #f00;
margin: 10px;
transition: 2s;
}
.on {
background-color: #f00;
}
p {
position: absolute;
width: 30px;
height: 30px;
top: 0;
bottom: 0;
margin: auto;
background-color: pink;
}
#prev {
left: 0;
}
#next {
right: 0;
}
</style>
</head>
<body>
<div class="box">
<ul>
<li class="on">
<img src="./images/1.jpg" alt="" />
</li>
<li>
<img src="./images/2.jpg" alt="" />
</li>
<li>
<img src="./images/3.jpg" alt="" />
</li>
<li>
<img src="./images/4.jpg" alt="" />
</li>
</ul>
<p id="prev"></p>
<p id="next"></p>
<ol>
<li class="on"></li>
<li></li>
<li></li>
<li></li>
</ol>
</div>
<script>
var t,
index = 0;
var flag = true;
var oUlLis = document.querySelectorAll("ul li");
var oOlLis = document.querySelectorAll("ol li");
var oBox = document.querySelector(".box");
var prev = document.querySelector("#prev");
var next = document.querySelector("#next");
autoplay();
// window.onfocus=()=>{
// autoplay()
// }
// window.onblur=()=>{
// clearInterval(t)
// }
next.onmouseover = function () {
clearInterval(t);
};
prev.onmouseover = function () {
clearInterval(t);
console.log(1);
};
oBox.onmouseover = function () {
clearInterval(t);
};
oBox.onmouseout = function () {
autoplay();
};
//上一页按钮
prev.onclick = function () {
clearInterval(t);
index--;
if (index < 0) {
index = oUlLis.length - 1;
}
showOne();
autoplay();
};
//下一页按钮
next.onclick = function () {
clearInterval(t);
index++;
if (index ==oUlLis.length) {
index = 0;
}
showOne();
autoplay();
};
//自动播放,定时器t要定义一个全局变量
function autoplay() {
clearInterval(t);
t = setInterval(() => {
index++;
if (index == oUlLis.length) {
index = 0;
}
showOne();
}, 2000);
}
//表示的是小圆圈的颜色的切换,遍历数组ol
function showOne() {
oUlLis.forEach((val, i) => {
val.classList.remove("on");
oOlLis[i].classList.remove("on");
});
oUlLis[index].classList.add("on");
oOlLis[index].classList.add("on");
}
</script>
</body>
</html>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通