【问题标题】:How to prevent repeating animation on jQuery animate toggle?如何防止在 jQuery 动画切换上重复动画?
【发布时间】:2016-05-13 06:09:05
【问题描述】:

我有一个Pen here

我有一个分享按钮,当您悬停时会展开,一切正常 - 除非您将鼠标悬停多次,否则它会在您悬停时不断重复动画。

有什么办法可以防止动画两次触发并跳跃?这是我的原始代码。

$('.share-icon').hover(function () {
  $(".share-open").animate({
    opacity: "toggle",
    width: "toggle"
  }, 200, function () {});
});

我试图通过仅在元素设置为不显示时触发动画来防止它,并且它的效果会更好一些,但是如果你玩它会很跳跃,所以我知道必须有更好的方法。 Attempt pen.

$('.share-icon').hover(function () {
  if ($('.share-open').css('display') == 'none') {
    $(".share-open").animate({
      opacity: "toggle",
      width: 170
    }, 200, function () {});
  } else {
    $(".share-open").animate({
      opacity: "toggle",
      width: 0
    }, 200, function () {});
  }
});

这里是 HTML。

<div class="share-outer">
  <div class="share-event">
    Share with your friends
    <div class="share-icon">
      <img src="arrow-hover.png">
      <div class="share-open">
        <img src="arrow.png">
        <i class="fa fa-facebook-official"></i>
        <i class="fa fa-twitter"></i>
        <i class="fa fa-envelope-o"></i>
      </div>
    </div>
  </div>
</div>

相关CSS:

.share-open { 
    display: none;
    opacity: 1;
}

谢谢!

【问题讨论】:

    标签: javascript jquery html css animation


    【解决方案1】:

    你必须使用 stop() $(".share-open").stop().animate({

    【讨论】:

    • 抱歉回复延迟,谢谢!完美运行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-26
    • 2012-06-16
    • 2012-03-11
    • 2010-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多