【问题标题】:Quirkiness with jQuery mouseenter - mouseleavejQuery mouseenter 的古怪之处 - mouseleave
【发布时间】:2012-08-16 03:35:54
【问题描述】:

我正在尝试在光标进入其父元素时为元素设置动画,并在鼠标退出元素时可靠地反转该动画 - 对于多层嵌套元素。我可以用 CSS 笨拙地做到这一点(过渡不是一个选项),但想用 jQuery 来让它性感起来;)

我正在尝试实现的一个示例是这种类型的 accordion menu,但正如我所说,使用 jQuery。

到目前为止,我有 tried using mouseenter and mouseleave 用于相同的示例,但在第一次扩展和收缩后似乎无法使其工作。

我怎样才能可靠而优雅地将此隐藏/显示动画绑定到 mouseenter/mouseleave 或 jQuery 中的类似内容?

【问题讨论】:

    标签: jquery hover nested mouseevent accordion


    【解决方案1】:

    看来可以

    http://jsfiddle.net/sechou/Rce8A/2/

    $(function(){
      //bind event
      $("li.level1").mouseenter(   
        function(){
           // make sure the item would diplay
           $(this).find("ul:first").find("li.level2").show(); 
           // do "show" 
           $(this).find("ul:first").show(500);       
        });
    
      $("li.level1").mouseleave(
          function() {
            // do "hide" 
            $(this).find(":parent").hide(500);
          }
      );
    });
    

    【讨论】:

    • 对了,我选择jquery 1.8(在左栏)
    • 嗯,是的,它似乎确实有效,但看起来很老套。你能解释一下它为什么有效吗?
    • 最重要的是绑定事件
    • nothing .....,您只是忘记在jsfiddle.net/Rce8A/1 中选择jquery 版本但是,在您的代码中,有一个小错误。你必须添加 $(this).find("ul:first").find("li.level2").show();使物品集中展示
    • 我知道 jQuery 库不是问题所在。但是看看你的解决方案,你似乎是偶然发现的......你能解释一下为什么你这样写而不是:$(this).find("ul:first, ul:first > li").show(500) 例如哪个更精简、更高效??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多