jquery - 淡出除悬停的子项之外的所有子项
试图做一些相当简单的事情,但它却让我困惑。我有以下 HTML:
<div id="four">
<div id="thumb1" class="suiting-thumb">
<img src="img/gallery/suit1-thumb.jpg" alt="" title="" />
</div>
<div id="thumb2" class="suiting-thumb">
<img src="img/gallery/suit2-thumb.jpg" alt="" title="" />
</div>
<div id="thumb3" class="suiting-thumb">
<img src="img/gallery/suit3-thumb.jpg" alt="" title="" />
</div>
</div>
我想做的就是“调暗”父 div 的子级,除了悬停的子级。我用这个 jQuery 代码片段成功地做到了这一点,但是在淡出/淡入之间有一个短暂的延迟:
$('.suiting-thumb').hover(function() {
var thumbBtnIdPrefix = 'thumb';
var thumbBtnNum = $(this).attr('id').substring((thumbBtnIdPrefix.length));
$('.suiting-thumb:not(#thumb' + thumbBtnNum + ')').animate({
"opacity": .3
}),200;
},
function() {
$('.suiting-thumb').animate({
"opacity": 1
}),200;
});
我觉得好像我需要通过使用悬停语句选择 #four 来淡出父 div 的所有子元素,但我不太确定该怎么做。任何帮助将不胜感激,谢谢!
Trying to do something fairly simple, but it's eluding me. I have the following HTML:
<div id="four">
<div id="thumb1" class="suiting-thumb">
<img src="img/gallery/suit1-thumb.jpg" alt="" title="" />
</div>
<div id="thumb2" class="suiting-thumb">
<img src="img/gallery/suit2-thumb.jpg" alt="" title="" />
</div>
<div id="thumb3" class="suiting-thumb">
<img src="img/gallery/suit3-thumb.jpg" alt="" title="" />
</div>
</div>
All I would like to do is "dim" the children of the parent div, EXCEPT for the child being hovered. I'm successfully doing so with this jQuery snippet, but there is a brief delay between the fade out / in:
$('.suiting-thumb').hover(function() {
var thumbBtnIdPrefix = 'thumb';
var thumbBtnNum = $(this).attr('id').substring((thumbBtnIdPrefix.length));
$('.suiting-thumb:not(#thumb' + thumbBtnNum + ')').animate({
"opacity": .3
}),200;
},
function() {
$('.suiting-thumb').animate({
"opacity": 1
}),200;
});
I feel as though I need to be fading out all the children of the parent div by selecting #four with my hover statement, but I'm not quite sure how to do that. Any help would much appreciated, thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您正在向动画队列添加新命令。您必须调用
stop()
来停止所有正在进行的动画并立即启动新的动画。http://jsfiddle.net/ywUUL/1/
The problem is that you're adding new commands to the animation queue. You have to call
stop()
which stops all ongoing animations and immediately starts the new one.http://jsfiddle.net/ywUUL/1/
这应该可以解决问题:
这样动画就可以并行执行。我还对选择器进行了一些优化,使其更具可读性,并且仅对需要动画的元素进行动画处理。
请注意,您需要等待悬停动画完成才能开始下一个悬停动画。如果您希望立即执行此操作,请确保调用 $('.suiting-thumb').stop(true, false ) 立即停止所有动画,然后开始下一个动画。
This should do the trick:
This way the animations are executed in parallel. I've also optimized the selector somewhat to make it more readable and animate only the elements that need animation.
Note that you need to wait for a hover animation to finish before the next one starts. If you want that to be instant, make sure calling $('.suiting-thumb').stop(true, false) to stop all animations immediately and then start the next animation.