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>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
.fade-image {
display: none;
position: absolute;
top: 0;
left: 0;
}
</style>
</head>
<body>
<img src="image1.jpg" class="fade-image" alt="Image 1">
<img src="image2.jpg" class="fade-image" alt="Image 2">
<button id="fade-in">淡入</button>
<button id="fade-out">淡出</button>
<script>
$(document).ready(function() {
let currentImage = 0;
const images = $('.fade-image');
function showImage(index) {
images.hide();
images.eq(index).fadeIn(1000);
}
$('#fade-in').click(function() {
currentImage = (currentImage + 1) % images.length;
showImage(currentImage);
});
$('#fade-out').click(function() {
currentImage = (currentImage - 1 + images.length) % images.length;
showImage(currentImage);
});
// 初始化显示第一张图片
showImage(currentImage);
});
</script>
</body>
</html>
通过以上示例代码和常见问题解决方法,你应该能够实现并调试 jQuery 图片淡入淡出的效果。