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.
Related
For some reason the svg icon in this css animation is flickering sometimes on Chromium based Browsers. On Safari and Firefox it works flawlessly. How can I fix that? Has something to do with transform: translate? I've also tried to paste -webkit-transform:translate3d(0,0,0); which should solve the problem but unfortunately it didn't.
body {
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.u-loading {
width: 128px;
height: 128px;
display: block;
margin: 48px;
}
.u-loading__symbol {
background-color: #4caf50;
padding: 8px;
animation: loading 3s infinite;
border-radius: 5px;
}
.u-loading__symbol img {
display: block;
max-width: 100%;
animation: loading-icon 3s infinite;
}
#keyframes loading {
0% {
transform: perspective(250px) rotateX(0deg) rotateY(0deg);
}
15% {
background-color: #4caf50;
}
16% {
background-color: #b44646;
}
50% {
transform: perspective(250px) rotateX(180deg) rotateY(0deg);
background-color: #b44646;
}
65% {
background-color: #b44646;
}
66% {
background-color: #4caf50;
}
100% {
transform: perspective(250px) rotateX(180deg) rotateY(-180deg);
}
}
#keyframes loading-icon {
0% {
transform: perspective(250px) rotateX(0deg) rotateY(0deg);
}
15% {
transform: perspective(250px) rotateX(0deg) rotateY(0deg);
}
16% {
transform: perspective(250px) rotateX(180deg) rotateY(0deg);
}
50% {
transform: perspective(250px) rotateX(180deg) rotateY(0deg);
}
65% {
transform: perspective(250px) rotateX(180deg) rotateY(0deg);
}
66% {
transform: perspective(250px) rotateX(180deg) rotateY(180deg);
}
100% {
transform: perspective(250px) rotateX(180deg) rotateY(180deg);
}
}
<div class="u-loading">
<div class="u-loading__symbol">
<img src="https://kaiser.kiwi/assets/kaiserkiwi-logo.svg">
</div>
</div>
Thank's in advance
I'm looking at your example in Chrome and the animation looks pretty slick. The only "flicker" I see is a brief glimpse of the back of the red div's image at the very end of it's transition. I think this can be solved by using backface-visibility: hidden; on the element you are transforming to flip over.
See: https://developer.mozilla.org/en-US/docs/Web/CSS/backface-visibility
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.
Using keyframe animation, the div with an id of "Second" animates slightly before the "first" div starts to. Here is my code shouldn't they move at the same speed by default? any help would be great thanks.
body { background-color: black; color: white;}
#First { width: 200px;
height: 50px;
position: absolute;
top:5px;
color: black;
text-align: center;
background-color: yellow;
-webkit-transform-origin: top;
-webkit-animation: myfirst 1s;
-webkit-transform:rotateX(90deg);
-webkit-animation-iteration-count: infinite;
}
#-webkit-keyframes myfirst
{
0% {-webkit-transform:rotateX(0deg);}
100% {-webkit-transform:rotateX(90deg);}
}
#Second { width: 200px;
height: 50px;
position: absolute;
top:5px;
left:200px;
color: black;
text-align: center;
background-color: green;
-webkit-transform-origin: bottom;
-webkit-animation: mysecond 1s;
-webkit-transform:rotateX(0deg);
-webkit-animation-iteration-count: infinite;
}
#-webkit-keyframes mysecond
{
0% {-webkit-transform:rotateX(90deg);}
100% {-webkit-transform:rotateX(0deg);}
}
and the HTML,
<div id="First">FIRST</div>
<div id="Second">SECOND</div>
Code on jsfiddle: http://jsfiddle.net/x3p64/
Demo
#-webkit-keyframes were different for both
As per requirements
New Demo
#-webkit-keyframes myfirst {
0% {
-webkit-transform: scaleY(0);
}
20% {
-webkit-transform: scaleY(0.2);
}
40% {
-webkit-transform: scaleY(0.4);
}
60% {
-webkit-transform: scaleY(0.6);
}
80% {
-webkit-transform: scaleY(0.8);
}
100% {
-webkit-transform: scaleY(1);
}
}
#-webkit-keyframes mysecond {
0% {
-webkit-transform: scaleY(1);
}
20% {
-webkit-transform: scaleY(0.8);
}
40% {
-webkit-transform: scaleY(0.6);
}
60% {
-webkit-transform: scaleY(0.4);
}
80% {
-webkit-transform: scaleY(0.2);
}
100% {
-webkit-transform: scaleY(0);
}
}
It's not that it is starting before, it just looks like it because of the easing properties. Both animations are starting and stopping at the same time, they just look different. Try using a linear easing on both.
-webkit-animation: mysecond 1s linear;
This is the fiddle.I have searched a solution for this but didn't find one. The correct one should be the one in Chrome/Opera/Safari. I want to show only the image from top to bottom.
The HTML:
<div class="container">
<img class="image" src="http://www.hotel-aramis.com/slider/home/notre-dame-de-paris.jpg" />
</div>
The CSS:
#-webkit-keyframes pan {
0% {
-webkit-transform: translate(0%, 0%);
}
100% {
top: 100%;
-webkit-transform: translate(0%, -100%);
}
}
#keyframes pan {
0% {
-webkit-transform: translate(0%, 0%);
}
100% {
top: 100%;
-webkit-transform: translate(0%, -100%);
}
}
.container {
position:relative;
width:1100px;
height:480px;
overflow:hidden;
background-color:#000;
}
.image {
position:absolute;
top: 0;
max-width:100%;
min-width:100%;
-webkit-animation: pan 5s alternate infinite linear;
animation: pan 5s alternate infinite linear;
}
Firefox doesn't use webkit why your -webkit-transform will do nothing. Change it to transform and it should most likely work.
I made a JSFiddle for you
#-webkit-keyframes pan {
0% {
-webkit-transform: translate(0%, 0%);
}
100% {
top: 100%;
-webkit-transform: translate(0%, -100%);
}
}
#keyframes pan {
0% {
transform: translate(0%, 0%);
}
100% {
top: 100%;
transform: translate(0%, -100%);
}
}
I somehow manage to create this. I created a cube, that rotate horizontally, when it is hovered, but i want it to stay at its current location when it is not hovered. Ive been searching this for awhile now, but I cant seem to find the answer.
<html>
<style>
.wrap {
-moz-perspective: 800px;
-webkit-perspective: 800px;
perspective: 800px;
-moz-perspective-origin: 50% 100px;
-webkit-perspective-origin: 50% 100px;
perspective-origin: 50% 100px;
}
.cube {
position: relative;
width: 200px;
-webkit-transform-style: preserve-3d;
-moz-transform-style: preserve-3d;
margin: 0 auto;
margin-top: 30px;
}
.cube div {
position: absolute;
width: 200px;
height: 200px;
}
.back {
-webkit-transform: translateZ(-100px) rotateY(180deg);
-moz-transform: translateZ(-100px) rotateY(180deg);
background: orange;
opacity: 0.5;
}
.right {
-webkit-transform: rotateY(-270deg) translateX(100px);
-moz-transform: rotateY(-270deg) translateX(100px);
-webkit-transform-origin: top right;
-moz-transform-origin: top right;
background: yellow;
opacity: 0.5;
}
.left {
-webkit-transform: rotateY(270deg) translateX(-100px);
-moz-transform: rotateY(270deg) translateX(-100px);
-webkit-transform-origin: center left;
-moz-transform-origin: center left;
background: violet;
opacity: 0.5;
}
.top {
-moz-transform: rotateX(-90deg) translateY(-100px);
-webkit-transform: rotateX(-90deg) translateY(-100px);
-webkit-transform-origin: top center;
-moz-transform-origin: top center;
background: green;
opacity: 0.5;
}
.bottom {
-webkit-transform: rotateX(90deg) translateY(100px);
-moz-transform: rotateX(90deg) translateY(100px);
-webkit-transform-origin: bottom center;
-moz-transform-origin: bottom center;
background: blue;
opacity: 0.5;
}
.front {
-webkit-transform: translateZ(100px);
-moz-transform: translateZ(100px);
background: red;
opacity: 0.5;
}
#-webkit-keyframes spin {
from { -webkit-transform: rotateY(0); }
to { -webkit-transform: rotateY(360deg); }
}
.cube:hover {
animation: spin 5s infinite linear;
-webkit-animation: spin 5s infinite linear;
-moz-animation: spin 5s infinite linear;
}
</style>
<body>
<div class="wrap">
<div class="cube">
<div class="front">front</div>
<div class="back">back</div>
<div class="top">top</div>
<div class="bottom">bottom</div>
<div class="left">left</div>
<div class="right">right</div>
</div>
</div
</body>
</html>
Anynone can point me to the right direction? thank you very much,
You can set the animation for all states of .cube, and just toggle animation-play-state on hovering (see JSFiddle):
.cube {
/* other styles... */
-webkit-animation: spin 5s infinite linear;
animation: spin 5s infinite linear;
-webkit-animation-play-state: paused;
animation-play-state: paused;
}
/* other rules... */
.cube:hover {
-webkit-animation-play-state: running;
animation-play-state: running;
}
Also, I suppose that now there is no much need to specify -moz-properties for transforms and animations because Firefox supports them unprefixed since version 16 (it's 7 versions back!).
Following CSS will call in case of mouse hover, as there is no spinning animation within this, it won't trigger any animation
.cube : hover {
//Do nothing
}