First show the actual effect:
The old rule, go directly to the code:
<!DOCTYPE html>
<html>
<head>
<style>
/* CSS样式代码 */
.slider-container {
position: relative;
width: 600px;
/* 设置轮播图容器宽度 */
height: 400px;
/* 设置轮播图容器高度 */
overflow: hidden;
/* 隐藏超出容器的部分 */
margin: 0 auto;
}
.slider {
display: flex;
transition: transform 0.3s ease-in-out;
/* 添加过渡效果 */
}
.slider-item {
flex-shrink: 0;
width: 100%;
/* 设置每张图片的宽度 */
height: 100%;
/* 设置每张图片的高度 */
}
.slider-item img {
width: 100%;
/* 设置每张图片的宽度 */
height: 100%;
/* 设置每张图片的高度 */
}
.slider-controls {
position: absolute;
top: 50%;
transform: translateY(-50%);
display: flex;
justify-content: space-between;
align-items: center;
width: 100%;
z-index: 1;
/* 将导航按钮置于轮播图上方 */
}
.slider-controls button {
background-color: #333;
color: #fff;
border: none;
padding: 5px 10px;
margin: 0 5px;
cursor: pointer;
}
.slider-controls .prev-btn::before,
.slider-controls .next-btn::after {
content: "";
display: inline-block;
width: 10px;
height: 10px;
border-top: 2px solid #fff;
border-right: 2px solid #fff;
transform: rotate(45deg);
margin-right: 5px;
}
.slider-controls .prev-btn::before {
transform: rotate(-135deg);
margin-right: 0;
margin-left: 5px;
}
</style>
</head>
<body>
<div class="slider-container">
<div class="slider">
<div class="slider-item">
<img src="./img/1.jpg" alt="Image 1">
</div>
<div class="slider-item">
<img src="./img/2.jpg" alt="Image 2">
</div>
<div class="slider-item">
<img src="./img/3.jpg" alt="Image 3">
</div>
<div class="slider-item">
<img src="./img/4.jpg" alt="Image 4">
</div>
</div>
<div class="slider-controls">
<button class="prev-btn"></button>
<button class="next-btn"></button>
</div>
</div>
<script>
const sliderContainer = document.querySelector('.slider-container');
const slider = document.querySelector('.slider');
const prevBtn = document.querySelector('.prev-btn');
const nextBtn = document.querySelector('.next-btn');
const slideWidth = document.querySelector('.slider-item').offsetWidth;
let currentPosition = 0;
let isPaused = false;
// 自动轮播
const autoPlay = () => {
if (!isPaused) {
currentPosition -= slideWidth;
if (currentPosition <= -(slideWidth * (slider.children.length - 1))) {
currentPosition = 0;
}
slider.style.transform = `translateX(${currentPosition}px)`;
}
setTimeout(autoPlay, 2000);
};
// 鼠标悬停/离开事件监听器
sliderContainer.addEventListener('mouseenter', () => {
isPaused = true;
});
sliderContainer.addEventListener('mouseleave', () => {
isPaused = false;
});
// 上一页按钮点击事件监听器
prevBtn.addEventListener('click', () => {
currentPosition += slideWidth;
if (currentPosition > 0) {
currentPosition = -(slideWidth * (slider.children.length-1));
}
slider.style.transform = `translateX(${currentPosition}px)`;
});
// 下一页按钮点击事件监听器
nextBtn.addEventListener('click', () => {
currentPosition -= slideWidth;
if (currentPosition <= -(slideWidth * (slider.children.length))) {
currentPosition = 0;
}
slider.style.transform = `translateX(${currentPosition}px)`;
});
// 启动自动轮播
autoPlay();
</script>
</body>
</html>
This code implements a simple carousel function. The specific functions are as follows:
1. Automatic carousel function: Use the `autoPlay()` function to achieve the effect of automatically switching pictures. The function first checks the value of the `isPaused` variable. If it is `false`, which means there is no pause, then `currentPosition` is subtracted by the width of the image. Then check whether the last picture has been reached, and if so, reset `currentPosition` to 0 to achieve a loop playback effect. Finally, by setting the `transform` attribute of the carousel image container, use the `translateX()` function to achieve the translation animation effect of the image. Then use the `setTimeout()` method to call the `autoPlay()` function in a timed loop to implement the automatic playback function.
// 自动轮播
const autoPlay = () => {
if (!isPaused) {
currentPosition -= slideWidth;
if (currentPosition <= -(slideWidth * (slider.children.length - 1))) {
currentPosition = 0;
}
slider.style.transform = `translateX(${currentPosition}px)`;
}
setTimeout(autoPlay, 2000);
};
2.. Mouseover/leave event function: By using the `addEventListener()` method, `mouseenter` and `mouseleave` event listeners are added to the carousel container. When the mouse enters the container, set the `isPaused` variable to `true`, which means pausing the automatic carousel. When the mouse leaves the container, set the `isPaused` variable to `false` to resume automatic carousel.
// 鼠标悬停/离开事件监听器
sliderContainer.addEventListener('mouseenter', () => {
isPaused = true;
});
sliderContainer.addEventListener('mouseleave', () => {
isPaused = false;
});
3. Previous page button click event function: By using the `addEventListener()` method, a `click` event listener is added to the previous page button. When the button is clicked, increase `currentPosition` by the width of the image. Then check whether the first picture has been reached. If so, set `currentPosition` to the position of the last picture to achieve a loop switching effect. Finally, by setting the `transform` attribute of the carousel image container, use the `translateX()` function to achieve the translation animation effect of the image.
// 上一页按钮点击事件监听器
prevBtn.addEventListener('click', () => {
currentPosition += slideWidth;
if (currentPosition > 0) {
currentPosition = -(slideWidth * (slider.children.length-1));
}
slider.style.transform = `translateX(${currentPosition}px)`;
});
4. Next page button click event function: By using the `addEventListener()` method, a `click` event listener is added to the next page button. When the button is clicked, `currentPosition` is subtracted by the width of the image. Then check whether the last picture has been reached, and if so, reset `currentPosition` to 0 to achieve a loop switching effect. Finally, by setting the `transform` attribute of the carousel image container, use the `translateX()` function to achieve the translation animation effect of the image.
// 下一页按钮点击事件监听器
nextBtn.addEventListener('click', () => {
currentPosition -= slideWidth;
if (currentPosition <= -(slideWidth * (slider.children.length))) {
currentPosition = 0;
}
slider.style.transform = `translateX(${currentPosition}px)`;
});
If you find it useful, please give it a thumbs up TAT