javascript - 如何修复 jQuery 中不稳定的动画

标签 javascript jquery animation

我用 jQuery 构建了一个简单的菜单

http://vanquish.websitewelcome.com/~toberua/

这是菜单示例

<ul>
  <li id="your-residences">
    <strong>Your Residences</strong>

    <ul>
      <li class="menu-1"><a href=
      "/~toberua/your-residences/deluxe-ocean-front-bure/">Deluxe
      Ocean Front Bure</a></li>

      <li class="menu-2"><a href=
      "/~toberua/your-residences/premium-ocean-front-bure/">Premium
      Ocean Front Bure</a></li>
    </ul>
  </li>

  <li id="your-island">
    <strong>Your Island</strong>

    <ul>
      <li class="menu-1"><a href=
      "/~toberua/your-island/where-is-toberua/">Where is
      Toberua?</a></li>

      <li class="menu-2"><a href=
      "/~toberua/your-island/island-facilities/">Island
      Facilities</a></li>

      <li class="menu-3"><a href=
      "/~toberua/your-island/massage-and-spa/">Massage &amp;
      Spa</a></li>
    </ul>
  </li>
</ul>

这是我的 jQuery:

var menu = {

  init: function() {

    $('#header > ul > li').hoverIntent(function() {
      $(this).find('ul').show().css({opacity: '0.3'}).height(0).animate({height: '88px', opacity: '1.0'}, 800);

    }, function() {

      $(this).find('ul').animate({height: '0', opacity: '0.3'}, 400, function() { $(this).hide();  }); 

    });

  }

}

这会产生一些古怪的行为。例如,有时菜单会向下滑动,然后继续向上和向下滑动。其他麻烦是菜单完美地向下滑动,然后闪烁到空白背景,然后再次返回。

有人可以指出我可能做错了什么吗?

谢谢

最佳答案

我立即注意到的唯一问题是,您的代码会很高兴地对另一个动画进行排队,而不会清除已经运行的动画的队列 - 因此,如果您稍微晃动一下鼠标,就会打开菜单并反复关闭,恶作剧风格!

幸运的是,这是一个简单的解决方法:只需调用 stop()在每个 animate() 之前打电话...

关于javascript - 如何修复 jQuery 中不稳定的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/828145/

相关文章:

javascript - 扩展原型(prototype)而不是声明整个对象的优点

javascript - 为什么 antlr4 在 LT 上会卡住!*

jquery - 当我在导航栏上隐藏元素时,如何修复导航栏中剩余元素发生的 "jump"?

html - 如何通过CSS更改图像覆盖整个div

ios - 设置 UIImage 动画的正确/干净的方法?

javascript - Twitter 未捕获类型错误 : undefined is not a function

javascript - $.cache 什么时候被认为太大了?

javascript - Ajax 长轮询问题(网关 504)

Ios 7 自定义 UINavigationController 动画因目标 Controller 中的 mapView 而失败

javascript - 单击 'sweetAlert' 警报 'ok' 后如何调用 Bootstrap 模式?