用html和css javascript实现汽车图片轮播功能
时间: 2023-07-29 08:07:30 浏览: 138
javascript实现图片轮播效果
以下是一个简单的汽车图片轮播的HTML/CSS/JavaScript代码示例:
```
<!DOCTYPE html>
<html>
<head>
<title>汽车图片轮播</title>
<style>
.container {
position: relative;
width: 800px;
height: 500px;
margin: 0 auto;
overflow: hidden;
}
.slides {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: flex;
transition: transform 0.5s ease-in-out;
}
.slide {
flex: 1;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.slide img {
max-width: 100%;
max-height: 100%;
}
.controls {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
display: flex;
align-items: center;
}
.control {
cursor: pointer;
margin: 0 10px;
font-size: 30px;
color: #ccc;
}
.control.active {
color: #000;
}
</style>
</head>
<body>
<div class="container">
<div class="slides">
<div class="slide">
<img src="car1.jpg" alt="Car 1">
</div>
<div class="slide">
<img src="car2.jpg" alt="Car 2">
</div>
<div class="slide">
<img src="car3.jpg" alt="Car 3">
</div>
<div class="slide">
<img src="car4.jpg" alt="Car 4">
</div>
</div>
<div class="controls">
<div class="control prev">❮</div>
<div class="control next">❯</div>
</div>
</div>
<script>
var slides = document.querySelector('.slides');
var slide = document.querySelectorAll('.slide');
var prev = document.querySelector('.prev');
var next = document.querySelector('.next');
var index = 0;
prev.addEventListener('click', function() {
index--;
if (index < 0) {
index = slide.length - 1;
}
updateSlide();
});
next.addEventListener('click', function() {
index++;
if (index > slide.length - 1) {
index = 0;
}
updateSlide();
});
function updateSlide() {
slides.style.transform = 'translateX(-' + index * 100 + '%)';
for (var i = 0; i < slide.length; i++) {
slide[i].classList.remove('active');
}
slide[index].classList.add('active');
}
</script>
</body>
</html>
```
上述代码中,使用CSS实现了容器和轮播图的样式,包括容器的大小、轮播图的位置、轮播图的过渡效果等。JavaScript代码实现了轮播图的切换功能,通过对上一页和下一页按钮的点击事件来更新轮播图的位置。
阅读全文