jQuery 图片叠加滚动是指使用 jQuery 库来实现多个图片层叠在一起,并且可以通过滚动或其他交互方式展示不同的图片内容。这种效果常用于创建动态的视觉效果,提升用户体验。
以下是一个简单的 jQuery 图片叠加滚动的示例代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery 图片叠加滚动</title>
<style>
.container {
position: relative;
width: 80%;
margin: 0 auto;
}
.image-container {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.image-container img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
opacity: 0;
transition: opacity 1s ease-in-out;
}
.image-container img.active {
opacity: 1;
}
</style>
</head>
<body>
<div class="container">
<div class="image-container">
<img src="image1.jpg" alt="Image 1" class="active">
<img src="image2.jpg" alt="Image 2">
<img src="image3.jpg" alt="Image 3">
</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
let images = $('.image-container img');
let currentIndex = 0;
function showImage(index) {
images.removeClass('active').eq(index).addClass('active');
}
setInterval(function() {
currentIndex = (currentIndex + 1) % images.length;
showImage(currentIndex);
}, 3000);
});
</script>
</body>
</html>
opacity
和 transition
属性来平滑过渡。通过以上方法,可以有效地实现和优化 jQuery 图片叠加滚动效果。
领取专属 10元无门槛券
手把手带您无忧上云