我需要一些关于jquery的帮助,以实现淡出效果。以下是我的代码:
当你将鼠标悬停在图片上时,图片需要淡出并显示下面的红色区域;当你将鼠标移开时,图片应该再次淡入。我认为代码可能需要一个停止函数 - 只是写起来有点困难。
欢迎所有建议!
欢迎所有建议!
在你的特定jsFiddle中,使用以下内容:
$('#show').hover(function() {
$(this).stop(true).fadeTo("slow", 0);
}, function() {
$(this).stop(true).fadeTo("slow", 1);
});
.fadeOut()
和 .fadeIn()
,因为当它们完成时,它们会设置 display: none
,这会导致元素被隐藏,从而破坏了 .hover()
函数。因此,只需将不透明度淡化为 0(但悬停仍然有效),然后当悬停离开时,将其淡化回不透明度为 1。$('#show').hover(
function () {
$(this).fadeOut("slow");
},
function () {
$(this).fadeIn("slow");
}
);
.fadeOut()
在完成后隐藏了对象,这会影响.hover()
。 - undefined我使用Firefox,因此示例仅适用于Firefox,但其他浏览器应该是对称的。
在这里阅读更多相关内容:
致谢:
使用 fadeOut
的示例将无法与您的HTML代码一起工作。原因很简单 - 在 fadeOut
动画结束时,mouseout
或 hover
的第二个函数将被触发,因为图像将获得 display:none
属性。请查看我的代码 http://jsfiddle.net/TW232/
$('div').hover(function(){
$(this).width($('img', this).width()).height($('img', this).height());
$('img', this).stop(true,true).fadeOut('fast');
}, function(){
$('img', this).stop(true,true).fadeIn('fast');
});
html:
<div>
<img id="show" src="http://img14.imageshack.us/img14/9698/29588166.jpg" alt=""/>
</div>
和 CSS:
div {background:red; width:0px;}
.stop(true)
,这样如果你快速进出,它的表现会好一些。 - undefined