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>
#slider {
width: 300px;
height: 200px;
overflow: hidden;
position: relative;
}
#slider img {
width: 100%;
height: auto;
position: absolute;
transition: top 0.5s ease-in-out;
}
</style>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<div id="slider">
<img src="image1.jpg" alt="Image 1" id="slideImage">
</div>
<button id="prev">上一张</button>
<button id="next">下一张</button>
<script>
$(document).ready(function() {
var images = ["image1.jpg", "image2.jpg", "image3.jpg"];
var currentIndex = 0;
function showImage(index) {
$("#slideImage").attr("src", images[index]);
$("#slideImage").css("top", "0");
}
$("#prev").click(function() {
currentIndex = (currentIndex - 1 + images.length) % images.length;
$("#slideImage").css("top", "-200px");
setTimeout(function() {
showImage(currentIndex);
}, 500);
});
$("#next").click(function() {
currentIndex = (currentIndex + 1) % images.length;
$("#slideImage").css("top", "200px");
setTimeout(function() {
showImage(currentIndex);
}, 500);
});
// 初始显示第一张图片
showImage(currentIndex);
});
</script>
</body>
</html>
通过以上方法,可以有效实现和优化jQuery图片上下滑动效果。
领取专属 10元无门槛券
手把手带您无忧上云