淘宝轮播图html代码
时间: 2023-12-23 10:27:34 浏览: 150
以下是一个简单的淘宝轮播图的HTML代码示例:
```html
<!DOCTYPE html>
<html>
<head>
<title>淘宝轮播图</title>
<style>
.carousel {
position: relative;
width: 600px;
height: 400px;
overflow: hidden;
}
.carousel img {
width: 100%;
height: 100%;
}
.carousel .prev,
.carousel .next {
position: absolute;
top: 50%;
transform: translateY(-50%);
width: 50px;
height: 50px;
background-color: rgba(0, 0, 0, 0.5);
color: #fff;
text-align: center;
line-height: 50px;
cursor: pointer;
}
.carousel .prev {
left: 10px;
}
.carousel .next {
right: 10px;
}
.carousel .dots {
position: absolute;
bottom: 10px;
left: 50%;
transform: translateX(-50%);
display: flex;
}
.carousel .dot {
width: 10px;
height: 10px;
background-color: #ccc;
border-radius: 50%;
margin: 0 5px;
cursor: pointer;
}
.carousel .dot.active {
background-color: #333;
}
</style>
</head>
<body>
<div class="carousel">
<img src="image1.jpg" alt="Image 1">
<img src="image2.jpg" alt="Image 2">
<img src="image3.jpg" alt="Image 3">
<div class="prev">‹</div>
<div class="next">›</div>
<div class="dots">
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
</div>
</div>
<script>
var carousel = document.querySelector('.carousel');
var prevButton = carousel.querySelector('.prev');
var nextButton = carousel.querySelector('.next');
var dots = carousel.querySelectorAll('.dot');
var images = carousel.querySelectorAll('img');
var currentIndex = 0;
function showImage(index) {
for (var i = 0; i < images.length; i++) {
images[i].style.display = 'none';
dots[i].classList.remove('active');
}
images[index].style.display = 'block';
dots[index].classList.add('active');
}
function prevImage() {
currentIndex--;
if (currentIndex < 0) {
currentIndex = images.length - 1;
}
showImage(currentIndex);
}
function nextImage() {
currentIndex++;
if (currentIndex >= images.length) {
currentIndex = 0;
}
showImage(currentIndex);
}
prevButton.addEventListener('click', prevImage);
nextButton.addEventListener('click', nextImage);
for (var i = 0; i < dots.length; i++) {
dots[i].addEventListener('click', function() {
var dotIndex = Array.prototype.indexOf.call(dots, this);
showImage(dotIndex);
});
}
showImage(currentIndex);
</script>
</body>
</html>
```
这段代码实现了一个基本的淘宝轮播图效果,包括左右箭头切换图片和底部的小圆点指示器。你可以根据需要修改图片地址和样式来适应你的项目。
阅读全文