<!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>
/* 样式 */
* {
margin: 0;
padding: 0;
/* box-sizing: border-box; */
}
body {
background-color: rgba(62, 146, 146, 0.377);
}
.banner {
position: relative;
width: 1000px;
height: 400px;
border: 10px solid #fff;
border-radius: 20px;
margin: 200px auto;
box-shadow: 20px 30px 20px rgba(0, 0, 0, 0.5);
overflow: hidden;
}
.focus {
position: absolute;
left: 0;
top: 0;
width: calc(100% * 3);
height: 100%;
}
.focus .image {
float: left;
width: 1000px;
height: 400px;
background-image: url("https://picsum.photos/id/326/4928/3264");
/* background-position: center center; */
background-size: cover;
background-position: center;
}
.image:nth-child(2) {
background-image: url("https://picsum.photos/id/325/4928/3264");
}
.image:nth-child(3) {
background-image: url("https://picsum.photos/id/999/4000/2667");
}
.dots {
position: absolute;
bottom: 10px;
right: 0;
transform: translateX(-50%);
z-index: 111;
}
.dots > div {
float: left;
width: 30px;
height: 30px;
border: 5px solid #fff;
border-radius: 50%;
background-size: cover;
background-position: center;
margin: 10px;
transition: all 0.5s;
}
.dots > div:hover {
cursor: pointer;
transform: scale(1.2);
border: 5px solid rgb(216, 24, 24);
}
.dots > div:nth-child(1) {
background-image: url("https://picsum.photos/id/326/4928/3264");
}
.dots > div:nth-child(2) {
background-image: url("https://picsum.photos/id/325/4928/3264");
}
.dots > div:nth-child(3) {
background-image: url("https://picsum.photos/id/999/4000/2667");
}
.btn {
position: absolute;
width: 100%;
height: 100%;
display: flex;
justify-content: space-between;
align-items: center;
}
.btn div {
display: flex;
justify-content: center;
align-items: center;
width: 40px;
height: 100%;
font-size: 20px;
background-color: rgba(226, 223, 219, 0.411);
cursor: pointer;
}
</style>
</head>
<body>
<div class="banner">
<div class="focus">
<div class="image"></div>
<div class="image"></div>
<div class="image"></div>
</div>
<div class="dots">
<div></div>
<div></div>
<div></div>
</div>
<div class="btn">
<div class="left"><</div>
<div class="right">></div>
</div>
</div>
<script>
window.onload = () => {
// 获取小圆点
const dots = document.querySelectorAll(".dots div");
// 获取轮播图的宽度
const focus = document.querySelector(".focus");
// 索引
var index = 0;
// 定时器
var time;
// 设置偏离的位置
function position() {
console.log(-100 * index + "%");
focus.style.left = -100 * index + "%";
focus.style.transition = "all 0.5s";
}
// 右侧
function add() {
if (index >= dots.length - 1) {
index = 0;
} else {
index++;
}
}
// 左侧
function desc() {
if (index < 1) {
index = dots.length - 1;
} else {
index--;
}
}
// 定时器方法
function timer() {
time = setInterval(() => {
index++;
desc();
add();
position();
}, 3000);
}
// 小圆点点击事件
for (let i = 0; i < dots.length; i++) {
dots[i].addEventListener("click", () => {
index = i;
position();
clearInterval(time);
timer();
});
}
// 左点击
document.querySelector(".left").addEventListener("click", () => {
desc();
position();
clearInterval(time);
timer();
});
// 右点击
document.querySelector(".right").addEventListener("click", () => {
add();
position();
clearInterval(time);
timer();
});
// 调用定时器
timer();
};
</script>
</body>
</html>
常见的轮播图html,css,js
猜你喜欢
转载自blog.csdn.net/m0_51531365/article/details/124048857
今日推荐
周排行