html css js 手写简易轮播图
源码:https://github.com/pine007/source-codes/blob/main/html-css-js-手写简易轮播图.zip
1、效果
2、编码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>轮播图</title>
<style>
.about-ber {
width: 100%;
height: 800px;
background: #FFFFFF;
text-align: center;
padding-top: 40px;
overflow-x: hidden;
}
.about-ber-txt p span {
font-family: LinotypeDidot, serif;
font-size: 36px;
line-height: 60px;
font-weight: 400;
color: #2D302A;
}
.about-ber-txt p {
font-size: 20px;
line-height: 14px;
color: #8A8C88;
}
.about-ber-carousels {
position: relative;
}
.about-ber-carousel {
position: absolute;
transition: transform 0.35s;
top: 60px;
width: 50%;
background-color: #EFF0E9
}
.about-ber-carousel1 {
transform: translate3d(-100%, 0, 0) scale(0.81);
opacity: 0;
z-index: 1;
}
.about-ber-carousel2 {
transform: translate3d(-46%, 0, 0) scale(0.81);
z-index: 2;
}
.about-ber-carousel3 {
transform: translate3d(50%, 0, 0) scale(0.81);
top: 0;
}
.about-ber-carousel4 {
transform: translate3d(146%, 0, 0) scale(0.81);
z-index: 2;
}
.about-ber-carousel5 {
transform: translate3d(200%, 0, 0) scale(0.81);
opacity: 0;
z-index: 1;
}
.about-ber-carousel-img {
float: left;
width: 50%;
max-width: 440px;
}
.about-ber-carousel-img img {
width: 100%;
vertical-align: bottom;
}
.about-ber-carousel-txt {
float: left;
width: 50%;
max-width: 440px;
text-align: center;
}
.about-ber-carousel-txt img {
display: block;
margin: 30px 20px;
width: 18px;
height: 18px;
}
.about-ber-btn {
position: absolute;
top: 500px;
left: 48.5%;
z-index: 3;
}
.about-ber-btn img {
width: 28px;
cursor: pointer;
}
</style>
</head>
<body>
<div class="about-ber">
<div class="about-ber-txt">
<p><span># Berladies</span></p>
<p>Join the journey and tag or follow #berladies on Instagram!</p>
<p>Share confidence, encouragement, and empowerment with other ladies just like you and your feedback to
help us improve our products together.</p>
</div>
<div class="about-ber-carousels">
<div class="about-ber-carousel about-ber-carousel1">
<div>
<div class="about-ber-carousel-img">
<img src="./images/v1.jpg" alt=""/>
</div>
<div class="about-ber-carousel-txt">
<p><span>About Sustainability p1</span></p>
<p>
We believe offer a wide range of styles and trends aren't everything, but above all make
environmental
progress for a better tomorrow.
</p>
</div>
</div>
</div>
<div class="about-ber-carousel about-ber-carousel2">
<div>
<div class="about-ber-carousel-img">
<img src="./images/v2.jpg" alt=""/>
</div>
<div class="about-ber-carousel-txt">
<p><span>About Sustainability p2</span>
<p>
We believe offer a wide range of styles and trends aren't everything, but above all make
environmental
progress for a better tomorrow.
</p>
</div>
</div>
</div>
<div class="about-ber-carousel about-ber-carousel3">
<div>
<div class="about-ber-carousel-img">
<img src="./images/v3.jpg" alt=""/>
</div>
<div class="about-ber-carousel-txt">
<p><span>About Sustainability p3</span></p>
<p>
We believe offer a wide range of styles and trends aren't everything, but above all make
environmental
progress for a better tomorrow.
</p>
</div>
</div>
</div>
<div class="about-ber-carousel about-ber-carousel4">
<div>
<div class="about-ber-carousel-img">
<img src="./images/v4.jpg" alt=""/>
</div>
<div class="about-ber-carousel-txt">
<p><span>About Sustainability p4</span></p>
<p>
We believe offer a wide range of styles and trends aren't everything, but above all make
environmental
progress for a better tomorrow.
</p>
</div>
</div>
</div>
<div class="about-ber-carousel about-ber-carousel5">
<div>
<div class="about-ber-carousel-img">
<img src="./images/v5.jpg" alt=""/>
</div>
<div class="about-ber-carousel-txt">
<p><span>About Sustainability p5</span></p>
<p>
We believe offer a wide range of styles and trends aren't everything, but above all make
environmental
progress for a better tomorrow.
</p>
</div>
</div>
</div>
<div class="about-ber-btn">
<img id="prev" src="./images/arrow-l.png" alt="" onclick="prevImg()"/>
<img id="next" src="./images/arrow-r.png" alt="" onclick="nextImg()"/>
</div>
</div>
</div>
<script>
//--轮播图点击事件
let carousel = 'about-ber-carousel';
let carouselR = ['about-ber-carousel1', 'about-ber-carousel2', 'about-ber-carousel3', 'about-ber-carousel4', 'about-ber-carousel5'];
//上一张
function prevImg() {
carouselR.push(carouselR[0]);
carouselR.shift();
let lis = document.getElementsByClassName(carousel)
for (let i = 0; i < lis.length; i++) {
lis[i].setAttribute('class', carousel + ' ' + carouselR[i])
}
}
//下一张
function nextImg() {
carouselR.unshift(carouselR[4])
carouselR.pop()
let lis = document.getElementsByClassName(carousel)
for (let i = 0; i < lis.length; i++) {
lis[i].setAttribute('class', carousel + ' ' + carouselR[i])
}
}
</script>
</body>
</html>