简体   繁体   English

CSS动画与jquery悬停重复

[英]CSS animation repeating with jquery hover

I have a problem with a repeating CSS animation that is executed by a jquery hover function. 我有一个jquery悬停功能执行的重复CSS动画问题。 You can see an example of the problem in this DEMO . 您可以在此DEMO中看到问题的示例。

When you open the demo please hover over the first star and pan the mouse from left to right. 当您打开演示时,请将鼠标悬停在第一颗星上,并从左向右平移鼠标。 As you can see the animation repeats itself causing a stutter. 如您所见,动画重复本身会造成结结。 How can I fix this so that the animation only fire once per star, keeping the previous stars highlighted. 如何解决此问题,以便动画仅每颗星星触发一次,而使先前的星星保持突出显示。

HTML HTML

<div class="GvStarContainer">
  <!--Style 1 STARTED-->
  <div class="GvStarTmp">

    <div class="margi-star">
      <div class="rate-ex1-cnt">
        <div id="1" class="star star-one-1 rate-btn star-one"></div>
        <div id="2" class="star star-one-2 rate-btn star-one"></div>
        <div id="3" class="star star-one-3 rate-btn star-one"></div>
        <div id="4" class="star star-one-4 rate-btn star-one"></div>
        <div id="5" class="star star-one-5 rate-btn star-one"></div>
      </div>
    </div>
  </div>
  <!--Style 1 FINISHED-->
  <!--Style 2 STARTED-->
  <div class="GvStarTmp">

    <div class="margi-star">
      <div class="rate-ex2-cnt">
        <div id="1" class="star star-two-1 rate-btn star-two"></div>
        <div id="2" class="star star-two-2 rate-btn star-two"></div>
        <div id="3" class="star star-two-3 rate-btn star-two"></div>
        <div id="4" class="star star-two-4 rate-btn star-two"></div>
        <div id="5" class="star star-two-5 rate-btn star-two"></div>
      </div>
    </div>
  </div>
  <!--Style 2 FINISHED-->
  <!--Style 3 STARTED-->
  <div class="GvStarTmp">

    <div class="margi-star">
      <div class="rate-ex3-cnt">
        <div id="1" class="star star-tree-1 rate-btn star-tree"></div>
        <div id="2" class="star star-tree-2 rate-btn star-tree"></div>
        <div id="3" class="star star-tree-3 rate-btn star-tree"></div>
        <div id="4" class="star star-tree-4 rate-btn star-tree"></div>
        <div id="5" class="star star-tree-5 rate-btn star-tree"></div>
      </div>
    </div>
  </div>
  <!--Style 3 FINISHED-->
</div>

JS JS

$(document).ready(function() {

  var prevStars = $(this).prevAll();
  var nextStars = $(this).nextAll();

  $(".star").hover(
    function() {
      var prevStars = $(this).prevAll();
      prevStars.addClass('rate-btn-hover');
    },
    function() {
      var prevStars = $(this).prevAll();
      prevStars.removeClass('rate-btn-hover');
    }
  );

  $("body").on("click", ".star", function() {
    var prevStars = $(this).prevAll().addBack();
    prevStars.addClass('rate-btn-active');
  });
});

You are applying the animation to both :hover selector and .rate-btn-hover class with this: 您正在使用以下方法将动画应用于:hover选择器和.rate-btn-hover类:

.rate-ex1-cnt .rate-btn:hover, .rate-ex1-cnt  .rate-btn-hover, .rate-ex1-cnt  .rate-btn-active{
    background: url(http://www.oobenn.com/GvStar/css/img/rate-btn1-hover.png) no-repeat;
    -webkit-animation-name: bounceIn;
    animation-name: bounceIn;
    -webkit-animation-duration: .75s;
    animation-duration: .75s
}

Instead apply the animation only to :hover selector 而是仅将动画应用于:hover选择器

.rate-ex1-cnt .rate-btn:hover {
    background: url(http://www.oobenn.com/GvStar/css/img/rate-btn1-hover.png) no-repeat;
    -webkit-animation-name: bounceIn;
    animation-name: bounceIn;
    -webkit-animation-duration: .75s;
    animation-duration: .75s
}

and remove the animation properties from .rate-btn-hover class 并从.rate-btn-hover类中删除动画属性

.rate-ex1-cnt  .rate-btn-hover, .rate-ex1-cnt  .rate-btn-active{
    background: url(http://www.oobenn.com/GvStar/css/img/rate-btn1-hover.png) no-repeat;
}

See first star group in updated Demo . 请参阅更新的Demo中的第一颗星组。

The problem seems to be that you are clearing the previous stars in the hover() 'handleOut' function. 问题似乎是您正在清除hover()'handleOut'函数中的先前的星星。 You want to retain those stars as filled, therefore you only want to clear 'next' stars, and then separately manage the cleanup of all stars as you exit the area containing the group of stars. 您希望保留这些恒星已填充,因此只想清除“下一个”恒星,然后在退出包含恒星组的区域时分别管理所有恒星的清理。

$(".star").hover(
    function() {
       $(this).prevAll().addClass('rate-btn-hover');
       $(this).addClass('rate-btn-hover');
    },
    function() {
       $(this).nextAll().removeClass('rate-btn-hover');
       $(this).removeClass('rate-btn-hover');
    }
);

$(".margi-star div:first-child").hover(
    function() {
    },
    function() {
       $(this).children().removeClass('rate-btn-hover');
    }
);

With this approach I am handling the start of the animation through script by setting the current controls class: 通过这种方法,我通过设置当前控件类来通过脚本处理动画的开始:

$(this).addClass('rate-btn-hover');

The difficulty with a pure css solution in your current code is that you are setting a background image in your .rate-btn:hover selector, but then the non hover state managed by .rate-btn has a different background image (empty star). 当前代码中使用纯CSS解决方案的困难在于,您正在.rate-btn:hover选择器中设置背景图片,但是.rate-btn管理的非悬停状态具有不同的背景图片(空星) 。 This causes the filled star to be replaced with a blank star when .rate-btn:hover is no longer active. .rate-btn:hover不再处于活动状态时,这将使填充的星形替换为空白星形。

As the css hover event is no longer required in my attempted fix, you can also remove the :hover selector from your css as follows: 由于在我尝试的修复中不再需要css悬停事件,因此您还可以按如下所示从css中删除:hover选择器:

.rate-ex1-cnt  .rate-btn-hover, .rate-ex1-cnt  .rate-btn-active{
    background: url(http://www.oobenn.com/GvStar/css/img/rate-btn1-hover.png) no-repeat;
    -webkit-animation-name: bounceIn;
    animation-name: bounceIn;
    -webkit-animation-duration: .75s;
    animation-duration: .75s
}

The full demo is here: http://codepen.io/anon/pen/VKorea 完整的演示在这里: http : //codepen.io/anon/pen/VKorea

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM