CSS Translate and Scale on Y-Axis? - html

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.

Related

Css Animation Functional on Chrome but not Safari

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);
}
}

CSS Animations - Animation bug?

I'm trying to use CSS animations to animate a cube rotating, and pausing on each face for a set amount of time.
Pen here
#keyframes frontToLeft {
75% { transform: rotateY(0); }
100% { transform: rotateY(90deg); }
}
#keyframes leftToBack {
75% { transform: rotateY(90deg); }
100% { transform: rotateY(180deg); }
}
#keyframes backToRight {
75% { transform: rotateY(180deg); }
100% { transform: rotateY(270deg); }
}
#keyframes rightToFront {
75% { transform: rotateY(270deg); }
100% { transform: rotateY(360deg); }
}
.cube-container {
padding-top: 200px;
perspective: 800px;
perspective-origin: 50% 100px;
}
.qube {
position: relative;
width: 200px;
margin: 0 auto;
transform-style: preserve-3d;
animation-name: frontToLeft, leftToBack, backToRight, rightToFront;
animation-timing-function: linear;
animation-fill-mode: forwards;
animation-duration: 2s, 2s, 2s, 2s;
animation-delay: 2s, 4s, 6s, 8s;
* {
position: absolute;
left: 0;
top: 0;
margin: 0;
padding: 0;
width: 200px;
height: 200px;
background: rgba(255,255,255,0.1);
box-shadow: inset 0 0 30px rgba(125,125,125,0.8);
}
.front {
transform: translateZ(100px);
}
.back {
transform: translateZ(-100px) rotateY(180deg);
}
.top {
transform: rotateX(-90deg) translateY(-100px);
transform-origin: top center;
}
.bottom {
transform: rotateX(90deg) translateY(100px);
transform-origin: bottom center;
}
.left {
transform: rotateY(270deg) translateX(-100px);
transform-origin: center left;
}
.right {
transform: rotateY(-270deg) translateX(100px);
transform-origin: top right;
}
}
<div class="cube-container">
<div class="qube">
<div class="front">front</div>
<div class="left">left</div>
<div class="back">back</div>
<div class="right">right</div>
<div class="top">top</div>
<div class="bottom">bottom</div>
</div>
</div>
In Google Chrome and Edge, the animation seems to glitch, but in Firefox it works as intended.
I'd like the outcome to be:
Front Face - Pause 2 seconds, rotate 2 seconds
Left Face - Pause 2 seconds, rotate 2 seconds
Back Face - Pause 2 seconds, rotate 2 seconds
Right Face - Pause 2 seconds, rotate 2 seconds
Can anyone see where this would be going wrong? I have the Codepen preprocessing SCSS with prefixes.
Thanks in advance!
From what I can tell testing this it looks like a bug. Nothing I've tried seems to work to correct the animation. Like you say, Firefox works as expected.
All I can think of as a potential fix is to combine it into one animation something like this:
#keyframes spinCube {
20% { transform: rotateY(0deg); }
25% { transform: rotateY(90deg); }
45% { transform: rotateY(90deg); }
50% { transform: rotateY(180deg); }
70% { transform: rotateY(180deg); }
75% { transform: rotateY(270deg); }
95% { transform: rotateY(270deg); }
100% { transform: rotateY(360deg); }
}
.qube {
animation: spinCube 8s 1 forwards;
}
It would take a bit of tweaking to get the timing right, but it's the only thing I can think of.
Here's a CodePen Example of this alternative solution.

How to apply multiple css3 rotations on a single element with transition

I want to implement something like this:
img{
transition: 5s linear;
transform: scale(2,2) rotate(-20deg) rotate(40deg) rotate(-40deg) rotate(20deg) scale(0.5, 0.5);
}
But is just scales a bit and then scales back to the original size.
You simply have to create an element and add to it animation using keyframes, as in the example below:
.element {
animation: rotate 5s infinite;
width: 50px;
height: 50px;
background-color: green;
}
#keyframes rotate {
0% {
transform: rotate(0deg);
}
25% {
transform: rotate(20deg);
}
50% {
transform: rotate(-20deg);
}
75% {
transform: rotate(20deg);
}
100% {
transform: rotate(-20deg);
}
}
<div class="element"> </div>
You can find more information about it here: https://css-tricks.com/almanac/properties/a/animation/
I recommend you to use animation over transition.
#keyframes{
0%{
//your transform
}
50%{
//your transform
}
100%{
//your transform
}
}

Animating an image to make it rotate in a circle

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>

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