CSS 3 Stop Rotate Animation After N seconds after page load - html

I don't have much knowledge in css3 and I got this code fragment that makes a div spin infinitely.
.spin {
-webkit-animation: rotation 0.4s linear infinite;
animation: rotation 0.4s linear infinite;
-webkit-transform: translateX(100%) translateY(-100%) rotate(45deg);
transform: translateX(100%) translateY(-100%) rotate(45deg);
}
#-webkit-keyframes rotation {
0% { -webkit-transform: rotate(0deg); }
50% { -webkit-transform: rotate(-180deg); }
100% { -webkit-transform: rotate(-360deg); }
}
#keyframes rotation {
0% { transform: rotate(0deg); }
50% { transform: rotate(-180deg); }
100% { transform: rotate(-360deg); }
}
I want to stop the rotation after a couple of seconds but I don't know what to change. I tried changing infinite to 3 but it stopped and placed my div somewhere else. I need a smooth stop and retains the div original position.

see here jsfiddle
the element was moving because of the translate
code :
.spin {
height:50px;
width:50px;
background:red;
-webkit-animation: rotation 0.4s linear 3;
animation: rotation 0.4s linear 3;
-webkit-transform: rotate(45deg);
transform: rotate(45deg);
}
you should know that the infinite is a value of the animation-iteration-count property which specifies the number of times an animation should be played. so not the seconds.
if you want to stop after N seconds use animation-duration and i suggest you divide the animation declaration into parts, like this :
jsfiddle
for ex you want the spin to rotate for 2 seconds, if you set animation-duration to 2 seconds, the whole animation will take 2 seconds to complete, and that's not what you want. You want your spin to rotate fast for 2 seconds, so you need to calculate the animation-iteration-count like this 2second/animation-duration
say you want 1 rotation to be completed in 0.5s, you set the animation-duration:0.5s and then animation-iteration-count to 2s/0.5 = 4
code :
spin {
height:50px;
width:50px;
background:red;
-webkit-transform: rotate(45deg);
transform: rotate(45deg);
animation-name:rotation;
animation-delay:0s;
animation-duration:0.5s;
animation-iteration-count:4;
animation-fill-mode:backwards;
}
for more info about animations, read here : CSS3 animation Property

Related

how to animate elements to move in a circular path using CSS

I want my element, which is a text, to move in a circular path. I need to use CSS for this. I am attaching my code for the same. The error I face is I need to translate my element to the circumference of the circle. This causes the text to jump to the circumference at the start and the end of the animation. How do I solve this? Is there another method to animate objects to move in a circular direction?
.element{
position:relative;
top:50%;
left:50%;
transform:translate(-50%,-50%);
animation: orbit2 4s forwards;
}
#-webkit-keyframes orbit2 {
from { -webkit-transform: rotate(0deg) translateX(150px) rotate(0deg); }
to { -webkit-transform: rotate(360deg) translateX(150px) rotate(-360deg); }
}
<div class= "element">
<a>NEN</a>
</div>
Something like this (or can even change the way the text wraps around the circle):
#spin {
position:absolute;
top:25%;
left:50%;
margin:50px;
animation: orbit 4s forwards infinite;
animation-timing-function: linear;
}
#-webkit-keyframes orbit {
from { -webkit-transform: rotate(0deg) translateX(150px) rotate(0deg); }
to { -webkit-transform: rotate(360deg) translateX(150px) rotate(-360deg); }
}
<div id="spin"><p>text</p></div>

To let the animation complete on mouse leave

I have written keyframe animation on hover.
#keyframes juggle {
0%{ transform: skew(15deg, 15deg) translate(0,10px);}
50%{ transform: skew(-15deg, -15deg) translate(0,-10px);}
100%{ transform: skew(15deg, 15deg) translate(0,0);}
}
span:hover{ animation: juggle 1s; }
The animation works fine but on mouse leave, it stops abruptly.
The expectation is to complete the animation even on mouse leave.
Is it possible to do it without jquery.
Hope this works
let section = document.getElementById('homeSection');
let elmnt = section.getElementsByClassName('classname');
for (let i = 0; i < elmnt.length; i++) {
elmnt[i].addEventListener('mouseenter', function () {
elmnt[i].classList.add('Juggle');
setTimeout(function(){
elmnt[i].classList.remove('Juggle')
},400)
})
}
I believe this is something you trying to do
span {
animation-name: juggle-out;
animation-duration: 1s;
animation-repeat-count: 1;
animation-fill-mode: forwards;
}
span:hover{
animation-name: juggle-in;
}
#keyframes juggle-in {
0%{ transform: skew(15deg, 15deg) translate(0,10px);}
50%{ transform: skew(-15deg, -15deg) translate(0,-10px);}
100%{ transform: skew(15deg, 15deg) translate(0,0);}
}
#keyframes juggle-out {
0%{ transform: skew(15deg, 15deg) translate(0,10px);}
50%{ transform: skew(-15deg, -15deg) translate(0,-10px);}
100%{ transform: skew(0deg, 0deg) translate(0,0);}
}
you should use 2 animations for hover in and hover out
actually there is no hover in and hover out in CSS
It's just a state but the animation-fill-mode plays an important role
The support is not very good though ;)
I recommend adding class instead of this
Use the onmouseover function to add a class which contains the animation and manually remove the class using settimeout function.

Css rotate element 360 back and forth

As the question says I'd like to rotate an icon 360 degrees one way the rotate back the other repeatedly. Going one direction is easy enough what I don't understand is stopping and going the other direction.
#loading {
-webkit-animation: rotation 2s infinite linear;
}
#-webkit-keyframes rotation {
from {
-webkit-transform: rotate(0deg);
}
to {
-webkit-transform: rotate(359deg);
}
}
<i id="loading" class="material-icons">autorenew</i>
I have tried creating another rotation going the other direction but it doesn't seem to apply.
#-webkit-keyframes rotationBackwards {
from {
-webkit-transform: rotate(359deg);
}
to {
-webkit-transform: rotate(0deg);
}
}
Transformation doesn't apply on inline elements. You have to make your element a block-level element instead (See Transformable Elements on the specifications - If you include the Martial Icons, this will be set by default).
The Animation itself can simply be done with a rotation to 360 degrees for the first half (50%) and a rotation back to 0 degrees for the second half. Mind that the duration of the animation splits into both directions (given your 2s animation, every direction will take 1s).
#loading {
display: inline-block;
animation: rotation 2s infinite linear;
}
#keyframes rotation {
50% {
transform: rotate(360deg);
}
100% {
transform: rotate(0deg);
}
}
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<i id="loading" class="material-icons">autorenew</i>
Here is another idea by simply using alternate value of animation-direction and by keeping your initial animation:
#loading {
animation: rotation 2s infinite linear alternate;
}
#keyframes rotation {
/*from {
transform: rotate(0deg); no needed to define this
}*/
to {
transform: rotate(360deg);
}
}
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<i id="loading" class="material-icons">autorenew</i>

CSS Animation: Works in Chrome but not in Firefox?

In rotate animation, works in Chrome but not in Firefox. Why?
#-moz-keyframes rotate {
from { -moz-transform: rotate(0deg); }
to { -moz-transform: rotate(360deg); }
}
#-webkit-keyframes rotate {
from { -webkit-transform: rotate(0deg); }
to { -webkit-transform: rotate(360deg); }
}
#example {
background: red;
width: 100px;
height: 100px;
-moz-animation: rotate 20s linear 0 infinite;
-webkit-animation: rotate 20s linear 0 infinite;
}
http://jsfiddle.net/WsWWY/
Current Firefox implementations fail unless time values of 0 have units. Use 0s or 0ms.
http://jsfiddle.net/WsWWY/1/
Note: The W3C explicitly allows for the number 0, without units, to be a length value, but it says no such thing for other values. Personally, I hope this changes, but for the time being the Firefox behavior is not incorrect.

HTML5 Transition and Transform effect.

When I click a button, I wanna do some things listed below using HTML5 and CSS3. But I don't know how can I achieve these things at the same time.
When I click a button:
Change element A's CSS3 property -webkit-transform to rotateY(180deg) in 1 second (using -webkit-transition:-webkit-transform 1s;)
Change element A's CSS3 property -webkit-transform to scale(0.8) in 0.5 second (using -webkit-transition:-webkit-transform 0.5s;). Then change element A's CSS3 property -webkit-transform back to scale(1) in 0.5 second (using -webkit-transition:-webkit-transform 0.5s;).
Are there any solutions about this issue?
Thank you!
You'd have to use animation rather than transitions:
#-webkit-keyframes myAnimation{
from {
-webkit-transform: rotate(0deg) scale(1);
}
50% {
-webkit-transform: rotate(90deg) scale(0.8);
}
to {
-webkit-transform: rotate(180deg) scale(1);
}
}
div {
-webkit-animation-duration: 1s;
}
div:hover {
-webkit-animation-name: myAnimation;
-webkit-transform: rotate(180deg) scale(1);
}
Here's a demo.