JQuery Animate 和 CSS 过渡冲突?

标签 jquery css

我有两个 div 并排 float 。默认情况下,它们各占 50% 宽度。当您将鼠标悬停在一个 div 上时,事件 div 会增长到 70%,而另一个 div 会缩小到 30%。

JS 工作正常,除了我想用 CSS 添加缓动效果。当我添加 CSS 过渡时,第二个 div 在 mouseleave 时中断(但仅当您用鼠标移动整个包装元素时。)

我的猜测是组合宽度暂时大于 100%,迫使第二个 div 被推到第一个 float div 下面。

关于如何解决这个问题有什么建议吗?代码在下面,但这里有一个 fiddle 可以查看实际问题。

http://jsfiddle.net/gFf3n/1/

您只能通过将鼠标移出粉红色框并完全移出包装来复制错误。

<div class="wrapper">
    <div class="vail">
        <p class="overview"> I work always </p>
    </div>
     <div class="denver"> 
       <p class="overview"> I break when you mouseleave outside of the box, but work when you mouseleave over to the blue box </p>
    </div>
</div>

var $targets = $('.vail, .denver');
$targets.hover(function() {
    $(this).parent().children().not(this).animate({
        width: "30%"
    },  0);
    $(this).animate({
        width: "70%"
    }, 0);
    $(this).parent().children().not(this).find('.overview').hide();
    $(this).find('.overview').show();
});

$targets.mouseleave(function() {
    $('.overview').hide();
    $targets.animate({
        width: "50%"
    }, 0);
});

.wrapper {
    width: 600px; 
}
.overview {
    display: none;
}
.vail {
    transition: all 0.9s ease;
    background-color: blue;
    color: #fff;
    float: left;
    height: 300px;
    width: 50%;
}
.denver {
    transition: all 0.9s ease;
    background-color: pink;
    float: left;
    height: 300px;
    width: 50%;
}

最佳答案

我认为问题在于,当您离开 denver div 类时,vail div 类回到 50% 宽度的速度比 denver div 类稍快。

这使得两个 div 的总和超过 100%,并且它们不会彼此相邻。 我试过了,在鼠标离开时打破 div 没有问题。 希望这就是您要找的。

var $targets = $('.vail, .denver');
    $targets.hover(function() {
        $(this).parent().children().not(this).animate({
            width: "30%"
        },  0);
        $(this).animate({
            width: "70%"
        }, 0);
        $(this).parent().children().not(this).find('.overview').hide();
        $(this).find('.overview').show();
    });

    $targets.mouseleave(function() {
        $('.overview').hide();
         $(this).animate({
               width: "50%"
           }, 0);

        $(this).parent().children().not(this).animate({
                width: "50%"
            }, 0);
    });

关于JQuery Animate 和 CSS 过渡冲突?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23744444/

相关文章:

javascript - 在窗口中获取第一个可见的 iFrame 并打印它

javascript - 在 Marionette 中使用 appendHtml() 时是否可以避免添加默认 View 元素 "div"?

javascript - 无法使背景图像淡入?

jquery - 正确使用 jQuery Effects

jquery - 基于导航栏淡入淡出 DIV

javascript - 如何自动检测特定 Div 的 scrollTop 位置?

javascript - 单击交换按钮时,如何让我的红色 div 更改背景颜色以在红色和绿色之间切换?

jQuery 动画将页面分成两半并显示一个 div

html - 在 bootstrap 列中对齐图像

html - 图像定位