jQuery:淡入/淡出 +动画元素
我使用 jQuery 淡入/淡出某些元素并更改其他元素的不透明度。
$(function(){
$('.image').each(function() {
$(this).hover(
function() {
$(this).stop().animate({ opacity: 0.3 }, 'slow');
$(this).siblings().fadeIn('slow');
},
function() {
$(this).stop().animate({ opacity: 1 }, 'slow');
$(this).siblings().fadeOut('slow');
})
});
});
您可以在 http://projects.klavina.com/stackoverflow/01/ (我还在页面上使用 jQuery Masonry 插件)。
我对 JS/jQuery 相当陌生,上面的代码不能很好地工作,除非我非常缓慢地将鼠标悬停在 .image 元素上。当我更快地在元素上移动时,即使我已经移动到另一个元素上,图像上的标题也会显示。我怎样才能删除它?即,只有当我仍然悬停在该特定元素上时,标题才会淡入。
示例网站上的第一张图像具有“z-index: 100;”对于标题,这样我就可以获得完全不透明的文本叠加。理想情况下,我会有“z-index:100;”对于所有的标题,但这使得悬停工作变得更糟。
另外,淡入/淡出在 IE 中看起来很扭曲。我该如何解决这个问题?我确实在另一个页面上使用了不透明度更改,并通过向元素添加白色背景来修复 IE 错误,但我不能在这里这样做(因为我下面有一张照片)。
谢谢你!
I'm using jQuery to fade in/out some elements and change the opacity of the others.
$(function(){
$('.image').each(function() {
$(this).hover(
function() {
$(this).stop().animate({ opacity: 0.3 }, 'slow');
$(this).siblings().fadeIn('slow');
},
function() {
$(this).stop().animate({ opacity: 1 }, 'slow');
$(this).siblings().fadeOut('slow');
})
});
});
You can see the full code at http://projects.klavina.com/stackoverflow/01/ (I'm also using the jQuery Masonry plugin on the page).
I'm fairly new to JS/jQuery and the above code doesn't work well, unless I mousover the .image element really slowly. When I move over the elements faster, the captions over the images show even when I've already moved over another element. How can I remove that? I.e. the captions should fade in only when I'm still hovering that particular element.
The first image on the example site has a "z-index: 100;" for the caption, so I can get the text overlay at full opacity. Ideally, I would have "z-index: 100;" for all the captions, but this makes the hovering work even worse.
Plus, the fading in/out looks distorted in IE. How can I fix that? I did use the opacity change on another page, and fixed the IE bug by adding a white background to the element, but I can't do that here (as I have a photo underneath).
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题的核心原因是您需要调用
.siblings().stop()
,此外还需要调用$(this).stop( )
(你已经有了)。解决这个问题后,您会看到一个新问题,您的字幕最初工作正常,但当反复将鼠标悬停在其上时,它们开始仅部分淡入(最终,它们会完全消失,直到您重新加载字幕)页)。这是由于
.fadeIn()
与.fadeOut()
结合使用时的工作方式 -fadeIn()
并不总是淡出 -进入opacity:1
——相反,它淡入到之前调用fadeOut()
时应用的任何不透明度。要解决此问题,您可以使用
animate({opacity:1},'slow')
而不是fadeIn('slow')
——或者您可以使用 more简洁(更清晰).fadeTo('slow',1)
(docs)。 (请注意,与其他动画函数相比,fadeTo 上的参数顺序不同 - 持续时间首先出现,然后是要淡入淡出的值)。当然,您也可以使用
fadeTo()
代替其他不透明动画 - 尽管使用animate()
肯定没有问题,正如您所展示的 - - 两者是等价的。 (当然,如果你想同时操作多个 css 属性,你需要使用animate()
。)当这一切组合在一起时,它可能看起来像这样:
你可以看到这个jsFiddle 的实际代码: http://jsfiddle.net/coltrane/XstpE/
(注意:该示例取决于上面原始帖子附带的托管资源,因此如果这些资源被移动或变得不可用,它将无法工作)。
另请注意:在上面的示例中,我像您在原始示例中所做的那样包含了
.each()
的使用,但我想指出这确实没有必要。以下是等效的(并且通常被认为是“更好的”jQuery 技术):
当您将事件处理程序应用于多元素集时,jQuery 会自动在该集中的每个元素上绑定相同的处理程序。 (我已经更新了 jsFiddle 中的示例(上面链接),以显示没有
each()
的代码)。编辑
OP指出,将鼠标悬停在标题(位于图像顶部)上会导致触发
mouseleave
处理程序,从而导致执行推出操作。期望的行为是让标题不触发推出。出现此问题的原因是标题“遮挡”了图像,并且
hover()
应用于图像。当鼠标滑过标题时,它不再位于图像上(它位于标题上),因此浏览器会在图像上触发鼠标左键。同样的情况也可能引起各种其他微妙的问题——尤其是当您添加更复杂的内容时。为了解决这个问题,我建议您简单地将
hover()
应用到上一级(到保存图像和标题的容器),而不是将其应用到图像直接地。在本例中,该容器是$('.entry')
。代码将更改如下:这是 jsFiddle 的新版本
The core cause of your issue is that you need a call to
.siblings().stop()
, in addition to the$(this).stop()
(which you already have).After you fix that, you'll see a new issue, where your captions initially work correctly, but then begin to fade in only partially once they've been moused-over repeatedly (and eventually, they'll disappear completely until you reload the page). This is due to the way
.fadeIn()
works when combined with.fadeOut()
--fadeIn()
doesn't always fade-in toopacity:1
-- instead, it fades-in to whatever opacity was applied at the timefadeOut()
was called previously.To get around this, you can use
animate({opacity:1},'slow')
instead offadeIn('slow')
-- or you can use the more concise (and clearer).fadeTo('slow',1)
(docs). (note, the parameter order is different on fadeTo, as compared to the other animation functions - the duration comes first, then the value you want to fade to).Of course, you could also use
fadeTo()
in place of your other opacity animation --though there is certainly nothing wrong with usinganimate()
as you've shown -- the two are equivalent. (of course, you would need to seanimate()
if you want to manipulate multiple css properties at the same time.)When it all comes together, it might look something like this:
You can see this code in action at jsFiddle: http://jsfiddle.net/coltrane/XstpE/
(note: that example depends on the hosted resources that go with the original post above, so it won't work if those get moved or become otherwise unavailable).
Also note: In the example above, I have included the use of
.each()
as you did in your original example, but I want to point out that it's really not necessary.The following is equivalent (and would usually be considered "better" jQuery technique):
When you apply an event handler to a multi-element set, jQuery automatically binds the same handler on every element in the set. (I've updated my example at jsFiddle (linked above) to show the code without the
each()
).Edit
The OP points out that hovering over the caption (which sits on top of the image) causes the
mouseleave
handler to trigger, thus resulting in the roll-out action being performed. The desired behavior is to have the caption not trigger the rollout.This issue happens because the caption "shadows" the image, and the
hover()
is applied to the image. When the mouse rolls over the caption it's no longer on the image (it's on the caption) so the browser fires a mouseleave on the image. This same situation can give rise to all sorts of other subtle problems too -- especially as you add more complex content.To solve this, I recommend that you simply apply
hover()
one level up (to the container that holds the image and the caption), instead of applying it to the image directly. In this case that container is$('.entry')
. The code would change like this:here is a new version of the jsFiddle
不确定“慢”参数在您的动画函数中意味着多长时间。尝试将其调整为“快速”,甚至提供以毫秒为单位的数值,看看是否有帮助。
要在鼠标离开元素时停止元素上的动画:
Not sure how long 'slow' paramater means in your animate functions. Try tweaking that to 'fast' or even provide a numeric value in milliseconds and see if that would help.
To stop animation on an element when your mouse leaves it:
尝试使用 mouseenter() 和 mouseleave() 而不是 hover()。
Try using mouseenter() and mouseleave() instead of hover().