Center align rotating SVG in Loader CSS - html

I am trying to animate a SVG from 0deg to 360deg. But if i use the transform: rotate property then the svg loses its position and its not centre aligned when the browser resizes. I used transform-origin to 50%. But the svg loses its position.
HTML :
<div id="hexagon-spinner">
<Hexagon className="hexagon-loader" viewBox="0 0 65.103 75.174" />
</div>
#hexagon-spinner {
position: fixed;
width: 100%;
left: 0;
right: 0;
top: 0;
bottom: 0;
background-color: rgba(255, 255, 255, 0.7);
z-index: 9999;
}
.hexagon-loader {
animation-name: spin;
animation-duration: 0.8s;
/* Things added */
animation-iteration-count: infinite;
transform-origin: -50% 50%;
display: inline-block;
position: absolute;
top: 50%;
left: 50%;
}
#-webkit-keyframes spin {
0% {
-webkit-transform: rotate(0deg);
}
100% {
-webkit-transform: rotate(359deg);
}
}
#keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(359deg);
}
}

First of all, when it is 100%, you should define 360 degrees, not 359 degrees.
100% {
transform: rotate(359deg); // ->> 360deg
}
What to do about the average,
#keyframes spin {
0% {
transform: rotate(0deg);
transform-origin: -50% 50%;
}
100% {
transform: rotate(360deg);
transform-origin: -50% 50%;
}
}
Finally,
If we need to shorten the code (since it will start with 0deg by default), if we enter only the parameter 100%, there will be no problem.
#keyframes spin {
100% {
transform: rotate(360deg);
transform-origin: -50% 50%;
}
}
Simple Code Snippet
#keyframes spin {
100% {
transform: rotate(360deg);
transform-origin: -50% 50%;
}
}
div {
position: absolute;
top: 50%;
left: 50%;
animation-name: spin;
animation-duration: 0.8s;
/* Things added */
animation-iteration-count: infinite;
transform-origin: -50% 50%;
}
<div>LOADING</div>
#hexagon-spinner {
position: fixed;
width: 100%;
left: 0;
right: 0;
top: 0;
bottom: 0;
background-color: rgba(255, 255, 255, 0.7);
z-index: 9999;
display: flex;
align-items:center;
justify-content: center;
}
.hexagon-loader {
background-color: purple;
height: 40px;
width: 40px;
animation-name: spin;
animation-duration: 0.8s;
/* Things added */
animation-iteration-count: infinite;
display: inline-block;
}
#-webkit-keyframes spin {
0% {
-webkit-transform: rotate(0deg);
}
100% {
-webkit-transform: rotate(359deg);
}
}
#keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(359deg);
}
}
<div id="hexagon-spinner">
<div class="hexagon-loader"></div>
</div>
When we shift the element with the translate (to center it) we naturally distort its center. Therefore it will not work properly.
I suggest a solution for this. (flexbox) is to use. You will see an example below.
Note: (Don't forget to remove Absolute and Transform Origin features)

Related

Car animation from left to right and then hide car image

I need to make an animation of a moving car from left to right and then hide the picture and another one of the same, but from right to left.
Can someone help me with this?
This is my code for now:
.car-movement {
position: absolute;
top: 65%;
left: 0;
-webkit-animation: linear infinite;
-webkit-animation-name: run;
-webkit-animation-duration: 5s;
}
#-webkit-keyframes run {
0% {
left: 0;
}
50% {
left: calc(100% - 100px);
}
100% {
left: 0;
}
}
<img class="car-movement" src="/assets/img/1car.svg" alt="car">
Place the image in a div that has overflow.
Animate over transform: rotateY and left.
I used a div with a car emoticon, instead of an image.
.car-movement {
overflow: hidden;
font-size: 40px;
height: 50px;
}
.car-movement > .car {
position: relative;
display: inline-block;
animation: linear infinite;
animation-name: run;
animation-duration: 5s;
}
#keyframes run {
0% {
transform: rotateY(180deg);
left: -100px;
}
50% {
transform: rotateY(180deg);
left: 100%;
}
51% {
transform: rotateY(0deg);
left: calc(100% + 100px);
}
100% {
transform: rotateY(0deg);
left: -100px;
}
<div class="car-movement">
<div class="car">🚕</div>
</div>
If I understood correctly, you want that car would ride from left to right and hide on the right side of the window?
Then you should edit this code part:
0% {
left: -100px;
}
50% {
left: calc(100% - 100px);
}
Instead of 100px write car image length.
Also before all code, you should write this for the body:
body {
overflow-x: hidden;
}
.car-movement {
position: absolute;
top: 65%;
left: 0;
-webkit-animation: linear infinite;
-webkit-animation-name: run;
-webkit-animation-duration: 5s;
}
#-webkit-keyframes run {
0% {
left: 0;
}
48% {
-webkit-transform: rotateY(0deg);
}
50% {
left: calc(100% - 100px);
-webkit-transform: rotateY(180deg);
}
98% {
-webkit-transform: rotateY(180deg);
}
100% {
left: 0;
-webkit-transform: rotateY(0deg);
}
}
<img class="car-movement" src="/assets/img/1car.svg" alt="car">
If you give your keyframes a couple of intermediate steps you can not only get the car going from left to right and back again but you can get it to turn round before doing the right to left bit.
Obviously you need to put in your car svg image to get the full effect.
.car-movement {
position: absolute;
top: 65%;
left: 0;
animation: linear infinite;
animation-name: run;
animation-duration: 5s;
}
#keyframes run {
0% {
left: 0;
transform: translate(-100%);
}
50% {
left: 100%;
transform: translate(0);
}
50.5% {
transform: rotateY(180deg) translate(0);
}
99.5% {
left: 0;
transform: rotateY(180deg) translate(0);
}
100% {
transform: rotateY(0deg) translate(0);
}
}
<img class="car-movement" src="/assets/img/1car.svg" alt="car">

How to move an element linearly while rotating with css

When i use transform on one element for translate3d and rotate3d then the element starts to orbit. i want linear motion while rotating.
i have used webkit animations in css
img{height:50px;
width:50px;
animation:tt;
animation-duration:10s;
position:relative;
top:40vh;
left:40vw;}
#keyframes tt
{ 0%{
transform:rotate3d(0,0,0,0) translate3d(0,0,0);
}
50%{
transform:rotate3d(0,0,1,2000deg) translate3d(300px,0,0);
}
}
i wanted it move forward while rotating like a cars tire instead its like a comet or excited electron
You could use the left and right CSS properties, similar example on https://www.w3schools.com/css/css3_animations.asp
I have included the snippet below:
https://codepen.io/mohamedhmansour/pen/bOONQr
img {
height: 50px;
width: 50px;
animation: tt;
animation-duration: 5s;
position: relative;
top: 0;
left: 0;
}
#keyframes tt {
0% {
transform: rotate(0deg);
left: 0px;
}
50% {
transform: rotate(-360deg);
left: 100%;
}
100% {
transform: rotate(0deg);
left: 0px;
}
}
<div class="wrapper">
<img src="http://blog.codepen.io/wp-content/uploads/2012/06/Button-Fill-Black-Small.png" />
</div>
https://codepen.io/mohamedhmansour/pen/bOONQr
You didn't define the 100% state so by default it will be the a transform:none (the default value since no transform is defined on the element) which is creating the issue. You should define it and use a bigger value for the angle by keeping the same axis to keep your element on the orbit:
.img {
height: 50px;
width: 50px;
animation: tt linear 10s;
position: relative;
top: 40vh;
left: 40vw;
background:red;
}
#keyframes tt {
0% {
transform: rotate3d(0, 0, 0, 0) translate3d(0, 0, 0);
}
50% {
transform: rotate3d(0, 0, 1, 2000deg) translate3d(100px, 0, 0);
}
100% {
transform: rotate3d(0, 0, 1, 4000deg) translate3d(100px, 0, 0);
}
<div class="img"></div>
And if you want a linear animation simply do like this (translation before rotation):
.img {
height: 50px;
width: 50px;
animation: tt linear 5s forwards;
position: relative;
top: 40vh;
left: 40vw;
background:red;
}
#keyframes tt {
0% {
transform: translateX(0) rotate(0);
}
100% {
transform: translateX(100px) rotate(360deg);
}
<div class="img"></div>
You can try this:
img{
height: 50px;
width: 50px;
position: absolute;
top:40vh;
left:40vw;
-webkit-animation: spinner 10s linear infinite;
left: 0;
}
#-webkit-keyframes spinner{
50%{
-webkit-transform: rotate(1440deg);
left: calc(100% - 200px);
}
}
<img src="https://www.gstatic.com/webp/gallery/4.sm.jpg">

Making a heart pulsate with CSS

I have made a heart using CSS. I was just looking for a way to make it beat / pulsate.
Here is the code (fiddle):
#heart {
position: relative;
width: 100px;
height: 90px;
}
#heart:before,
#heart:after {
position: absolute;
-webkit-animation: heart 1s linear infinite;
content: "";
left: 50px;
top: 0;
width: 50px;
height: 80px;
background: red;
-moz-border-radius: 50px 50px 0 0;
border-radius: 50px 50px 0 0;
-webkit-transform: rotate(-45deg);
transform: rotate(-45deg);
-webkit-transform-origin: 0 100%;
transform-origin: 0 100%;
}
#heart:after {
left: 0;
-webkit-transform: rotate(45deg);
transform: rotate(45deg);
-webkit-transform-origin: 100% 100%;
transform-origin: 100% 100%;
}
<div id="heart"></div>
What a lovely post to begin the evening with.
Sure, this is possible with pure CSS - you are likely interested in animations:
CSS
#heart-container {
width: 100px;
height: 90px;
animation: pulsate 0.5s infinite;
}
#keyframes pulsate {
0% {
transform: scale(1);
}
50% {
transform: scale(1.5);
}
100% {
transform: scale(1);
}
}
Working example on JSFiddle.
Just wrap your lovely heart in #heart-container, and you are good to go. Also, don't forget to include the vendor specific prefixes where necessary.
FYI, the animation shorthand property - similar to transition - also accepts an easing setting (timing-function), like ease-in-out. Or, use animation-timing-function.
See the animation easing example on JSFiddle.
Note
For anyone wondering why I suggested this approach instead of an alternating animation, this approach synergizes with easing functions pretty well.
The animation property is your friend. :)
#heart {
position: relative;
width: 100px;
height: 90px;
-webkit-animation: heartbeat .8s ease-in-out 0s infinite;
-moz-animation: heartbeat .8s ease-in-out 0s infinite;
animation: heartbeat .8s ease-in-out 0s infinite;
transform: scale(1);
}
#-moz-keyframes heartbeat {
0% {transform: scale(1);}
50% {transform: scale(1.5);}
100% {transform: scale(1);}
}
#-webkit-keyframes heartbeat {
0% {transform: scale(1);}
50% {transform: scale(1.5);}
100% {transform: scale(1);}
}
#keyframes heartbeat {
0% {transform: scale(1);}
50% {transform: scale(1.5);}
100% {transform: scale(1);}
}
#heart:before,
#heart:after {
position: absolute;
content: "";
left: 50px;
top: 0;
width: 50px;
height: 80px;
background: red;
-moz-border-radius: 50px 50px 0 0;
border-radius: 50px 50px 0 0;
-webkit-transform: rotate(-45deg);
transform: rotate(-45deg);
-webkit-transform-origin: 0 100%;
transform-origin: 0 100%;
}
#heart:after {
left: 0;
-webkit-transform: rotate(45deg);
transform: rotate(45deg);
-webkit-transform-origin: 100% 100%;
transform-origin: 100% 100%;
}
<div id = "heart"></div>
Create a beating heart icon using font-awesome, JQuery and CSS animations.
#keyframes heartbeat
{
0%
{
transform: scale( .75 );
}
20%
{
transform: scale( 1 );
}
40%
{
transform: scale( .75 );
}
60%
{
transform: scale( 1 );
}
80%
{
transform: scale(.75);
}
100%
{
transform: scale( 1 );
}
}
create beating heart

Making a CSS3 animation more reliable among browsers

I have this CSS3 animation working on codepen.
HTML
<div class="heart heart1"></div>
<div class="heart heart2"></div>
CSS3
html, body{
width: 100%;
height: 100%;
min-width: 500px;
min-height: 500px;
overflow: hidden;
}
.heart {
position: absolute;
width: 100px;
height: 90px;
top: 50%;
left: 50%;
margin-top: -45px;
margin-left: -50px;
}
.heart:before,
.heart:after {
position: absolute;
content: "";
left: 50px;
top: 0;
width: 50px;
height: 80px;
background: #fc2e5a;
border-radius: 50px 50px 0 0;
transform: rotate(-45deg);
transform-origin: 0 100%;
}
.heart:after {
left: 0;
transform: rotate(45deg);
transform-origin :100% 100%;
}
.heart1{
animation: heart-anim 1s linear .4s infinite;
}
.heart2{
animation: pounding .5s linear infinite alternate;
}
.heart1:after, .heart1:before{
background-color: #ff7693;
}
#keyframes pounding{
0%{ transform: scale(1.5); }
100%{ transform: scale(1); }
}
#keyframes heart-anim {
46% {
transform: scale(1);
}
50% {
transform: scale(1.3);
}
52% {
transform: scale(1.5);
}
55% {
transform: scale(3);
}
100% {
opacity: 0;
transform: scale(50);
}
}
Check it here: http://codepen.io/RadValentin/pen/sfnCE
As you can see is working ok, BUT, if I post the exact code to my local server OR to jsfiddle it does not work any more: http://jsfiddle.net/40aydbfr/
I believe the animation is not made according to the best practices since it breaks very easily.
So, Why it does not work outside of codepen and how can I make it more cross browser compatible.
PS: Im using Chrome.
It doesn't work because you are missing vendor prefixes for -webkit- browsers.
The reason why it works on codepen is because, if you click on the settings button above the CSS window, you'll see that -prefix-free is enabled, which means it adds the prefixes automatically.
Always check browser support, if something doesn't work.
Updated Codepen
Updated Fiddle
html,
body {
width: 100%;
height: 100%;
min-width: 500px;
min-height: 500px;
overflow: hidden;
}
.heart {
position: absolute;
width: 100px;
height: 90px;
top: 50%;
left: 50%;
margin-top: -45px;
margin-left: -50px;
}
.heart:before,
.heart:after {
position: absolute;
content: "";
left: 50px;
top: 0;
width: 50px;
height: 80px;
background: #fc2e5a;
border-radius: 50px 50px 0 0;
transform: rotate(-45deg);
transform-origin: 0 100%;
}
.heart:after {
left: 0;
transform: rotate(45deg);
transform-origin: 100% 100%;
}
.heart1 {
-webkit-animation: heart-anim 1s linear .4s infinite;
animation: heart-anim 1s linear .4s infinite;
}
.heart2 {
-webkit-animation: pounding .5s linear infinite alternate;
animation: pounding .5s linear infinite alternate;
}
.heart1:after,
.heart1:before {
background-color: #ff7693;
}
#-webkit-keyframes pounding {
0% {
transform: scale(1.5);
}
100% {
transform: scale(1);
}
}
#keyframes pounding {
0% {
transform: scale(1.5);
}
100% {
transform: scale(1);
}
}
#-webkit-keyframes heart-anim {
46% {
transform: scale(1);
}
50% {
transform: scale(1.3);
}
52% {
transform: scale(1.5);
}
55% {
transform: scale(3);
}
100% {
opacity: 0;
transform: scale(50);
}
}
#keyframes heart-anim {
46% {
transform: scale(1);
}
50% {
transform: scale(1.3);
}
52% {
transform: scale(1.5);
}
55% {
transform: scale(3);
}
100% {
opacity: 0;
transform: scale(50);
}
}
<div class="heart heart1"></div>
<div class="heart heart2"></div>

left and top properties are not animated

In the animation below the transform is animated correctly, but the left and top properties are not. Why is this?
.element-animation {
background-color: yellow;
width: 30px;
height: 30px;
animation: animationFrames ease 2s;
animation-iteration-count: infinite;
}
#keyframes animationFrames {
0% {
left: 0px;
top: 0px;
opacity: 1;
transform: rotate(0deg) scaleX(1) scaleY(1) skewX(0deg) skewY(0deg);
}
25% {
left: 0px;
top: -90px;
}
75% {
left: 200px;
top: -90px;
}
100% {
left: 200px;
top: 0px;
opacity: 1;
transform: rotate(0deg) scaleX(2) scaleY(2) skewX(45deg) skewY(45deg);
}
}
<div class="element-animation"></div>
Your animation relies on positioning, therefore you have to add a position property:
.element-animation{
position:relative;
}
.element-animation {
background-color: yellow;
width: 30px;
height: 30px;
animation: animationFrames ease 2s;
animation-iteration-count: 1;
position: relative;
}
#keyframes animationFrames {
0% {
left: 0px;
top: 0px;
opacity: 1;
transform: rotate(0deg) scaleX(1) scaleY(1) skewX(0deg) skewY(0deg);
}
25% {
left: 0px;
top: -90px;
}
75% {
left: 200px;
top: -90px;
}
100% {
left: 200px;
top: 0px;
opacity: 1;
transform: rotate(0deg) scaleX(1) scaleY(1) skewX(0deg) skewY(0deg);
}
}
<div class="element-animation"></div>
For older browsers you may need to add the -webkit- prefix for the animation property. Check browser compatibility over on caniuse.com
You should copy all the code for every Browser. not just standard.
so it should contain the following stuff
-webkit-animation: animationFrames linear 0.7s;
-webkit-animation-iteration-count: 1;
-webkit-transform-origin: ;
see in http://jsfiddle.net/KxM68/8/