Banner图片轮播是一种常见的网页设计技术,用于在网页上展示一系列图片,并自动或手动切换显示不同的图片。这种技术可以吸引用户的注意力,提高用户体验。
以下是一个简单的JavaScript实现banner图片轮播的示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Banner轮播</title>
<style>
#banner {
width: 100%;
overflow: hidden;
position: relative;
}
#banner img {
width: 100%;
display: none;
}
#banner img.active {
display: block;
}
</style>
</head>
<body>
<div id="banner">
<img src="image1.jpg" alt="Image 1" class="active">
<img src="image2.jpg" alt="Image 2">
<img src="image3.jpg" alt="Image 3">
</div>
<script>
const images = document.querySelectorAll('#banner img');
let currentIndex = 0;
function showImage(index) {
images.forEach((img, i) => {
img.classList.remove('active');
});
images[index].classList.add('active');
}
function nextImage() {
currentIndex = (currentIndex + 1) % images.length;
showImage(currentIndex);
}
setInterval(nextImage, 3000); // 每3秒切换一次图片
</script>
</body>
</html>
通过以上代码示例和问题解决方法,可以实现一个基本的banner图片轮播功能,并解决常见的问题。
领取专属 10元无门槛券
手把手带您无忧上云