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 Mouseover Image Swap</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
.image-container {
position: relative;
display: inline-block;
}
.hover-image {
position: absolute;
top: 0;
left: 0;
opacity: 0;
transition: opacity 0.5s ease-in-out;
}
.image-container:hover .hover-image {
opacity: 1;
}
</style>
</head>
<body>
<div class="image-container">
<img src="image1.jpg" alt="Image 1">
<img src="image2.jpg" alt="Image 2" class="hover-image">
</div>
<script>
$(document).ready(function() {
$('.image-container').hover(
function() {
$(this).find('img').eq(1).stop().fadeIn();
},
function() {
$(this).find('img').eq(1).stop().fadeOut();
}
);
});
</script>
</body>
</html>
.hover()
方法来处理鼠标进入和离开元素的事件。通过以上示例和解释,你应该能够实现一个基本的 jQuery 鼠标经过切换图片特效,并了解其背后的基础概念和应用场景。
领取专属 10元无门槛券
手把手带您无忧上云