How to define css animation? - css

I have written this code to make a rectangle move from left to right like a car but this is not working. I think there is problem with #keyframes example
* {
margin: auto;
padding: 0px;
}
body {
background-color: lightgreen;
}
.main {
height: 100%;
width: 100%;
border: 1px solid black;
}
.road {
height: 300px;
width: 100%;
background-color: black;
margin-top: 25%;
}
#keyframes example {
from {
top: 20%;
left: 80%
}
20% {
top: 20%;
left: 60%
}
40% {
top: 20%;
left: 50%
}
60% {
top: 20%;
left: 30%
}
80% {
top: 20%;
left: 10%
}
to {
top: 20%;
left: 0%
}
}
#-webkit-keyframes example {
0% {
top: 20%;
left: 80%
}
20% {
top: 20%;
left: 60%
}
40% {
top: 20%;
left: 50%
}
60% {
top: 20%;
left: 30%
}
80% {
top: 20%;
left: 10%
}
100% {
top: 20%;
left: 0%
}
}
#-moz-keyframes example {
0% {
top: 20%;
left: 80%
}
20% {
top: 20%;
left: 60%
}
40% {
top: 20%;
left: 50%
}
60% {
top: 20%;
left: 30%
}
80% {
top: 20%;
left: 10%
}
100% {
top: 20%;
left: 0%
}
}
.car {
height: 100px;
width: 200px;
background-color: red;
font-size: 50px;
color: black;
text-align: center;
vertical-align: middle;
margin-top: 100px;
animation-name: example;
animation-duration: 10s;
animation-iteration-count: infinite;
-webkit-animation-name: example;
-webkit-animation-duration: 10s;
-webkit-animation-iteration-count: infinite;
-moz-animation-name: example;
-moz-animation-duration: 10s;
-moz-animation-iteration-count: infinite;
}

You need to use position: relative on .car to be displayed like that.
.car {
position: relative;
height:100px;
width:200px;
background-color: red;
font-size: 50px;
color:black;
text-align: center;
vertical-align: middle;
margin-top: 100px;
animation-name:example;
animation-duration: 10s;
animation-iteration-count: infinite;
-webkit-animation-name: example;
-webkit-animation-duration: 10s;
-webkit-animation-iteration-count: infinite;
-moz-animation-name: example;
-moz-animation-duration: 10s;
-moz-animation-iteration-count: infinite;
}

It seems that you're trying to move the car as if it were absolutely positioned. Make sure you have a wrapper that the car is inside of (In this case I'm assuming it's the road) and give that a relative position. Give you're car an absolute position. Here's a codepen I created that may help you!
https://codepen.io/anon/pen/MdYLOM
Also try making your animation like this:
#keyframes example {
0% { left: 0% }
100% { left: 100% }
}

Related

Change slideshow speed CSS

I currently have a slideshow on my website but it is sliding too fast, I would like to make it slower but I dont know how can I do that, everytime I try to change some things I end up ruining everything.
I wanted to get like 5/6 seconds each slide.
I would be extremely grateful if someone could help me.
.topSlider {
display: block;
width: 100%;
height: 100%;
background-color: #1f1f1f;
overflow: hidden;
position: absolute; }
.topSlider span {
position: absolute;
color: white;
z-index: 1;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
text-align: center; }
.topSlider .slide {
position: absolute;
display: block;
width: 100%;
height: 100%;
-webkit-animation: slide 12s infinite;
animation: slide 10s infinite;
overflow: hidden; }
.topSlider .slide:nth-child(2) {
left: 0%;
-webkit-animation-delay: -1s;
animation-delay: -0.5s;
background-color: black;
background-size: cover;
background-position: center; }
.topSlider .slide:nth-child(3) {
left: 100%;
-webkit-animation-delay: 2s;
animation-delay: 2s;
background-color: red;
background-size: cover;
background-position: center; }
.topSlider .slide:nth-child(4) {
left: 100%;
-webkit-animation-delay: 5s;
animation-delay: 4.5s;
background-color: white;
background-size: cover;
background-position: center; }
.topSlider .slide:nth-child(5) {
left: 100%;
-webkit-animation-delay: 8s;
animation-delay: 7s;
background-color: blue;
background-size: cover;
background-position: center; }
.topSlider p {
text-align: center;
display: inline-block;
width: 100%;
margin-top: 340px;
color: white; }
#-webkit-keyframes slide {
0% {
left: 100%;
width: 100%; }
5% {
left: 0%; }
25% {
left: 0%; }
30% {
left: -100%;
width: 100%; }
31% {
left: -100%;
width: 0%; }
100% {
left: 100%;
width: 0%; } }
#keyframes slide {
0% {
left: 100%;
width: 100%; }
5% {
left: 0%; }
25% {
left: 0%; }
30% {
left: -100%;
width: 100%; }
31% {
left: -100%;
width: 0%; }
100% {
left: 100%;
width: 0%; } }
<div class="topSlider">
<div class="slide"></div>
<div class="slide"></div>
<div class="slide"></div>
<div class="slide"></div>
</div>
You can change the seconds
-webkit-animation-delay: -1s;
animation-delay: -0.5s;
for each child.
but also make sure that the main slider time is proportional to the interval between the seconds in the slider children. Have a look at my example below and compare it to yours to see what I mean.
main slider time in your example:
-webkit-animation: slide 12s infinite;
animation: slide 10s infinite;
.topSlider {
display: block;
width: 100%;
height: 100%;
background-color: #1f1f1f;
overflow: hidden;
position: absolute; }
.topSlider span {
position: absolute;
color: white;
z-index: 1;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
text-align: center; }
.topSlider .slide {
position: absolute;
display: block;
width: 100%;
height: 100%;
-webkit-animation: slide 20s infinite;
animation: slide 20s infinite;
overflow: hidden; }
.topSlider .slide:nth-child(2) {
left: 0%;
-webkit-animation-delay: -1s;
animation-delay: -0.5s;
background-color: black;
background-size: cover;
background-position: center; }
.topSlider .slide:nth-child(3) {
left: 100%;
-webkit-animation-delay: 5s;
animation-delay: 4.5s;
background-color: red;
background-size: cover;
background-position: center; }
.topSlider .slide:nth-child(4) {
left: 100%;
-webkit-animation-delay: 10s;
animation-delay: 9.0s;
background-color: white;
background-size: cover;
background-position: center; }
.topSlider .slide:nth-child(5) {
left: 100%;
-webkit-animation-delay: 15s;
animation-delay: 13.5s;
background-color: blue;
background-size: cover;
background-position: center; }
.topSlider p {
text-align: center;
display: inline-block;
width: 100%;
margin-top: 340px;
color: white; }
#-webkit-keyframes slide {
0% {
left: 100%;
width: 100%; }
5% {
left: 0%; }
25% {
left: 0%; }
30% {
left: -100%;
width: 100%; }
31% {
left: -100%;
width: 0%; }
100% {
left: 100%;
width: 0%; } }
#keyframes slide {
0% {
left: 100%;
width: 100%; }
5% {
left: 0%; }
25% {
left: 0%; }
30% {
left: -100%;
width: 100%; }
31% {
left: -100%;
width: 0%; }
100% {
left: 100%;
width: 0%; } }
<div class="topSlider">
<div class="slide"></div>
<div class="slide"></div>
<div class="slide"></div>
<div class="slide"></div>
</div>
You can change the duration part of the -webkit-animation and animation properties ("2s"):
-webkit-animation: slide 2s infinite;
animation: slide 2s infinite;
.topSlider {
display: block;
width: 100%;
height: 100%;
background-color: #1f1f1f;
overflow: hidden;
position: absolute; }
.topSlider span {
position: absolute;
color: white;
z-index: 1;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
text-align: center; }
.topSlider .slide {
position: absolute;
display: block;
width: 100%;
height: 100%;
-webkit-animation: slide 6s infinite;
animation: slide 6s infinite;
overflow: hidden; }
.topSlider .slide:nth-child(2) {
left: 0%;
-webkit-animation-delay: 6s;
animation-delay: 6s;
background-color: black;
background-size: cover;
background-position: center; }
.topSlider .slide:nth-child(3) {
left: 100%;
-webkit-animation-delay: 6s;
animation-delay: 6s;
background-color: red;
background-size: cover;
background-position: center; }
.topSlider .slide:nth-child(4) {
left: 100%;
-webkit-animation-delay: 6s;
animation-delay: 6s;
background-color: white;
background-size: cover;
background-position: center; }
.topSlider .slide:nth-child(5) {
left: 100%;
-webkit-animation-delay: 6s;
animation-delay: 6s;
background-color: blue;
background-size: cover;
background-position: center; }
.topSlider p {
text-align: center;
display: inline-block;
width: 100%;
margin-top: 340px;
color: white; }
#-webkit-keyframes slide {
0% {
left: 100%;
width: 100%; }
5% {
left: 0%; }
25% {
left: 0%; }
30% {
left: -100%;
width: 100%; }
31% {
left: -100%;
width: 0%; }
100% {
left: 100%;
width: 0%; } }
#keyframes slide {
0% {
left: 100%;
width: 100%; }
5% {
left: 0%; }
25% {
left: 0%; }
30% {
left: -100%;
width: 100%; }
31% {
left: -100%;
width: 0%; }
100% {
left: 100%;
width: 0%; } }
<div class="topSlider">
<div class="slide"></div>
<div class="slide"></div>
<div class="slide"></div>
<div class="slide"></div>
</div>

Adding a bounce effect using CSS animation

I'm playing round with CSS animation by trying to replicate the following new google ads logo - example.
What is the best way to add the bounce effect on the green ball?
My current animation:
#keyframes greenblock {
0% {
top: 0px;
}
50% {
top: 45px;
}
100% {
bottom: 0px;
}
}
My code (fiddle):
.wrap {
border: 1px solid red;
height: 300px;
width: 300px;
position: relative
}
.blue-shape {
position: absolute;
left: 100px;
top: 0px;
width: 45px;
height: 45px;
background: #4285F4;
display: block;
border-radius: 45px;
animation: blueblock 2s forwards;
transform-origin: top center;
}
.yellow-shape {
position: absolute;
left: 122px;
top: 0px;
width: 45px;
height: 45px;
background: #FBBC04;
display: block;
border-radius: 45px;
animation: yellowblock 2s forwards;
transform-origin: top center;
}
.green-ball {
position: absolute;
border-radius: 45px;
width: 45px;
height: 45px;
background: #34A853;
animation: greenblock 1.5s forwards;
}
#keyframes blueblock {
0% {
height: 45px;
}
25% {
height: 140px;
transform: rotate(0deg);
}
50% {
transform: rotate(-30deg);
}
100% {
height: 140px;
transform: rotate(-30deg);
}
}
#keyframes yellowblock {
0% {
height: 45px;
opacity: 0;
}
25% {
height: 140px;
transform: rotate(0deg);
opacity: 0;
}
50% {
transform: rotate(30deg);
}
100% {
height: 140px;
transform: rotate(30deg);
opacity: 100;
left: 122px;
}
}
#keyframes greenblock {
0% {
top: 0px;
}
50% {
top: 45px;
}
100% {
bottom: 0px;
}
}
<div class="wrap">
<div class="yellow-shape">
<div class="green-ball">
</div>
</div>
<div class="blue-shape">
</div>
</div>
I've tried with this animation
animation: greenblock .6s ease-in-out .5s forwards;
and this set of keyframes
#keyframes greenblock {
0% { top: 0px; }
75% { top: calc(100% - 55px); }
50%, 100% { top: calc(100% - 45px); }
}
Demo
.wrap {
border: 1px solid red;
height: 300px;
width: 300px;
position: relative
}
.blue-shape {
position: absolute;
left: 100px;
top: 0px;
width: 45px;
height: 45px;
background: #4285F4;
display: block;
border-radius: 45px;
animation: blueblock 2s forwards;
transform-origin: top center;
}
.yellow-shape {
position: absolute;
left: 122px;
top: 0px;
width: 45px;
height: 45px;
background: #FBBC04;
display: block;
border-radius: 45px;
animation: yellowblock 2s forwards;
transform-origin: top center;
}
.green-ball {
position: absolute;
border-radius: 45px;
width: 45px;
height: 45px;
background: #34A853;
animation: greenblock .6s ease-in-out .5s forwards;
}
#keyframes blueblock {
0% {
height: 45px;
}
25% {
height: 140px;
transform: rotate(0deg);
}
50% {
transform: rotate(-30deg);
}
100% {
height: 140px;
transform: rotate(-30deg);
}
}
#keyframes yellowblock {
0% {
height: 45px;
opacity: 0;
}
25% {
height: 140px;
transform: rotate(0deg);
opacity: 0;
}
50% {
transform: rotate(30deg);
}
100% {
height: 140px;
transform: rotate(30deg);
opacity: 100;
left: 122px;
}
}
#keyframes greenblock {
0% { top: 0px; }
75% { top: calc(100% - 55px); }
50%, 100% { top: calc(100% - 45px); }
}
<div class="wrap">
<div class="yellow-shape">
<div class="green-ball">
</div>
</div>
<div class="blue-shape">
</div>
</div>

keyframes animations not returning to first point or repeating

I'm trying to use a keyframe that will not return to first position, I mean if I have a transition from left to right, to stay right not return to the left side.
Code
div {
width: 100px;
height: 100px;
background-color: red;
position: relative;
-webkit-animation-name: example;
-webkit-animation-duration: 4s;
animation-name: example;
animation-duration: 4s;
}
/* Safari 4.0 - 8.0 */
#-webkit-keyframes example {
0% {
background-color: red;
left: 0px;
top: 0px;
}
100% {
background-color: yellow;
left: 200px;
top: 0px;
}
}
/* Standard syntax */
#keyframes example {
0% {
background-color: red;
left: 0px;
top: 0px;
}
100% {
background-color: yellow;
left: 200px;
top: 0px;
}
}
<div></div>
You just need to add animation-fill-mode: forwards; to the div.
div {
width: 100px;
height: 100px;
background-color: red;
position: relative;
-webkit-animation-name: example;
-webkit-animation-duration: 4s;
-webkit-animation-fill-mode: forwards;
animation-name: example;
animation-duration: 4s;
animation-fill-mode: forwards;
}
/* Safari 4.0 - 8.0 */
#-webkit-keyframes example {
0% {background-color:red; left:0px; top:0px;}
100% {background-color:yellow; left:200px; top:0px;}
}
/* Standard syntax */
#keyframes example {
0% {background-color:red; left:0px; top:0px;}
100% {background-color:yellow; left:200px; top:0px;}
}
<div></div>
You just need to add 'animation-iteration-count: 1' it will work single time or if you want to take it in the loop then can try 'animation-iteration-count: infinite' into the div.
div {
width: 100px;
height: 100px;
background-color: red;
position: relative;
-webkit-animation-name: example;
-webkit-animation-duration: 4s;
animation-name: example;
animation-duration: 4s;
animation-iteration-count: 1; /*value can be infinite if you want to it in loop */
}
/* Safari 4.0 - 8.0 */
#-webkit-keyframes example {
0% {
background-color: red;
left: 0px;
top: 0px;
}
50%{
background-color: yellow;
left: 200px;
top: 0px;
}
100% {
background-color: red;
left: 0px;
top: 0px;
}
}
/* Standard syntax */
#keyframes example {
0% {
background-color: red;
left: 0px;
top: 0px;
}
50%{
background-color: yellow;
left: 200px;
top: 0px;
}
100% {
background-color: red;
left: 0px;
top: 0px;
}
}
<div></div>

strange keyframes setting in CSS animation

Inspired by this tutorial http://tympanus.net/codrops/2014/01/07/shape-hover-effect-with-svg/ I decided to make pure css version of similar effect.
And it looks good and work pretty smooth. What bothers me is why after few attempts I had to set keyframes at 24% and 74% instead of 50%? With 50% animation looks choppy. I really don't like to do things blindfolded, so I'll be grateful for help.
Here is quick dirty implementation:
html {
background: #ccc;
}
.card {
position: relative;
display: inline-block;
height: 400px;
width: 200px;
background: #000;
margin: 50px;
overflow: hidden;
}
.card-head {
position: absolute;
background: #000;
height: 400px;
width: 400px;
border-radius: 50%;
left: -100px;
top: -173px;
z-index: 10;
-webkit-animation-name: carda;
animation-name: carda;
}
.card-extend {
position: absolute;
background: #fff;
height: 400px;
width: 400px;
bottom: -200px;
left: -100px;
z-index: 5;
-webkit-animation-name: cardb;
animation-name: cardb;
}
.card-animated {
-webkit-animation-duration: .2s;
animation-duration: .2s;
-webkit-animation-fill-mode: forwards;
animation-fill-mode: forwards;
-webkit-animation-timing-function:ease-in-out;
animation-timing-function:ease-in-out;
}
.card:hover .card-head,
.card:focus .card-head{
-webkit-animation-name: cardhovera;
animation-name: cardhovera;
}
.card:hover .card-extend,
.card:focus .card-extend{
-webkit-animation-name: cardhoverb;
animation-name: cardhoverb;
}
#-webkit-keyframes carda {
from {
border-radius: 0%;
top: -320px;
z-index: 2;
}
24% {
top: -320px;
border-radius: 25%;
z-index: 2;
}
to {
border-radius: 50%;
top: -173px;
}
}
#keyframes carda {
from {
border-radius: 0%;
top: -320px;
z-index: 2;
}
24% {
top: -320px;
border-radius: 25%;
z-index: 2;
}
to {
border-radius: 50%;
top: -173px;
}
}
#-webkit-keyframes cardhovera {
from {
border-radius: 50%;
top: -173px;
}
76% {
top: -320px;
border-radius: 25%;
z-index: 2;
}
to {
border-radius: 0%;
top: -320px;
z-index: 2;
}
}
#keyframes cardhovera {
from {
border-radius: 50%;
top: -173px;
}
76% {
top: -320px;
border-radius: 25%;
z-index: 2;
}
to {
border-radius: 0%;
top: -320px;
z-index: 2;
}
}
#-webkit-keyframes cardb {
from {
bottom: -53px;
border-radius: 50%;
}
76% {
bottom: -200px;
border-radius: 25%;
}
to {
border-radius: 0;
z-index: 5;
bottom: -200px;
}
}
#keyframes cardb {
from {
bottom: -53px;
border-radius: 50%;
}
76% {
bottom: -200px;
border-radius: 25%;
}
to {
border-radius: 0;
z-index: 5;
bottom: -200px;
}
}
#-webkit-keyframes cardhoverb {
from {
border-radius: 0;
z-index: 5;
bottom: -200px;
}
24% {
bottom: -200px;
border-radius: 25%;
}
to {
bottom: -53px;
border-radius: 50%;
}
}
#keyframes cardhoverb {
from {
border-radius: 0;
z-index: 5;
bottom: -200px;
}
24% {
bottom: -200px;
border-radius: 25%;
}
to {
bottom: -53px;
border-radius: 50%;
}
}
<div tabindex="0" class="card">
<div class="card-head card-animated">
</div>
<div class="card-extend card-animated">
</div>
</div>
I think this choppy effect you are talking about has more to do with the way animation in css work. As the easing is applied to the whole extension of it, this means, imagine some keyframes like this:
#keyframes exampleFrames {
0% {
transform: translateX(50px)
}
50% {
transform: translateX(0)
}
100% {
transform: translateX(50px)
}
}
Even though you can add easing to the animation the element affected will start at 50 pixels to the right and start moving to the left to it's initial position and in the center frame will suddenly change direction to get to the last position again. The issue is with this sudden change, this is what makes it choppy.
To avoid this you might need to use javascript or, as you've seen tweak the keyframes to minimise this undesirable visual effect.

How do I prevent Safari css keyframe animation flicker?

I've tried everything from adding extra keyframes (0%, 1%, 100% or 0%, 99%, 100%) to setting -webkit-animation-fill-mode to forwards to the oft-mentioned -webkit-backface-visibility: hidden; trick mentioned in other threads, but I'm still seeing a flicker in my css keyframe animation at the start of almost every animation iteration in Safari 7 (both desktop and iOS). Chrome seems to be flicker-free.
JSBin: http://jsbin.com/julor/2/edit
HTML:
<div class="ripple"></div>
CSS:
body {
background-color: #90CBEA;
}
.ripple, .ripple:before, .ripple:after {
background-image: radial-gradient(circle at 50% 50%, rgba(255, 255, 255, 0) 50%, rgba(255, 255, 255, .15) 100%);
border-radius: 50%;
position: absolute;
top: 50%; left: 50%;
-webkit-transform: translateX(-50%) translateY(-50%);
}
.ripple:before, .ripple:after {
content: '';
display: block;
}
.ripple {
-webkit-animation-name: innerRipple;
-webkit-animation-duration: 3s;
-webkit-animation-iteration-count: infinite;
-webkit-animation-timing-function: ease-out;
&:before {
-webkit-animation-name: ripple;
-webkit-animation-duration: 3s;
-webkit-animation-iteration-count: infinite;
-webkit-animation-timing-function: ease-out;
}
&:after {
-webkit-animation-name: outerRipple;
-webkit-animation-duration: 3s;
-webkit-animation-iteration-count: infinite;
-webkit-animation-timing-function: ease-out;
}
}
#-webkit-keyframes innerRipple {
from {
height: 0px;
width: 0px;
opacity: 1;
}
to {
height: 200px;
width: 200px;
opacity: 0;
}
}
#-webkit-keyframes ripple {
from {
height: 0px;
width: 0px;
opacity: 1;
}
to {
height: 300px;
width: 300px;
opacity: 0;
}
}
#-webkit-keyframes outerRipple {
from {
height: 0px;
width: 0px;
opacity: 1;
}
to {
height: 340px;
width: 340px;
opacity: 0;
}
}
Adding a frame in between a little earlier than at 99% made the flickering disappear on Safari! (Safari 8 OS X)
#-webkit-keyframes innerRipple {
0% { height: 0px; width: 0px; opacity: 1; }
95% { height: 200px; width: 200px; opacity: 0; }
100% { width: 0px; height: 0px; opacity: 0; }
}
#-webkit-keyframes ripple {
0% { height: 0px; width: 0px; opacity: 1; }
95% { height: 300px; width: 300px; opacity: 0; }
100% { width: 0px; height: 0px; opacity: 0; }
}
#-webkit-keyframes outerRipple {
0% { height: 0px; width: 0px; opacity: 1; }
95% { height: 340px; width: 340px; opacity: 0; }
100% { width: 0px; height: 0px; opacity: 0; }
}

Resources