联系我们
简单又实用的WordPress网站制作教学
当前位置:网站首页 > 网页前端技术 > 正文

jQuery 淡出一个图像到另一个图像的实现代码 - Web前端

作者:98wpeu发布时间:2026-06-27分类:网页前端技术浏览:2


导读:JQuery淡出一张图片到另一张图片,例如有下边的html:复制代码代码如下:<divclass="fade"><imgsrc="1.jpg"/&...
JQuery 淡出一张图片到另一张图片,例如有下边的 html:

复制代码 代码如下:
<div class="fade">
<img src="1.jpg" />
</div>


首先,确保 div 的大小和图片大小一样,这个 div 有另一个背景图,如下:

CSS代码:

复制代码 代码如下:
.fade
{
background-image:url('images/2.jpg');
width:300px;
height:225px;
}


jQuery 代码如下:

复制代码 代码如下:
$(document).ready(function () {
$(".fade").hover(function () {
$(this).find("img").stop(true, true).Animate({ opacity: 0 }, 500);
}, function () {
$(this).find("img").stop(true, true).animate({ opacity: 1 }, 500);
});
});


完整实现代码:

复制代码 代码如下:
<div class="fade"><img src="1.jpg" /></div>
<style type="text/css">
.fade
{
background-image:url('2.jpg');
width:300px;
height:225px;
margin:0 auto 15px;
}</style>
<script type="text/javascript">
$(document).ready(function () {
$(".fade").hover(function () {
$(this).find("img").stop(true, true).animate({ opacity: 0 }, 500);
}, function () {
$(this).find("img").stop(true, true).animate({ opacity: 1 }, 500);
});
});
</script>


作者:jquery学习

标签:图像代码WebjQuery


网页前端技术排行
最近发表
网站分类
标签列表