<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
img{
}
*{
padding: 0;
margin: 0;
}
.main{
height: 100vh;
width: 100vw;
overflow: hidden;
display: flex;
align-items: center;
justify-content: center;
}
.content{
height: 700px;
width: 400px;
overflow: hidden;
position: relative;
}
.content .banner-list{
height: 100%;
width: 300%;
display: flex;
position: absolute;
left: 0;
}
.content .banner-list .banner-item{
height: 100%;
width: 400px;
}
.content .banner-list .banner-item a{
display: block;
}
.content .banner-list .banner-item a img{
display: block;
height: 100%;
width: 100%;
}
.opt{
height: 100%;
width: 100%;
position: absolute;
left: 0;
top: 0;
}
.opt .allow{
height: 50px;
width: 50px;
position: absolute;
top: 275px;
font-size: 26px;
color: white;
display: none;
align-items: center;
justify-content: center;
background-color: rgba(0,0,0,0.3);
cursor: pointer;
}
.opt .left{
left: 0;
}
.opt .right{
right: 0;
}
.opt .dots{
height: 20px;
width: 150px;
background-color: rgba(0,0,0,0.4);
position: absolute;
bottom: 200px;
left: 130px;
border-radius: 10px;
display: flex;
align-items: center;
justify-content: space-around;
padding: 0 5px;
}
.opt .dots .dot {
height: 16px;
width: 16px;
border-radius: 50%;
background-color: gray;
}
.opt .dots .dot.active {
height:15px;
width: 15px;
border:1px solid black;
background-color: red;
}
</style>
</head>
<body>
<div class="main">
<div class="content">
<div class="banner-list">
<div class="banner-item">
<a href="https://www.baidu.com" target="_blank">
<img src="./images/0%7DRQEE1DZ)ZU6NQU8@14%7B8B.jpg" alt="">
</a>
</div>
<div class="banner-item">
<a href="https://www.zhihu.com" target="_blank">
<img src="./images/%601Z%5B9(G_6YZJDK0Q(77L@F0.jpg" alt="">
</a>
</div>
<div class="banner-item">
<a href="https://www.mi.com" target="_blank">
<img src="./images/X%7DQ9%7D%7D@9USXRWKHG%60Q7DLYX.jpg" alt="">
</a>
</div>
</div>
<div class="opt">
<div class="allow left"><</div>
<div class="allow right">></div>
<div class="dots">
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
</div>
</div>
</div>
</div>
</body>
<script>
const itemCount = document.getElementsByClassName("banner-item").length;
const itemWidth = document.querySelector(".banner-item").clientWidth;
let index=0;
const contentDiv = document.querySelector(".content")
const allows = document.getElementsByClassName("allow")
let interval = null;
contentDiv.onmouseover=function () {
for(let all of allows){
all.style.display = "flex";
}
clearInterval(interval)
}
contentDiv.onmouseleave=function () {
for(let all of allows){
all.style.display = "none";
}
autoPlay();
}
document.querySelector(".left").onclick=function (){
if (index ===0){
index=itemCount-1;
}else{
index--;
}
setBannerMovie();
}
document.querySelector(".right").onclick=nextBanner;
function nextBanner() {
if(index === itemCount-1){
index = 0;
}else{
index++;
}
setBannerMovie();
}
function autoPlay() {
interval=setInterval(nextBanner,3000);
}
function setBannerMovie(type=1) {
const dots = document.getElementsByClassName("dot");
for(const d of dots){
d.classList.remove("active");
}
dots[index].classList.add("active");
let left = itemWidth*index*(-1);
document.querySelector(".banner-list").style.left=left===0?left : left+"px";
for (let i =0;i<dots.length;i++){
dots[i].onclick = function(){
for (let j=0;j<dots.length;j++){
dots[j].className="dot";
}
dots[i].classList.add("active");
let left = itemWidth*i*(-1);
document.querySelector(".banner-list").style.left=left===0?left : left+"px";
index = i;
}
}
}
window.onload = function (){
setBannerMovie();
autoPlay()
}
</script>
</html>