I am working on image transforms, rotates, and keyframes, but I am having a bit of an issue trying to get an image to spin in a circle. I have it set to rotate 360deg and two keyframes 0 and 100. I am wanting the image to spin from the center point of the image, so it looks as if the loading circle is loading something.
Right now it seems the image spins from the top-left corner.
Does anyone see what I am doing wrong?
#spinning-circle {
animation-name: spinning-circle;
animation-duration: 10s;
animation-iteration-count: infinite;
width: 40px;
height: 40px;
}
#-webkit-keyframes spinning-circle {
0% {
-webkit-transform: rotate(0deg);
transform: rotate(0deg);
}
100% {
-webkit-transform: rotate(360deg);
transform: rotate(360deg);
}
}
<div id="spinning-circle">
<img src="http://i.stack.imgur.com/WbNlQ.jpg">
</div>
Add
#spinning-circle img {
width: 100%;
height: auto;
}
to your styles
It's spinning on an offset because the image is bigger than the container. If you wish to move the origin of transformations, use the transform-origin property
You need to give it a transform-origin property
#spinning-circle {
animation-name: spinning-circle;
animation-duration: 5s;
animation-iteration-count: infinite;
width: 40px;
height: 40px;
}
#-webkit-keyframes spinning-circle {
0% {
-webkit-transform: rotate(0deg);
transform: rotate(0deg);
-webkit-transform-origin: 125% 125%;
transform-origin: 125% 125%;
}
100% {
-webkit-transform: rotate(360deg);
transform: rotate(360deg);
-webkit-transform-origin: 125% 125%;
transform-origin: 125% 125%;
}
}
<div id="spinning-circle">
<img src="http://i.stack.imgur.com/WbNlQ.jpg">
</div>
Related
I'm trying to create an animation for a three-letter word. For now, take the word XYZ. The idea is that each letter of XYZ will move in a circular path of different radii and directions (Like one letter moving right, another left, and bottom) before coming back to the original position. I have tried using different forms of code for this but am failing because I don't clearly understand how to animate circular motion with a fixed origin. Will be helpful if anyone could share how to do this. I am also attaching part of my code
.animation-container {
display: flex;
position: relative;
top: 10rem;
left: 50%;
align-items: center;
text-align: center;
}
.letter {
animation: move-letter 4s ease-in-out infinite;
animation-iteration-count: infinite;
}
#keyframes move-letter {
from {
-webkit-transform: rotate(0deg) translateX(150px) rotate(0deg);
}
to {
-webkit-transform: rotate(360deg) translateX(150px) rotate(-360deg);
}
}
<div class="animation-container">
<div class="letter X">X</div>
<div class="letter Y">Y</div>
<div class="letter Z">Z</div>
</div>
It depends what you actually want, but you definitely need three different animations (i.e. with different settings), and 3 or more stages per animation, returning to the first position in each case:
.animation-container {
display: flex;
position: relative;
top: 10rem;
left: 50%;
align-items: center;
text-align: center;
}
.letter.X {
animation: move-letter_x 4s ease-in-out infinite;
animation-iteration-count: infinite;
}
.letter.Y {
animation: move-letter_y 4s ease-in-out infinite;
animation-iteration-count: infinite;
}
.letter.Z {
animation: move-letter_z 4s ease-in-out infinite;
animation-iteration-count: infinite;
}
#keyframes move-letter_x {
0% {
-webkit-transform: rotate(0deg) translateX(150px) rotate(0deg);
}
50% {
-webkit-transform: rotate(360deg) translateX(20px) rotate(-360deg);
}
100% {
-webkit-transform: rotate(0deg) translateX(150px) rotate(0deg);
}
}
#keyframes move-letter_y {
0% {
-webkit-transform: rotate(360deg) translateX(150px) rotate(-360deg);
}
50% {
-webkit-transform: rotate(0deg) translateX(80px) rotate(0deg);
}
100% {
-webkit-transform: rotate(360deg) translateX(150px) rotate(-360deg);
}
}
#keyframes move-letter_z {
0% {
-webkit-transform: rotate(0deg) translateX(150px) rotate(0deg);
}
50% {
-webkit-transform: rotate(360deg) translateX(140px) rotate(-360deg);
}
100% {
-webkit-transform: rotate(0deg) translateX(150px) rotate(0deg);
}
}
<div class="animation-container">
<div class="letter X">X</div>
<div class="letter Y">Y</div>
<div class="letter Z">Z</div>
</div>
I recently found a flying bird animation that I have been attempting to implement into one of my recent projects. I have attempted to reciprocate the same exact animation, yet the animation appears to be broken on safari browsers (both mobile and desktop). I am approximately 95% certain that most of my css is correct, and that every property that requires it, has the -webkit prefix for safari.
With all that said, I have provided below a link to a Codepen that contains my exact code for the animation. As I mentioned before, if you open up the animation on Google Chrome, it works perfectly fine. Yet, when it is opened up on Safari, the animation acts completely different. Here are the primary issues I am noticing:
The 'Flying' animation appears to not be scrolling the bird background image properly(as this animation is meant to act like a flip book), which results in a previous frame of the bird appearing.
On Chrome, when the window is resized, the birds flight path adjusts to the width of the window, as I set the 'fly-across-1' animation (uses the transform property) to be responsive to the view window width. However, when you resize (try expanding the window) a safari window, the birds 'flight' does not reach the end of the window.
Not sure if any of this makes sense, but I have completely run out of mental capacity trying to adjust for the issues myself. If anyone has any idea what I am missing or has any suggestions, I would greatly appreciate the help. Thanks again.
Here is the Codepen
For reference, here is the css code:
.container {
position: relative;
overflow: hidden;
display: flex;
align-items: center;
justify-content: center;
min-height: 35rem;
}
.bird-box {
position: absolute;
top: 20%;
left: -10%;
transform: translateX(-10vw);
-webkit-transform: translateX(-10vw);
will-change: transform;
}
.bird-box-one {
animation: fly-across-1 7s linear infinite;
-webkit-animation: fly-across-1 7s linear infinite;
}
.bird {
background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/174479/bird-cells-new.svg);
background-size: auto 100%;
width: 22px;
height: 31.25px;
will-change: background-position;
animation: flying;
-webkit-animation: flying;
animation-timing-function: steps(10);
-webkit-animation-timing-function: steps(10);
animation-iteration-count: infinite;
-webkit-animation-iteration-count: infinite;
}
.bird-one {
animation-duration: 1.3s;
-webkit-animation-duration: 1.3s;
animation-delay: -0.5s;
-webkit-animation-delay: -0.5s;
}
#keyframes flying {
100% {
background-position: -900px 0;
}
}
#-webkit-keyframes flying {
0% {
background-position: 0 0;
}
100% {
background-position: -900px 0;
}
}
#keyframes fly-across-1 {
0% {
transform: translateX(-10vw);
-webkit-transform: translateX(-10vw);
}
10% {
transform: translateY(-2vh) translateX(10vw);
-webkit-transform: translateY(-2vh) translateX(10vw);
}
20% {
transform: translateY(-4vh) translateX(30vw);
-webkit-transform: translateY(-4vh) translateX(30vw);
}
30% {
transform: translateY(-6vh) translateX(50vw);
-webkit-transform: translateY(-6vh) translateX(50vw);
}
40% {
transform: translateY(-5vh) translateX(70vw);
-webkit-transform: translateY(-5vh) translateX(70vw);
}
50% {
transform: translateY(-3vh) translateX(90vw);
-webkit-transform: translateY(-3vh) translateX(90vw);
}
60% {
transform: translateY(-2vh) translateX(110vw);
-webkit-transform: translateY(-2vh) translateX(110vw);
}
100% {
transform: translateY(0) translateX(110vw);
-webkit-transform: translateY(0) translateX(110vw);
}
}
#-webkit-keyframes fly-across-1 {
0% {
transform: translateX(-10vw);
-webkit-transform: translateX(-10vw);
}
10% {
transform: translateY(-2vh) translateX(10vw);
-webkit-transform: translateY(-2vh) translateX(10vw);
}
20% {
transform: translateY(-4vh) translateX(30vw);
-webkit-transform: translateY(-4vh) translateX(30vw);
}
30% {
transform: translateY(-6vh) translateX(50vw);
-webkit-transform: translateY(-6vh) translateX(50vw);
}
40% {
transform: translateY(-5vh) translateX(70vw);
-webkit-transform: translateY(-5vh) translateX(70vw);
}
50% {
transform: translateY(-3vh) translateX(90vw);
-webkit-transform: translateY(-3vh) translateX(90vw);
}
60% {
transform: translateY(-2vh) translateX(110vw);
-webkit-transform: translateY(-2vh) translateX(110vw);
}
100% {
transform: translateY(0) translateX(110vw);
-webkit-transform: translateY(0) translateX(110vw);
}
}
I am taking an online course learning CSS and we are just covering CSS animations. I am trying to practice some of the things I learned (just basic transforms for now) by creating a small animation of a man walking towards the screen down a pathway.
Basically, I want to both translate and scale my image at the same time. I got this working fine, but now I also wanted to add some small rotation so that it looks like the man is slightly moving left and right. Here is my code in a jsfiddle, I don't know how to change the transform-origin so that the man is walking in a straight line on the Y-Axis, the scale makes him walk in a diagonal. I hope that makes sense...
The commented out part of the code includes the scale, as soon as that is added back, and the part without scale is commented out, it acts funny and I'm thinking this has to do with the origin?
https://jsfiddle.net/qLLqdxbm/
HTML:
<div class="man-scale">
<img class="man-walk" src="http://clipart-library.com/img/1184697.png">
</div>
CSS:
.man-walk {
width: 100px;
height: 125px;
position: absolute;
top: 0;
left: 50px;
animation-name: man-walk;
animation-duration: 0.45s;
animation-iteration-count: infinite;
}
#keyframes man-walk {
0% {
transform: rotate(0deg);
}
25% {
transform: rotate(1.5deg);
}
50% {
transform: rotate(0deg);
}
75% {
transform: rotate(-1.5deg);
}
100% {
transform: rotate(0deg);
}
}
.man-scale {
width: 100px;
height: 125px;
animation-name: man-scale;
animation-duration: 2s;
animation-timing-function: linear;
animation-iteration-count: infinite;
}
/* define the animation */
#keyframes man-scale {
/* 0% {
transform: translate(0px, 5px) scale(1.1);
}
25% {
transform: translate(0px, 15px) scale(1.5);
}
50% {
transform: translate(0px, 25px) scale(1.7);
}
75% {
transform: translate(0px, 35px) scale(2.0);
}
100% {
transform: translate(0px, 45px) scale(2.3);
} */
0% {
transform: translate(0px, 5px);
}
25% {
transform: translate(0px, 15px);
}
50% {
transform: translate(0px, 25px);
}
75% {
transform: translate(0px, 35px);
}
100% {
transform: translate(0px, 45px);
}
}
Thanks for the help!
Each time you scale the image along X and Y, the origin shifts in both dimensions by a specific offset. If you can compensate for that offset in the X dimension then a vertical animation could be achieved.
In this case in first keyframe the scale increased by 0.1 which is 100 * 0.1 = 10px now origin got offset by 5px in X dimension, compensating in terms of translateX(-5px). Similarly for all the other keyframes.
If you want a faster animation in the Y dimension just increase the Y translate values without touching the X translation values.
.man-walk {
width: 100px;
height: 125px;
position: absolute;
top: 0;
left: 50px;
animation-name: man-walk;
animation-duration: 0.45s;
animation-iteration-count: infinite;
}
#keyframes man-walk {
0% {
transform: rotate(0deg);
}
25% {
transform: rotate(1.5deg);
}
50% {
transform: rotate(0deg);
}
75% {
transform: rotate(-1.5deg);
}
100% {
transform: rotate(0deg);
}
}
.man-scale {
width: 100px;
height: 125px;
animation-name: man-scale;
animation-duration: 2s;
animation-timing-function: linear;
animation-iteration-count: infinite;
}
/* define the animation */
#keyframes man-scale {
0% {
transform: translate(-5px, 30px) scale(1.1);
}
25% {
transform: translate(-20px, 70px) scale(1.4);
}
50% {
transform: translate(-35px, 120px) scale(1.7);
}
75% {
transform: translate(-50px, 180px) scale(2.0);
}
100% {
transform: translate(-65px, 250px) scale(2.3);
}
}
<div class="man-scale">
<img class="man-walk" src="http://clipart-library.com/img/1184697.png">
</div>
There might be some advanced CSS techniques to calculate the offset automatically.
I'm wondering if it's possible to perform a CSS transition where an image does this:
Begins on the left.
Moves from the left, to the right.
When it reaches the right hand side, it flips horizontally, and then
moves back towards the left again.
When it reaches the left, it flips horizontally again and moves
towards the right side, and so on.
So far I have this fiddle: https://jsfiddle.net/kkn7cpqL/1/
This:
animation-direction: alternate;
is moving the image back and forth correctly, but I don't know how to make it flip once it reaches the sides. I'd like the arrow to always be pointing in the direction it's moving.
Any help with this would be really appreciated, thanks!
Try this code
This might help you to achieve what you want.
#keyframes arrow {
0% {
left: 0%;
transform: rotateY(0deg);
}
49% {
transform: rotateY(0deg);
}
50% {
left: 90%;
transform: rotateY(180deg);
}
99% {
transform: rotateY(180deg);
}
100% {
left: 0%;
transform: rotateY(0deg);
}
}
div.arrow {
width: 44px;
height: 50px;
position: absolute;
animation-name: arrow;
animation-duration: 10s;
/* double of original time */
animation-fill-mode: forwards;
animation-iteration-count: infinite;
animation-timing-function: linear;
}
<div class="arrow">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQvXxGWc9lfv_WXJ3RtUb-4pBMRDYZOG4b9YXNnNkNGOsImdDnN5w" />
</div>
Just make it flip on 0% and 100% in animation it will give you the required result. Or you can just simply copy the below code.
#keyframes arrow {
0% {left:50px; top:10px;-moz-transform: scaleX(-1);
-o-transform: scaleX(-1);
-webkit-transform: scaleX(-1);
transform: scaleX(-1);
filter: FlipH;
-ms-filter: "FlipH";}
100% {left:250px; top:10px;-moz-transform: scaleX(1);
-o-transform: scaleX(1);
-webkit-transform: scaleX(1);
transform: scaleX(1);
filter: FlipH;
-ms-filter: "FlipH";}
}
div.arrow {
width: 44px;
height: 50px;
position: absolute;
animation-name: arrow;
background-repeat: no-repeat;
animation-duration: 3s;
animation-timing-function: linear;
animation-iteration-count: infinite;
animation-direction: alternate;
}
<div class="arrow">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQvXxGWc9lfv_WXJ3RtUb-4pBMRDYZOG4b9YXNnNkNGOsImdDnN5w"/>
</div>
Try this code
This might help you to achieve what you want
#keyframes arrow {
0% {left:50px; top:10px;}
50%{transform: rotateX(90deg);}
100% {left:250px; top:10px;}
}
div.arrow {
width: 44px;
height: 50px;
position: absolute;
animation-name: arrow;
background-repeat: no-repeat;
animation-duration: 3s;
animation-timing-function: linear;
animation-iteration-count: infinite;
animation-direction: alternate;
}
http://codepen.io/ankitkothari225/pen/bpgRPo
Is there a way to get this CSS code:
/** Swing **/
.animated {
-webkit-animation-duration: 1s;
animation-duration: 1s;
-webkit-animation-fill-mode: both;
animation-fill-mode: both;
}
#-webkit-keyframes swing {
20%, 40%, 60%, 80%, 100% { -webkit-transform-origin: top center; }
20% { -webkit-transform: rotate(15deg); }
40% { -webkit-transform: rotate(-10deg); }
60% { -webkit-transform: rotate(5deg); }
80% { -webkit-transform: rotate(-5deg); }
100% { -webkit-transform: rotate(0deg); }
}
#keyframes swing {
20% { transform: rotate(15deg); }
40% { transform: rotate(-10deg); }
60% { transform: rotate(5deg); }
80% { transform: rotate(-5deg); }
100% { transform: rotate(0deg); }
}
.swing {
-webkit-transform-origin: top center;
transform-origin: top center;
-webkit-animation-name: swing;
animation-name: swing;
}
/****/
Generated from this website: https://coveloping.com/tools/css-animation-generator
To work only when hovering over an element?
I added:
.swing:hover {
-webkit-transform-origin: top center;
transform-origin: top center;
-webkit-animation-name: swing;
animation-name: swing;
}
Myself and it's not working.
Oh... And I'm trying to avoid using javascript/jQuery so the page will load faster..
Demo
Use the animation-play-state:running; on :hover
so that it plays while hovering over.
Not hovered element should then be : animation-play-state:paused; so its not running without hover.
Setting the animation-iteration-count:infinite; makes the animation run infinite (as long as you hover in this case).