jQuery鼠标滑过换图片是一种常见的网页交互效果,通过监听鼠标悬停(hover)事件,实现图片的动态切换。这种效果通常用于提升用户体验,使页面更加生动和吸引人。
以下是一个简单的jQuery鼠标滑过换图片的示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery Hover Image Swap</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
.image-container {
width: 200px;
height: 200px;
overflow: hidden;
}
.image-container img {
width: 100%;
height: auto;
transition: opacity 0.3s ease;
}
</style>
</head>
<body>
<div class="image-container">
<img src="image1.jpg" alt="Image 1" id="hoverImage">
</div>
<script>
$(document).ready(function() {
$('#hoverImage').hover(
function() {
$(this).fadeOut(300, function() {
$(this).attr('src', 'image2.jpg').fadeIn(300);
});
},
function() {
$(this).fadeOut(300, function() {
$(this).attr('src', 'image1.jpg').fadeIn(300);
});
}
);
});
</script>
</body>
</html>
通过以上方法,可以有效解决jQuery鼠标滑过换图片时可能遇到的问题,确保效果的稳定性和流畅性。
没有搜到相关的文章