Gradient Animation - prevent the "skipping" - css

https://jsfiddle.net/2ndjazmy/14/
#keyframes gradient-animation {
from {
background-position: 100% 0%;
}
to {
background-position: 0% 0%;
}
}
.animation{
background: linear-gradient(90deg, #f11e1d, #952491 12.5%, #8cc838 25%, #feb034 37.5%, #f11e1d 50%, #952491 62.5%, #8cc838 75%, #feb034 87.5%);
background-size: 200%;
background-position: 100% 0%;
animation-name: gradient-animation;
animation-duration: 2s;
animation-iteration-count: infinite;
animation-fill-mode: forwards;
animation-timing-function: linear;
height: 50px;
}
<div class="animation"></div>
I'm trying to make a gradient animation effect, in order to transition into other colours. You can see I almost have it working, but at the end of the animation the colours "skip" back to the beginning. I think it's because I don't have the colour stops set up right, but I'm not sure how to fix it.

You can correct it like this (I added extra values to better see the pattern)
#keyframes gradient-animation {
from {
background-position: 100% 0%;
}
to {
background-position: 0% 0%;
}
}
.animation{
background: linear-gradient(90deg,
#f11e1d 0%, #952491 12.5%, #8cc838 25%, #feb034 37.5%, #f11e1d 50%,
#f11e1d 50%, #952491 62.5%, #8cc838 75%, #feb034 87.5%, #f11e1d 100%);
background-size: 200%;
background-position: 100% 0%;
animation-name: gradient-animation;
animation-duration: 2s;
animation-iteration-count: infinite;
animation-fill-mode: forwards;
animation-timing-function: linear;
height: 50px;
}
<div class="animation"></div>
But to avoid complex situation you can consider repeating-linear-gradient and you don't have to bother about calculation.
#keyframes gradient-animation {
from {
background-position: 100% 0%;
}
to {
background-position: 0% 0%;
}
}
.animation{
background: repeating-linear-gradient(90deg,
#f11e1d 0%, #952491 12.5%, #8cc838 25%, #feb034 37.5%, #f11e1d 50%);
background-size: 200%;
background-position: 100% 0%;
animation-name: gradient-animation;
animation-duration: 2s;
animation-iteration-count: infinite;
animation-fill-mode: forwards;
animation-timing-function: linear;
height: 50px;
}
<div class="animation"></div>

It skips because the colors at the two ends of the gradient are different. You can fix this by simply adding an extra color at the end of the gradient that matches the color at the beginning. That is, changing this:
background: linear-gradient(90deg, #f11e1d, #952491 12.5%, ... , #feb034 87.5%);
to this:
background: linear-gradient(90deg, #f11e1d, #952491 12.5%, ... , #feb034 87.5%, #f11e1d 100%);
Example on JSFiddle

Related

clip-path is not animating?

WHAT I HAVE
I have a div (class = circle), I set key-fames for 4s so that it nicely animates from triangle to octagon.
body {
background-color: #252525;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.circle {
width: 30rem;
height: 5rem;
background-color: #1c7ed6;
animation: wave--animate 4s infinite ease-in-out alternate;
}
#keyframes wave--animate {
0% {
/* triangle */
clip-path: polygon(50% 0%, 0% 100%, 100% 100%);
}
100% {
/* octagon */
clip-path: polygon(30% 0%, 70% 0%, 100% 30%, 100% 70%, 70% 100%, 30% 100%, 0% 70%, 0% 30%);
}
}
<body>
<section class="section--water-waves">
<p class=circle></p>
</section>
</body>
WHAT I NEED
but, it immediately changes to octagon, instead of nicely and slowly getting animated to octagon...

Why does this animation start not to have transition when I add a linear gradient over it? [duplicate]

I want to move my gradient that has multiple colors smoothly but the problem is that the animation is not smooth. It just changes its position at every step.
<style>
.animated {
width: 300px;
height: 300px;
border: 1px solid black;
animation: gra 5s infinite;
animation-direction: reverse;
-webkit-animation: gra 5s infinite;
-webkit-animation-direction: reverse;
animation-timing-function: linear;
-webkit-animation-timing-function: linear;
}
#keyframes gra {
0% {
background: -webkit-gradient(linear, left top, right bottom, color-stop(0%, #ff670f), color-stop(21%, #ff670f), color-stop(56%, #ffffff), color-stop(88%, #0eea57));
background: -webkit-linear-gradient(-45deg, #ff670f 0%, #ff670f 21%, #ffffff 56%, #0eea57 88%);
background: linear-gradient(135deg, #ff670f 0%, #ff670f 21%, #ffffff 56%, #0eea57 88%);
}
50% {
background: -webkit-gradient(linear, left top, right bottom, color-stop(0%, #ff670f), color-stop(10%, #ff670f), color-stop(40%, #ffffff), color-stop(60%, #0eea57));
background: -webkit-linear-gradient(-45deg, #ff670f 0%, #ff670f 10%, #ffffff 40%, #0eea57 60%);
background: linear-gradient(135deg, #ff670f 0%, #ff670f 10%, #ffffff 40%, #0eea57 60%);
}
100% {
background: -webkit-gradient(linear, left top, right bottom, color-stop(0%, #ff670f), color-stop(5%, #ff670f), color-stop(10%, #ffffff), color-stop(40%, #0eea57));
background: -webkit-linear-gradient(-45deg, #ff670f 0%, #ff670f 5%, #ffffff 10%, #0eea57 40%);
background: linear-gradient(135deg, #ff670f 0%, #ff670f 5%, #ffffff 10%, #0eea57 40%);
}
}
</style>
<div class="animated">
<h1>Hello</h1>
</div>
Is it possible to accomplish without using jQuery?
My jsfiddle link is https://jsfiddle.net/bAUK6
Please try this code:
#gradient
{
height:300px;
width:300px;
border:1px solid black;
font-size:30px;
background: linear-gradient(130deg, #ff7e00, #ffffff, #5cff00);
background-size: 200% 200%;
-webkit-animation: Animation 5s ease infinite;
-moz-animation: Animation 5s ease infinite;
animation: Animation 5s ease infinite;
}
#-webkit-keyframes Animation {
0%{background-position:10% 0%}
50%{background-position:91% 100%}
100%{background-position:10% 0%}
}
#-moz-keyframes Animation {
0%{background-position:10% 0%}
50%{background-position:91% 100%}
100%{background-position:10% 0%}
}
#keyframes Animation {
0%{background-position:10% 0%}
50%{background-position:91% 100%}
100%{background-position:10% 0%}
}
<html>
<div id="gradient">
Hello
</div>
</html>
Dynamic implementation of Dave's answer:
:root{
--overlay-color-1: #ff0000;
--overlay-color-2: #0000ff;
--anim-duration: 2s;
}
#gradient {
opacity: 0.8;
background: none;
}
#gradient:after,
#gradient:before {
content: '';
display: block;
position: absolute;
top: 0; bottom: 0; left: 0; right: 0;
}
#gradient:before {
background: linear-gradient(135deg, var(--overlay-color-2) 0%, var(--overlay-color-1) 100%);
animation: OpacityAnim var(--anim-duration) ease-in-out 0s infinite alternate;
}
#gradient:after {
background: linear-gradient(135deg, var(--overlay-color-1) 0%, var(--overlay-color-2) 100%);
animation: OpacityAnim var(--anim-duration) ease-in-out calc(-1 * var(--anim-duration)) infinite alternate;
}
#keyframes OpacityAnim {
0%{opacity: 1.0}
100%{opacity: 0.0}
}
<div id="gradient"></div>
Using CSS variables it's now a trivial task.
Here is a basic example (hover to see the result)
#property --a{
syntax: '<angle>';
inherits: false;
initial-value: 90deg;
}
#property --l{
syntax: '<percentage>';
inherits: false;
initial-value: 10%;
}
#property --c{
syntax: '<color>';
inherits: false;
initial-value: red;
}
.box {
/* needed for firefox to have a valid output */
--a:80deg;
--l:10%;
--c:red;
/**/
cursor:pointer;
height:200px;
transition:--a 0.5s 0.1s,--l 0.5s,--c 0.8s;
background:linear-gradient(var(--a), var(--c) var(--l),blue,var(--c) calc(100% - var(--l)));
}
.box:hover {
--a:360deg;
--l:40%;
--c:green;
}
<div class="box"></div>
More details here: https://dev.to/afif/we-can-finally-animate-css-gradient-kdk
How about this:
Set the body margin and padding to 0. Set an html rule to 100% height (higher than 100% may be required).
Set the body to the end state for the gradient.
Create an empty div with a background which is the start state for the gradient. Give the empty div 100% height.
Give both the body and the empty div a background-attachment: fixed;
Create a wrapper for your body content.
Set the empty div to position: fixed;
Set the wrapper to position: relative;
Give both a z-index, the wrapper being higher.
Create an animation that will change the opacity of the empty div from 1 to 0 over the desired time. Add animation-fill-mode:forwards; to the div rule so the animation stays where it ends.
It's not as sexy as a real animated gradient shift, but it's as simple as you can get with CSS only and keyframes, I think.
Here is another way. The following has the static gradient containing all phases of the animation, which is then moved inside the outer element. This allows to perform animation smoothly (as the topic suggests), because the only animation here is the element position.
Please note that for the sake of performance the gradient element left unchanged. Although the question was to animate the gradient, moving the background does practically the same thing, while the performance wins!
.animated {
width: 300px;
height: 300px;
overflow: hidden;
position: relative;
border: 1px solid black;
}
.innerGradient {
z-index: -1;
width: 300%;
height: 300%;
position: absolute;
animation: gra 5s infinite;
-webkit-animation: gra 5s infinite;
background: linear-gradient(135deg, #ff670f 0%, #ff670f 20%, #ffffff 50%, #0eea57 80%, #0eea57 100%);
background: -webkit-linear-gradient(135deg, #ff670f 0%, #ff670f 20%, #ffffff 50%, #0eea57 80%, #0eea57 100%);
}
#keyframes gra {
0% { left: -200%; top: -200%; }
50% { left: 0%; top: 0%; }
100% { left: -200%; top: -200%; }
}
<div class="animated">
<h1>Hello</h1>
<div class="innerGradient"></div>
</div>

Keyframes - gradient background is not animated

I am trying to animate background exchange using #keyframes. Background is radial gradient. Backgroud changes but it isn't animated.
#mydiv {
background: radial-gradient(circle at 90% 80%, #ff0000, #4d0019);
width: 100%;
height: 1246px;
position: relative;
overflow: hidden;
animation: background-gradient 5s;
animation-iteration-count: infinite;
backface-visibility: hidden;
animation-timing-function: ease-in-out;
}
#-webkit-keyframes background-gradient {
0% {background: radial-gradient(circle at 90% 80%, #ff0000, #4d0019);}
25% {background: radial-gradient(circle at 90% 80%, #993399, #4d0019);}
50% {background: radial-gradient(circle at 90% 80%, #3333cc, #4d0019);}
75% {background: radial-gradient(circle at 90% 80%, #00ffcc, #4d0019);}
100% {background: radial-gradient(circle at 90% 80%, #cc9900, #4d0019);}
}
#keyframes background-gradient {
0% {background: radial-gradient(circle at 90% 80%, #ff0000, #4d0019);}
25% {background: radial-gradient(circle at 90% 80%, #993399, #4d0019);}
50% {background: radial-gradient(circle at 90% 80%, #3333cc, #4d0019);}
75% {background: radial-gradient(circle at 90% 80%, #00ffcc, #4d0019);}
100% {background: radial-gradient(circle at 90% 80%, #cc9900, #4d0019);}
}
<div id="mydiv"></div>
I tried with just solid colors and it works fine (e.g. background: red, background: yellow...).
I tried background-image instead of just background on my gradients, it doesn't help.
Do you have any advice or know solution how to animate exhange, so it is not just rapid exhange of colors but gradual exchange from one to another.
Unfortunately, background gradients are not animatable
Luckily for you, your current desing doesn't need to animate the gradient, since the external color is constant.
Just make the gradient transparent, and set a solid changing color underneath
#mydiv {
width: 100%;
height: 600px;
position: relative;
overflow: hidden;
animation: background-gradient 5s;
animation-iteration-count: infinite;
backface-visibility: hidden;
animation-timing-function: ease-in-out;
background-image: radial-gradient(circle at 90% 80%, transparent, #4d0019);
}
#keyframes background-gradient {
0% {
background-color: #ff0000;
}
25% {
background-color: yellow;
}
50% {
background-color: #3333cc;
}
75% {
background-color: #00ffcc;
}
100% {
background-color: #cc9900;
}
}
<div id="mydiv"></div>

Hover transition on clip path change

With this code ,the hover effect is working ,the bottom right corner disappears but there is no transition,it's something wrong?
.mydiv:hover{
background-color: blue;
clip-path: polygon(0% 0%, 100% 0%, 80% 100%, 0% 100%);
transition: 0.5s ease;
}
You need to add an initial clip-path definition to have a transition between two states:
.box {
background-color: blue;
clip-path: polygon(0% 0%, 100% 0%, 100% 100%, 0% 100%);
transition: 0.5s ease;
height:150px;
}
.box:hover {
clip-path: polygon(0% 0%, 100% 0%, 80% 100%, 0% 100%);
}
<div class="box">
</div>
You can also do the same with background and you will have better support:
.box {
background:
linear-gradient(blue,blue) left,
linear-gradient(to bottom right,blue 49.5%,transparent 50%) right;
background-size:100% 100%,0% 100%;
background-repeat:no-repeat;
transition: 0.5s ease;
height:150px;
}
.box:hover {
background-size:80.1% 100%,20% 100%;
}
<div class="box">
</div>
Transition property need set to class if you want to use it for pseudo class
.mydiv {
background: red;
clip-path: polygon(0% 0%, 100% 0%, 100% 100%, 0% 100%);
transition: all 0.5s ease;
}
.mydiv:hover {
background: blue;
clip-path: polygon(0% 0%, 100% 0%, 80% 100%, 0% 100%);
}
<div class="mydiv">
Hello world
</div>

Issue with CSS animation in Firefox

I have a fiddle which is a replica of something in one of our products at work. It is working in Chrome and IE but not firefox and I've stared for a while and can't see any reason why not. I'm hoping that asking will cause a brainwave.
It seems to show as a transparent color in Firefox, the fiddle shows values in Chrome and IE.
Cheers,
J
http://jsfiddle.net/qb9unwmh/2/
body {
background: #ff0000;
-webkit-backface-visibility: hidden;
-webkit-animation-duration: 1s;
-webkit-animation-name: taskUrgent;
-webkit-animation-iteration-count: infinite;
-webkit-animation-direction: alternate;
-moz-animation-duration: 1s;
-moz-animation-name: taskUrgent;
-moz-animation-iteration-count: infinite;
-moz-animation-direction: alternate;
backface-visibility: hidden;
animation-duration: 1s;
animation-name: taskUrgent;
animation-iteration-count: infinite;
animation-direction: alternate;
}
#-webkit-keyframes taskUrgent {
0% {
background: -webkit-linear-gradient(top, #ff0000 0, #8c0000 100%);
}
100% {
background: -webkit-linear-gradient(top, #8c0000 0, #510000 100%);
}
}
#-moz-keyframes taskUrgent {
0% {
background: -moz-linear-gradient(top, #ff0000 0, #8c0000 100%);
}
100% {
background: -moz-linear-gradient(top, #8c0000 0, #510000 100%);
}
}
#keyframes taskUrgent {
0% {
background: linear-gradient(to bottom, #ff0000 0%, #8c0000 100%);
}
100% {
background: linear-gradient(to bottom, #8c0000 0%, #510000 100%);
}
}

Resources