jquery - 淡出除悬停的子项之外的所有子项

发布于 2024-11-09 08:50:07 字数 1285 浏览 3 评论 0原文

试图做一些相当简单的事情,但它却让我困惑。我有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戏剧牡丹亭 2024-11-16 08:50:07

问题是您正在向动画队列添加新命令。您必须调用 stop() 来停止所有正在进行的动画并立即启动新的动画。

$('.suiting-thumb').hover(function() {
    var thumbBtnIdPrefix = 'thumb';
    var thumbBtnNum = $(this).attr('id').substring((thumbBtnIdPrefix.length));
    $('.suiting-thumb:not(#thumb' + thumbBtnNum + ')').stop().animate({
        "opacity": .3
    }), 200;
}, function() {
    $('.suiting-thumb').stop().animate({
        "opacity": 1
    }), 200;
});

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.

$('.suiting-thumb').hover(function() {
    var thumbBtnIdPrefix = 'thumb';
    var thumbBtnNum = $(this).attr('id').substring((thumbBtnIdPrefix.length));
    $('.suiting-thumb:not(#thumb' + thumbBtnNum + ')').stop().animate({
        "opacity": .3
    }), 200;
}, function() {
    $('.suiting-thumb').stop().animate({
        "opacity": 1
    }), 200;
});

http://jsfiddle.net/ywUUL/1/

荭秂 2024-11-16 08:50:07

这应该可以解决问题:

$('.suiting-thumb').hover(function() {
                $('.suiting-thumb').not(this).animate({
                    "opacity": .3
                },200);

                $(this).animate({
                    "opacity": 1
                },200);
        });

这样动画就可以并行执行。我还对选择器进行了一些优化,使其更具可读性,并且仅对需要动画的元素进行动画处理。

请注意,您需要等待悬停动画完成才能开始下一个悬停动画。如果您希望立即执行此操作,请确保调用 $('.suiting-thumb').stop(true, false ) 立即停止所有动画,然后开始下一个动画。

This should do the trick:

$('.suiting-thumb').hover(function() {
                $('.suiting-thumb').not(this).animate({
                    "opacity": .3
                },200);

                $(this).animate({
                    "opacity": 1
                },200);
        });

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文