简体   繁体   English

如何使用jQuery覆盖CSS3动画?

[英]How to override CSS3 animation with jQuery?

I have an animation on a couple of elements that last a few seconds. 我对几个元素进行了动画处理,持续了几秒钟。 When you hover over one of them, I want the CSS3 animation to pause and do something, and when hover comes off, to continue animation. 当您将鼠标悬停在其中之一上时,我希望CSS3动画暂停并执行某些操作,并且当鼠标悬停时,希望继续播放动画。

Here is a quick example fiddle that shows the CSS3 animation happening, and when you hover over the third bar (with class .test) it should override the CSS: 这是一个简短的示例小提琴,显示了CSS3动画的发生,当您将鼠标悬停在第三栏(带有.test类)上时,它应覆盖CSS:

http://jsfiddle.net/TmFFx/ http://jsfiddle.net/TmFFx/

  @keyframes animation {
    from { width: 0;}
    to {width: 200px;}
}

@-webkit-keyframes animation {
    from { width: 0;}
    to {width: 100%;}
}

div {
    animation: animation 3s;
    -webkit-animation: animation 3s; 
    display:block;
    height:50px;
    background:red;
    margin-bottom:20px;
}
.change {width: 50%;}

  $('.test').hover( function() {
        $(this).toggleClass('change'); 
});

Any ideas how to do this? 任何想法如何做到这一点?

hope this will help. 希望这会有所帮助。

  $('.test').hover(
      function(){
    $('div').css("-webkit-animation-play-state", "paused");
//do your stuff here

    },
      function(){
        $('div').css("-webkit-animation-play-state", "running");
    });

What about, just use CSS with transition ? 怎么样,只需将CSS与transition一起使用? (in your case) (根据您的情况)

div {
    display:block;
    width:100%;
    height:50px;
    background:red;
    margin-bottom:20px;
    -webkit-transition: all 1s ease-in-out;
    -moz-transition: all 1s ease-in-out;
    -o-transition: all 1s ease-in-out;
    transition: all 1s ease-in-out;
}
div:hover {width:50%;}

Demo : http://jsfiddle.net/TmFFx/5/ 演示: http : //jsfiddle.net/TmFFx/5/

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

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