<!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>轮播</title>
<style>
.slideshow-container {
max-width: 800px;
position: relative;
margin: auto;
}
.mySlides {
display: none;
width: 100%;
height: 300px;
background-color: #717171;
}
.prev,
.next {
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
padding: 16px;
margin-top: -22px;
color: white;
font-weight: bold;
font-size: 18px;
transition: 0.6s ease;
border-radius: 0 3px 3px 0;
user-select: none;
}
.next {
right: 0;
border-radius: 3px 0 0 3px;
}
.prev:hover,
.next:hover {
background-color: rgba(0, 0, 0, 0.8);
}
.dot-container {
text-align: center;
padding: 20px;
background: #ddd;
}
.dot {
cursor: pointer;
height: 15px;
width: 15px;
margin: 0 2px;
background-color: #bbb;
border-radius: 50%;
display: inline-block;
transition: background-color 0.6s ease;
}
.active,
.dot:hover {
background-color: #717171;
}
.fade {
animation-name: fade;
animation-duration: 1.5s;
}
@keyframes fade {
from {
opacity: 0.4;
}
to {
opacity: 1;
}
}
</style>
</head>
<body>
<div class="slideshow-container">
<div class="mySlides fade">
<p>aaaaaa</p>
</div>
<div class="mySlides fade">
<p>bbbbbbbbb</p>
</div>
<div class="mySlides fade">
<p>ccccccccc</p>
</div>
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
</div>
<div class="dot-container">
<div class="dot" onclick="currentSlide(1)"></div>
<div class="dot" onclick="currentSlide(2)"></div>
<div class="dot" onclick="currentSlide(3)"></div>
</div>
</body>
<script>
let slideIndex = 1
showSlides(slideIndex)
// 箭头切换
function plusSlides(n) {
showSlides((slideIndex += n))
}
// 圆点切换
function currentSlide(n) {
showSlides((slideIndex = n))
}
function showSlides(n) {
let i
const slides = document.getElementsByClassName('mySlides')
const dots = document.getElementsByClassName('dot')
if (n > slides.length) {
slideIndex = 1
}
if (n < 1) {
slideIndex = slides.length
}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = 'none'
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(' active', '')
}
slides[slideIndex - 1].style.display = 'block'
dots[slideIndex - 1].className += ' active'
}
// 自动播放
let autoIndex = 0
function autoSlides() {
let i
const slides = document.getElementsByClassName('mySlides')
const dots = document.getElementsByClassName('dot')
for (i = 0; i < slides.length; i++) {
slides[i].style.display = 'none'
}
autoIndex++
if (autoIndex > slides.length) {
autoIndex = 1
}
slides[autoIndex - 1].style.display = 'block'
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(' active', '')
}
dots[autoIndex - 1].className += ' active'
setTimeout(autoSlides, 2000)
}
autoSlides()
</script>
</html>
js轮播图
于 2023-02-24 19:45:47 首次发布