简体   繁体   English

(jQuery).hover()中的淡入/淡出元素的问题

[英](jQuery) Issue with fading in/out elements within .hover()

I'm having an issue trying to get .fadeIn() , .fadeOut() , and .hide() to behave properly when an element is hovered over. 我在试图让一个问题.fadeIn() .fadeOut().hide()当一个元素悬停在正确的行为。

Let's say I have a container, .post-box . 假设我有一个容器.post-box

.post-box contains two divs: .description and .quote . .post-box包含两个div: .description.quote The .quote div is initially hidden so that when .post-box is hovered over, it fades in and takes the place of the .description div, which gets hidden with .hide() . .quote div最初是隐藏的,因此当.post-box悬停在其上时,它会淡入并代替.description div的位置,后者通过.hide()隐藏。

When .post-box is hovered out of, .quote fades out and .description is faded in again. .post-box移出时, .quote淡出, .description再次淡入。

$(document).ready(function() {
    var description = $('.description');
    var quote = $('.quote');
    var postBox = $('.post-box');

    postBox.hover(function() {
        $(this).find(description).clearQueue().stop(true, true).hide(0, function() {
            quote.fadeIn(300);
        });
    }, function() {
        $(this).find(quote).clearQueue().stop(true, true).fadeOut(300, function() {
            description.fadeIn(300);
        });
    });
});

It seems that I've got this concept working fairly well except for one issue. 除了一个问题,我似乎已经使这个概念很好地工作了。 If you quickly hover over .post-box , quickly hover out, and then quickly hover over again, you're presented with both the .quote and .description divs showing at the same time (see example screenshot here) . 如果您快速将鼠标悬停在.post-box ,快速悬停然后再次快速悬停,则会同时显示.quote.description div (请参见此处的示例屏幕截图)

I thought I was preventing them from firing at the same time based on how my functions are set up, but I must be missing something important for this to be happening. 我以为我是根据功能的设置来阻止它们同时触发的,但是我必须错过一些重要的事情才能实现。

Here is my fiddle so you can see it in action. 这是我的小提琴,因此您可以在实际中看到它。

Could somebody please lead me in the right direction? 有人可以引导我朝正确的方向前进吗?

My guess would be to also clear the animation queue for the quote element on hover. 我的猜测是还要在悬停时清除quote元素的动画队列。

$(this).find(quote).clearQueue().stop(true, true).hide();

I've updated the fiddle accordingly . 我已经相应地更新了小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM