【问题标题】:jQuery animation within hover() only fires on second timehover() 中的 jQuery 动画仅在第二次触发
【发布时间】:2011-06-11 01:20:24
【问题描述】:

我正在使用 jQuery 对 hover() 的高度变化进行动画处理。

悬停时,它会应用一个 .hover 类,点击后,它会切换一个 .expanded 类。它主要工作,具有适当的动画,但只有在第一次之后。第一次悬停将完全跳过动画。

我很难过 - 这是有问题的代码:

$('#expandingbox').hover(

    /*on mouseenter, if not expanded, add hover class*/
    function() {
    if (!$(this).hasClass("expanded")) {
        $(this).stop(true, true).addClass("hover", "slow");
    }},

    /*on mouseout, if not expanded, remove hover class*/
    function() {
    if (!$(this).hasClass("expanded")) {
        $(this).stop(true, true).removeClass("hover", "slow");
    }
}).click(function() {
    $(this).toggleClass("expanded", "slow");
});

我在某处发现添加 $('#expandingbox').trigger('mouseout') 可以解决此问题,但它对我不起作用。 这是一个重现问题的示例: http://jsfiddle.net/Qc42v/


更新: 提交了一张票,结果证明这是一个 jQuery 错误。相同的代码适用于 jQuery 1.5(和最新版本的 jQuery UI)。

【问题讨论】:

标签: jquery jquery-ui jquery-hover


【解决方案1】:

这可能是一个合法的 jQuery UI 错误。事先添加一些随机课程似乎可以为我解决问题。检查:http://jsfiddle.net/Qc42v/9/

所以基本上是这样的:

/*on mouseenter, if not expanded, add hover class*/
function() {
if (!$(this).hasClass("expanded")) {
    $(this).addClass("xxx");
    $(this).stop(true, true).addClass("hover", "slow");
}},

如果这对你来说太难看了,也许只使用基本的 animate() 函数?不过,肯定有人应该提交错误:{

【讨论】:

  • 我不敢相信这行得通!你是怎么想到这样做的?我想我会提交一个错误,然后尝试使用 animate()。我从来没有使用过它,所以我想是时候学会它了!谢谢!
猜你喜欢
  • 2021-11-28
  • 1970-01-01
  • 1970-01-01
  • 2021-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-30
相关资源
最近更新 更多