I have bubbles that float up using keyframes, but they are buttons that I also want to disappear when clicked and then have it automatically restart at 0%.
I have been calling onmousedown
and onmouseup
to do this but it doesn't seem to be working. Any ideas?
$(document).ready() {
function Bubbles() {
$(".bubble_cluster_one").css("opacity", "0");
}
function Bubbles2() {
$("bubble_cluster_one").css("top": "400px", "opacity": "1");
}
}
.bubble_cluster_one {
position: absolute;
margin: 0;
padding: 0;
-webkit-animation: bubble_cluster_one 8s infinite;
left: 150px;
z-index: +1;
}
.bubble_cluster_one input {
width: 40px;
height: 60px;
}
@-webkit-keyframes bubble_cluster_one {
0% {
top: 400px;
}
100% {
top: -70px;
}
}
<div class="bubble_cluster_one">
<input type="image" src="bubbles_1.png" alt="button" onmousedown="Bubbles()" onmouseup="Bubbles2()">
</div>
JavaScript content That's because the only way to play an animation again is to remove the animation effect, let the document recompute styles so that it knows you've done that, then add the animation effect back to the element.
To restart animation in CSS3 and JavaScript, we can get the offsetHeight property to trigger reflow. function resetAnimation() { const el = document. getElementById("animated"); el. style.
More "Try it Yourself" examples below. The @keyframes rule specifies the animation code. The animation is created by gradually changing from one set of CSS styles to another. During the animation, you can change the set of CSS styles many times.
In our stylesheet, we can utilise @keyframes to describe our wobbling process! @keyframes allow us to storyboard the animation process. We can describe the transformations of the target element at any point of the animation cycle. In replace of 'step', we can use 0-100% to mark the animation frame, or use 'from' and 'to' to show a transition.
Definition and Usage The @keyframes rule specifies the animation code. The animation is created by gradually changing from one set of CSS styles to another. During the animation, you can change the set of CSS styles many times.
Definition and Usage. The @keyframes rule specifies the animation code. The animation is created by gradually changing from one set of CSS styles to another. During the animation, you can change the set of CSS styles many times.
There were a couple of problems in the snippet that was provided in question:
$(document).ready
wrapper was written wrongly (it should be written as mentioned in Roamer-1888's answer and the event handlers should be directly attached via JS. This is actually the best practice instead of using inline attributes.Other than those two the following are the things that you need to note regarding CSS animations:
opacity: 0
, the animation was running in the background.The above cannot be done with mousedown
and mouseup
events because when the animation is removed (on mousedown
) the element goes back to its original position and the chances are very high that your mouse pointer is no longer over the element. This means the mouseup
event would not get fired because it fires only when the mouse is still over the element.
From jQuery website: The mouseup event is sent to an element when the mouse pointer is over the element, and the mouse button is released.
So the solution is to do the following:
animation
properties alone, add this extra class to the element. This is not mandatory but makes it easier to add or remove the animation by just add/remove class methods.click
event on the element, first remove the animation class, set opacity
to 0
to hide the element and after a small delay (using setTimeout
) add the animation class back to the element and set the opacity
back to 1.$(document).ready(function() {
$(".bubble_cluster_one input").on('click', function() {
$(".bubble_cluster_one").removeClass("animation");
$(".bubble_cluster_one").css("opacity", "0");
setTimeout(
function() {
$(".bubble_cluster_one").addClass("animation");
$(".bubble_cluster_one").css("opacity", "1");
}, 100);
});
});
.bubble_cluster_one {
position: absolute;
margin: 0;
padding: 0;
left: 150px;
z-index: 1;
}
.animation {
animation: bubble_cluster_one 8s infinite;
}
.bubble_cluster_one input {
width: 40px;
height: 60px;
}
@keyframes bubble_cluster_one {
0% {
top: 400px;
}
100% {
top: -70px;
}
}
<!-- prefix free library is only to avoid vendor prefixes -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="bubble_cluster_one animation">
<input type="image" src="bubbles_1.png" alt="button">
</div>
Just adding top: 400px
on mouseup
would not work because when a CSS animation is running, the animation takes full control over properties that are being used by it. Only rules that can override the animation are those which have !important
. It is generally bad practice to use !important
when it can be avoided and so I won't recommend that way of restarting an animation.
From Animations Spec: CSS Animations affect computed property values. During the execution of an animation, the computed value for a property is controlled by the animation. This overrides the value specified in the normal styling system. Animations override all normal rules, but are overriden by !important rules.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With