CSS图片上下轮播是一种网页设计效果,通过CSS动画实现图片在页面上下滚动展示。这种效果可以吸引用户的注意力,增强页面的动态感。
以下是一个简单的CSS图片上下轮播的示例代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Image Carousel</title>
<style>
.carousel {
width: 80%;
margin: 0 auto;
overflow: hidden;
position: relative;
height: 300px;
}
.carousel img {
width: 100%;
position: absolute;
transition: top 1s ease-in-out;
}
.carousel img.active {
top: 0;
}
.carousel img.next {
top: -300px;
}
.carousel img.prev {
top: 300px;
}
</style>
</head>
<body>
<div class="carousel">
<img src="image1.jpg" alt="Image 1" class="active">
<img src="image2.jpg" alt="Image 2" class="next">
<img src="image3.jpg" alt="Image 3" class="prev">
</div>
<script>
const images = document.querySelectorAll('.carousel img');
let currentIndex = 0;
function moveToNextImage() {
images[currentIndex].classList.remove('active');
currentIndex = (currentIndex + 1) % images.length;
images[currentIndex].classList.add('active');
}
setInterval(moveToNextImage, 3000);
</script>
</body>
</html>
通过以上方法,你可以实现一个简单的CSS图片上下轮播效果,并解决常见的技术问题。
领取专属 10元无门槛券
手把手带您无忧上云