1.html
DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>无缝轮播图title>
<link rel="stylesheet" href="./index.css" />
head>
<body>
<div class="carousel-container">
<div class="carousel-list">
<div class="carousel-item">
<a href="#"><img src="./images/1.jpg" alt="" />a>
div>
<div class="carousel-item">
<a href="#"><img src="./images/2.jpg" alt="" />a>
div>
<div class="carousel-item">
<a href="#"><img src="./images/3.jpg" alt="" />a>
div>
div>
<div class="indicator">
<span class="active">span>
<span>span>
<span>span>
div>
<div class="arrow-left"><div>
<div class="arrow-right">>div>
div>
<script src="./index.js">script>
body>
html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
2.css
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.carousel-container {
width: 500px;
height: 300px;
margin: 50px auto;
position: relative;
overflow: hidden;
outline: 5px solid;
}
.carousel-container .carousel-list {
width: 100%;
height: 100%;
display: flex;
transition: all 0.5s;
}
.carousel-container .carousel-list img {
width: 500px;
height: 300px;
object-fit: cover;
}
.indicator {
display: flex;
position: absolute;
bottom: 10px;
left: 50%;
transform: translateX(-50%);
}
.indicator span {
width: 10px;
height: 10px;
border: 1px solid #fff;
border-radius: 50%;
margin: 0 3px;
cursor: pointer;
}
.indicator .active {
background-color: #fff;
border: 1px solid #fff;
}
.arrow-left {
position: absolute;
top: 50%;
left: 5px;
transform: translateY(-50%);
width: 50px;
height: 50px;
background-color: #000;
color: #fff;
opacity: 0.4;
font-size: 26px;
text-align: center;
line-height: 50px;
cursor: pointer;
user-select: none;
transition: 0.5s;
border-radius: 50%;
}
.arrow-right {
position: absolute;
top: 50%;
right: 5px;
transform: translateY(-50%);
width: 50px;
height: 50px;
background-color: #000;
color: #fff;
opacity: 0.4;
font-size: 26px;
text-align: center;
line-height: 50px;
cursor: pointer;
user-select: none;
transition: 0.5s;
border-radius: 50%;
}
.arrow-left:hover,
.arrow-right:hover {
opacity: 0.7;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
3.js
const doms = {
arrowLeft: document.querySelector(".arrow-left"),
arrowRight: document.querySelector(".arrow-right"),
carousel: document.querySelector(".carousel-list"),
indicators: document.querySelectorAll(".indicator span"),
};
let curIndex = 0;
function moveTo(index) {
doms.carousel.style.transform = `translateX(-${index}00%)`;
const active = document.querySelector(".indicator span.active");
active.classList.remove("active");
doms.indicators[index].classList.add("active");
curIndex = index;
}
doms.indicators.forEach(function (item, i) {
item.onclick = function () {
moveTo(i);
};
});
function init() {
const first = doms.carousel.firstElementChild.cloneNode(true);
const last = doms.carousel.lastElementChild.cloneNode(true);
doms.carousel.appendChild(first);
doms.carousel.insertBefore(last, doms.carousel.firstElementChild);
last.style.position = "absolute";
last.style.transform = "translateX(-100%)";
}
init();
let count = doms.indicators.length;
doms.arrowRight.addEventListener("click", rightNext);
doms.arrowLeft.addEventListener("click", leftNext);
function leftNext() {
if (curIndex === 0) {
doms.carousel.style.transform = `translateX(-${count}00%)`;
doms.carousel.style.transition = "none";
doms.carousel.clientHeight;
doms.carousel.style.transition = "all 0.5s";
moveTo(count - 1);
} else {
moveTo(curIndex - 1);
}
}
function rightNext() {
if (curIndex === count - 1) {
doms.carousel.style.transform = "translateX(100%)";
doms.carousel.style.transition = "none";
doms.carousel.clientHeight;
doms.carousel.style.transition = "all 0.5s";
moveTo(0);
} else {
moveTo(curIndex + 1);
}
}
let timer = setInterval(rightNext, 3000);
doms.carousel.addEventListener("mouseenter", function () {
clearInterval(timer);
});
doms.carousel.addEventListener("mouseleave", function () {
timer = setInterval(rightNext, 3000);
});
Ï
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93