【问题标题】:jQuery opacity/fade animation only works oncejQuery 不透明度/淡入淡出动画只工作一次
【发布时间】:2018-11-06 11:43:43
【问题描述】:

我正在建立一个个人网站。我研究并开始使用 CSS 关键帧,但自己找到了使用 jQuery + CSS 关键帧的解决方案。

我现在的问题(我假设有一个简单的答案,因为我一直盯着我的显示器太久了,所以我看不到)是我的悬停动画只能工作一次。也就是说,在鼠标悬停时,我的图像 1 淡入图像 2(图像 1 消失),然后图像 2 淡入图像 3(图像 2 消失),所以它就像一个级联。但是,这只能工作一次,如果我再次悬停,它将无法工作。

所以再次澄清一下,我希望这个动画从 image1 级联淡入淡出,到 image2,到 image3,然后反转出来(image3,到 image2,到 image1,即像一些变形效果)每次 用户鼠标:悬停在原始图像上。

我假设答案可能与添加但未删除的class“动画”有关?我实际上不知道 jQuery,但一直在自学即兴,并认为如果其他人看到我的代码,他们可以发现语法/错误。

也

这是将图像淡化/过渡到另一个的最佳方法吗?我想要一个平滑的淡入淡出,就像一个“变形”——这是我做的最好的方式吗? (jquery+keyframes),我应该添加更多关键帧以获得更平滑的效果吗?

这里是 jsfiddle: http://jsfiddle.net/1xrbxdnk/2/

以及源代码: HTML:

<div class="box">
  <img src="http://alpizano.com/assets/images/venom1.png" width="50%" class="top">
  <img src="http://alpizano.com/assets/images/venom2.png" width="50%" class="middle">
  <img src="http://alpizano.com/assets/images/venom3.png" width="50%" class="bottom">
</div>

JavsScript/jQuery

$("img.top").hover(function() {
  $(this).addClass("animated");
  $("img.middle").addClass("animated2");
  $("img.bottom").addClass("animated3");
})

$("img.bottom").bind("webkitAnimationEnd mozAnimationEnd animationEnd", function() {
    $(this).addClass("animated6")
  $("img.middle").addClass("animated5")
  $("img.top").addClass("animated4")
})

CSS

.box {
  position: relative;
}

img {
  position: absolute;
}

.middle {
  display: none;
}

.bottom {
  display: none;
}

@keyframes anim1 {
  0% {
    opacity: 1;
  }
  70% {
    opacity: 0;
  }
}

@keyframes anim2 {
  0% {
    opacity: 0;
  }
  50% {
    opacity: 1;
  }
}

@keyframes anim3 {
  0% {
    opacity: 0;
  }
  50% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}

@keyframes anim6 {
  0% {
    opacity: 1;
  }
  50% {
    opacity: 0;
  }
  100% {
    opacity: 0;
  }
}

@keyframes anim5 {
  0% {
    opacity: 0;
  }
  50% {
    opacity: 1;
  }
}

@keyframes anim4 {
  0% {
    opacity: 0;
  }
  70% {
    opacity: 1;
  }
}

.top.animated {
  animation: anim1 3s ease-in-out;
  opacity: 0;
}

.middle.animated2 {
  animation: anim2 3s ease-in-out;
  display: block;
  opacity: 0;
}

.bottom.animated3 {
  animation: anim3 3s ease-in-out;
  display: block;
  opacity: 1;
}

.bottom.animated6 {
  animation: anim6 3s ease-in-out;

  opacity: 0;
}

.middle.animated5 {
  animation: anim5 3s ease-in-out;

  opacity: 0;
}

.top.animated4 {
  animation: anim4 3s ease-in-out;
  opacity: 1;
}

【问题讨论】:

    标签: jquery image animation hover fade


    【解决方案1】:

    动画完成后,您需要删除动画类。您已经有一个用于动画结束的事件侦听器来添加反向动画。您可以执行以下操作:

    $("img.bottom").bind("webkitAnimationEnd mozAnimationEnd animationEnd",         
    function() {
      if (!$(this).hasClass('animated6')) {
        $(this).addClass("animated6");
        $("img.middle").addClass("animated5");
        $("img.top").addClass("animated4");
      }
      else {
        $(this).removeClass("animated6 animated3");
        $("img.middle").removeClass("animated5 animated2");
        $("img.top").removeClass("animated4 animated");
      }
    });
    

    这是你的小提琴更新:

    http://jsfiddle.net/1xrbxdnk/3/

    这是一个sn-p:

    $("img.top").hover(function() {
      $(this).addClass("animated");
      $("img.middle").addClass("animated2");
      $("img.bottom").addClass("animated3");
    })
    
    $("img.bottom").bind("webkitAnimationEnd mozAnimationEnd animationEnd", function() {
      if (!$(this).hasClass('animated6')) {
      	$(this).addClass("animated6");
        $("img.middle").addClass("animated5");
        $("img.top").addClass("animated4");
      }
      else {
        $(this).removeClass("animated6 animated3");
        $("img.middle").removeClass("animated5 animated2");
        $("img.top").removeClass("animated4 animated");
      }
    })
    .box {
      position: relative;
    }
    
    img {
      position: absolute;
    }
    
    .middle {
      display: none;
    }
    
    .bottom {
      display: none;
    }
    
    @keyframes anim1 {
      0% {
        opacity: 1;
      }
      70% {
        opacity: 0;
      }
    }
    
    @keyframes anim2 {
      0% {
        opacity: 0;
      }
      50% {
        opacity: 1;
      }
    }
    
    @keyframes anim3 {
      0% {
        opacity: 0;
      }
      50% {
        opacity: 0;
      }
      100% {
        opacity: 1;
      }
    }
    
    @keyframes anim6 {
      0% {
        opacity: 1;
      }
      50% {
        opacity: 0;
      }
      100% {
        opacity: 0;
      }
    }
    
    @keyframes anim5 {
      0% {
        opacity: 0;
      }
      50% {
        opacity: 1;
      }
    }
    
    @keyframes anim4 {
      0% {
        opacity: 0;
      }
      70% {
        opacity: 1;
      }
    }
    
    .top.animated {
      animation: anim1 3s ease-in-out;
      opacity: 0;
    }
    
    .middle.animated2 {
      animation: anim2 3s ease-in-out;
      display: block;
      opacity: 0;
    }
    
    .bottom.animated3 {
      animation: anim3 3s ease-in-out;
      display: block;
      opacity: 1;
    }
    
    .bottom.animated6 {
      animation: anim6 3s ease-in-out;
    
      opacity: 0;
    }
    
    .middle.animated5 {
      animation: anim5 3s ease-in-out;
    
      opacity: 0;
    }
    
    .top.animated4 {
      animation: anim4 3s ease-in-out;
      opacity: 1;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="box">
      <img src="http://alpizano.com/assets/images/venom1.png" width="50%" class="top">
      <img src="http://alpizano.com/assets/images/venom2.png" width="50%" class="middle">
      <img src="http://alpizano.com/assets/images/venom3.png" width="50%" class="bottom">
    </div>

    【讨论】:

    • 谢谢。我不知道 jQuery,所以我遇到了问题,我正在消化你的代码,我看到你如何在其中添加 if/else 条件来添加和删除类,非常感谢。
    • 没问题!顺便说一句,如果反向动画与开始动画完全相反,您可以查看“animation-direction:alternate”css 属性。如果你我的回答对你有用,你可以接受。
    【解决方案2】:

    为了重复性,您必须删除动画。类并将悬停功能绑定到父 div.box

    $(".box").hover(function() {
    
      $("img.top").removeClass().addClass("top");
      $("img.middle").removeClass().addClass("middle");
      $("img.bottom").removeClass().addClass("bottom");
    
      $(this).addClass("animated");
      $("img.middle").addClass("animated2");
      $("img.bottom").addClass("animated3");
    })
    
    $("img.bottom").bind("webkitAnimationEnd mozAnimationEnd animationEnd", function() {
        $(this).addClass("animated6")
      $("img.middle").addClass("animated5")
      $("img.top").addClass("animated4")
    })
    

    【讨论】:

    • 我将它复制到 jfiddle 中,但效果不佳,但感谢您提供一些帮助,我想我现在明白了,非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-18
    • 2012-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多