admin管理员组文章数量:1305731
Is there any way to do that without javascript or do I have to use JS to append/remove class from an object? Could you guys show me some live example? Right now I have something that works only on hover of the object:
.clicker:hover + .circle {
-webkit-animation: rotor 1.5s linear 0s infinite normal;
-mox-animation: rotor 1.5s linear 0s infinite normal;
-o-animation: rotor 1.5s linear 0s infinite normal;
animation: rotor 1.5s linear 0s infinite normal;
}
.paused{
-webkit-animation-play-state:paused;
-moz-animation-play-state:paused;
-o-animation-play-state:paused;
animation-play-state:paused;
}
Is there any way to do that without javascript or do I have to use JS to append/remove class from an object? Could you guys show me some live example? Right now I have something that works only on hover of the object:
.clicker:hover + .circle {
-webkit-animation: rotor 1.5s linear 0s infinite normal;
-mox-animation: rotor 1.5s linear 0s infinite normal;
-o-animation: rotor 1.5s linear 0s infinite normal;
animation: rotor 1.5s linear 0s infinite normal;
}
.paused{
-webkit-animation-play-state:paused;
-moz-animation-play-state:paused;
-o-animation-play-state:paused;
animation-play-state:paused;
}
Share
Improve this question
asked Sep 27, 2017 at 7:34
user8626881user8626881
5
- There is no click Action in CSS – Matthias Seifert Commented Sep 27, 2017 at 7:36
- you have to use javascript or jquery.. – Shadow Fiend Commented Sep 27, 2017 at 7:37
- so could you show me how? – user8626881 Commented Sep 27, 2017 at 7:37
- [ codepen.io/CWiech05/pen/… ] take a look at this sample, do you mean something like that? – Reza P. Commented Sep 27, 2017 at 7:41
- on your click, it Stops and Starts the animation again – Reza P. Commented Sep 27, 2017 at 7:41
3 Answers
Reset to default 3Assuming this (from your CSS)...
<div class="clicker">[Your Content]</div>
<div class="circle">[Your Content]</div>
WITH PURE JAVASCRIPT:
<div class="clicker" onclick="this.nextElementSibling.classList.toggle('paused');">[Your Content]</div>
<div class="circle">[Your Content]</div>
WITH JQUERY:
$('.clicker').click(function(){
$(this).next('.circle').toggleClass('paused');
});
You haven't post your html, so the JavaScript/jQuery selectors I'm using are base on your CSS. For a more specific answer with the best selectors, you should show your html.
EDIT:
I've just realized that with this solution you could have a problem with your CSS, because .circle
styles prevails over .paused
, so the class is been toggled but the styles aren't changing. You can easily solve it adjusting your CSS...
.clicker + .circle {
-webkit-animation: rotor 1.5s linear 0s infinite normal;
-mox-animation: rotor 1.5s linear 0s infinite normal;
-o-animation: rotor 1.5s linear 0s infinite normal;
animation: rotor 1.5s linear 0s infinite normal;
}
.paused {
-webkit-animation-play-state: paused !important;
-moz-animation-play-state: paused !important;
-o-animation-play-state: paused !important;
animation-play-state: paused !important;
}
I hope it helps
This is not possible in CSS, however if you are interested in a JavaScript example:
$(element).on('click', function () {
if ($(target).hasClass('paused'))
{
$(target).removeClass('paused');
}
else
{
$(target).addClass('paused');
}
});
Replace element
with the button or anchor that activates the class removal / addition. And replace target
with the element that should animate.
This solution requires jQuery.
var active = document.querySelector('.active');
var div = document.querySelector('.wrap');
var hasAnimationPaused = false;
var aniamtionPausedClass = 'paused'
div.className += ' circle';
active.addEventListener('click', function(e) {
var classNames = div.className.split(' ');
if (!hasAnimationPaused) {
div.className += ' ' + aniamtionPausedClass;
hasAnimationPaused = true;
} else {
classNames.splice(classNames.indexOf(aniamtionPausedClass), 1);
div.className = classNames.join(' ');
hasAnimationPaused = false;
}
})
.wrap {
color: black;
}
@keyframes rotor {
from {
color: red;
}
50% {
color: yellow;
}
to {
color: blue;
}
}
.circle {
-webkit-animation: rotor 1.5s linear 0s infinite normal;
-mox-animation: rotor 1.5s linear 0s infinite normal;
-o-animation: rotor 1.5s linear 0s infinite normal;
animation: rotor 1.5s linear 0s infinite normal;
}
.paused{
-webkit-animation-play-state:paused;
-moz-animation-play-state:paused;
-o-animation-play-state:paused;
animation-play-state:paused;
}
<div class="wrap"> TEst Test</div>
<button class="active">CLick</button>
本文标签: javascriptCSS Animationsstart and stop infinite animation on clickStack Overflow
版权声明:本文标题:javascript - CSS Animations, start and stop infinite animation on click - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741793527a2397801.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论