197 lines
4.4 KiB
HTML
197 lines
4.4 KiB
HTML
<!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>
|
|
* {
|
|
margin: 0;
|
|
padding: 0;
|
|
list-style: none;
|
|
}
|
|
|
|
.swiper-container {
|
|
width: 520px;
|
|
height: 280px;
|
|
margin: 10% auto;
|
|
position: relative;
|
|
overflow: hidden;
|
|
}
|
|
|
|
.swiper-wrapper {
|
|
position: absolute;
|
|
display: flex;
|
|
transition: all 0.3s;
|
|
}
|
|
|
|
.swiper-slide {
|
|
flex-shrink: 0;
|
|
}
|
|
|
|
.swiper-slide img {
|
|
width: 100%;
|
|
height: 100%;
|
|
}
|
|
|
|
.swiper-pagination {
|
|
display: flex;
|
|
position: absolute;
|
|
bottom: 15px;
|
|
left: 50%;
|
|
transform: translateX(-50%);
|
|
}
|
|
|
|
.pagination {
|
|
width: 15px;
|
|
height: 15px;
|
|
border-radius: 100%;
|
|
background: #fff;
|
|
margin-right: 10px;
|
|
cursor: pointer;
|
|
}
|
|
|
|
.pagination:last-child {
|
|
margin-right: 0;
|
|
}
|
|
|
|
.swiper-pagination .active {
|
|
background: red !important;
|
|
}
|
|
|
|
.swiper-button-prev,
|
|
.swiper-button-next {
|
|
width: 45px;
|
|
height: 45px;
|
|
background: rgb(0 0 0 / 58%);
|
|
color: #fff;
|
|
text-align: center;
|
|
line-height: 45px;
|
|
font-size: 18px;
|
|
position: absolute;
|
|
top: 50%;
|
|
transform: translateY(-50%);
|
|
border-radius: 100%;
|
|
}
|
|
|
|
.swiper-button-prev {
|
|
left: 10px;
|
|
}
|
|
|
|
.swiper-button-next {
|
|
right: 10px;
|
|
}
|
|
</style>
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div class="swiper-container">
|
|
|
|
<div class="swiper-wrapper" style="left: 0px;">
|
|
<div class="swiper-slide">
|
|
<img src="https://gtms01.alicdn.com/tps/i1/TB1r4h8JXXXXXXoXXXXvKyzTVXX-520-280.jpg" alt="">
|
|
</div>
|
|
<div class="swiper-slide">
|
|
<img src="https://gtms03.alicdn.com/tps/i3/TB1gXd1JXXXXXapXpXXvKyzTVXX-520-280.jpg" alt="">
|
|
</div>
|
|
<div class="swiper-slide">
|
|
<img src="https://img.alicdn.com/imgextra/i2/6000000004940/O1CN01GJOBZe1mMZHhWNN9e_!!6000000004940-0-octopus.jpg"
|
|
alt="">
|
|
</div>
|
|
</div>
|
|
|
|
<!-- 如果需要分页器 -->
|
|
<div class="swiper-pagination">
|
|
<div class="pagination active"></div>
|
|
<div class="pagination"></div>
|
|
<div class="pagination"></div>
|
|
</div>
|
|
|
|
<!-- 如果需要导航按钮 -->
|
|
<div class="swiper-button-prev"><</div>
|
|
<div class="swiper-button-next">></div>
|
|
|
|
</div>
|
|
|
|
</body>
|
|
|
|
<script>
|
|
|
|
var timer;
|
|
|
|
$(".swiper-button-next").onclick = () => {
|
|
motion(-520)
|
|
}
|
|
|
|
$(".swiper-button-prev").onclick = () => {
|
|
motion(520)
|
|
}
|
|
|
|
_(".swiper-pagination .pagination").forEach((v, i) => {
|
|
v.onclick = () => {
|
|
changePagination(i)
|
|
setOffset(i * -520)
|
|
}
|
|
})
|
|
|
|
|
|
|
|
function motion(offset) {
|
|
$(".swiper-wrapper").left()
|
|
var totalOffset = parseInt($(".swiper-wrapper").style.left) + offset;
|
|
if (totalOffset > 0) {
|
|
totalOffset = -1040
|
|
}
|
|
if (totalOffset < -1040) {
|
|
totalOffset = 0
|
|
}
|
|
|
|
changePagination(totalOffset / 520 * -1)
|
|
setOffset(totalOffset)
|
|
}
|
|
|
|
function changePagination(index) {
|
|
var pagination = _(".swiper-pagination .pagination")
|
|
pagination.forEach(v => {
|
|
v.classList.remove("active")
|
|
})
|
|
pagination[index].classList.add("active");
|
|
}
|
|
|
|
function autoPlay() {
|
|
timer = setInterval(() => {
|
|
motion(-520)
|
|
}, 3000)
|
|
}
|
|
|
|
autoPlay()
|
|
|
|
|
|
|
|
$(".swiper-container").onmouseout = () => {
|
|
autoPlay()
|
|
}
|
|
|
|
$(".swiper-container").onmouseover = () => {
|
|
clearInterval(timer)
|
|
}
|
|
|
|
function setOffset(offset) {
|
|
$(".swiper-wrapper").style.left = `${offset}px`
|
|
}
|
|
|
|
function $(className) {
|
|
return document.querySelector(className)
|
|
}
|
|
|
|
function _(className) {
|
|
return document.querySelectorAll(className)
|
|
}
|
|
|
|
|
|
</script>
|
|
|
|
</html> |