如何为 CSS 渐变停止设置动画并平滑过渡到透明?
How do I animate a CSS gradient stop with a smooth transition to transparent?
我有下面的代码。如何平滑地过渡渐变停止?
它只是突然从一个变为另一个。
我见过的大多数渐变动画示例似乎都使用渐变位置,但我相信改变渐变停止点也应该是可能的。
.test {
display: inline-block;
width: 300px;
height: 300px;
border-radius: 100%;
background: conic-gradient(red, red);
background-repeat: no-repeat;
background-size: cover;
background-position: center center;
background-size: auto;
-webkit-mask:radial-gradient(circle, transparent 50%, white calc(50% + 1px));
animation:
rotate
4.5s
ease-out
0s
infinite
none
running;
}
@keyframes rotate {
0% {
background-image: conic-gradient(red, red);
}
30% {
background-image: conic-gradient(red 70%, transparent);
}
70% {
background-image: conic-gradient(red 30%, transparent, transparent, transparent);
}
100% {
background-image: conic-gradient(red, transparent);
}
}
<div class="test"></div>
在这种特殊情况下,您可以像下面这样为背景色图层设置动画:
.test {
display: inline-block;
width: 300px;
height: 300px;
border-radius: 100%;
background:
conic-gradient(red, transparent)
red;
-webkit-mask:radial-gradient(circle, transparent 50%, white calc(50% + 1px));
animation:
rotate
4.5s
ease-out
0s
infinite
none
running;
}
@keyframes rotate {
100% {
background-color:blue;
}
}
<div class="test"></div>
更新
这是另一个结合不透明度和渐变动画的想法(有点小问题,但会尝试优化它)
.test {
display: inline-block;
width: 300px;
height: 300px;
border-radius: 100%;
background: conic-gradient(red 70%, transparent);
animation: grad1 3s linear infinite;
-webkit-mask: radial-gradient(circle, transparent 50%, white calc(50% + 1px));
position: relative;
}
.test::before {
content: "";
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
opacity: 1;
background: conic-gradient(red, red);
animation:
op 1s linear infinite,
grad 3s linear infinite;
}
@keyframes op {
100% {
opacity: 0;
}
}
@keyframes grad {
0%,
33.32% {
background: conic-gradient(red, red);
}
33.328%,
66.65% {
background: conic-gradient(red 70%, transparent);
}
66.658%,
100% {
background: conic-gradient(red 30%, transparent);
}
}
@keyframes grad1 {
0%,
33.32% {
background: conic-gradient(red 70%, transparent);
}
33.328%,
66.65% {
background: conic-gradient(red 30%, transparent);
}
66.658%,
100% {
background: conic-gradient(red, transparent);
}
}
<div class="test"></div>
截至 2020 年 12 月 3 日,这仅适用于 Chrome 或 Edge 95+
可以使用 CSS @property
.
设置渐变动画
@property --opacity {
syntax: '<percentage>';
initial-value: 100%;
inherits: false;
}
.test {
display: inline-block;
position: absolute;
border-radius: 100%;
background-image: conic-gradient(
red var(--opacity),
red 10%,
rgba(255, 0, 0, var(--opacity)),
transparent,
transparent
);
will-change: transform, background-image;
width: 200px;
height: 200px;
mask:radial-gradient(circle, transparent 47%, white calc(47% + 1px));
-webkit-mask:radial-gradient(circle, transparent 47%, white calc(47% + 1px));
animation:
conic-gradient
4.5s
ease-out
0s
infinite
none
running;
}
@keyframes conic-gradient {
50% {
--opacity: 0%;
}
85% {
--opacity: 100%;
}
}
<div class="test"></div>
我有下面的代码。如何平滑地过渡渐变停止? 它只是突然从一个变为另一个。
我见过的大多数渐变动画示例似乎都使用渐变位置,但我相信改变渐变停止点也应该是可能的。
.test {
display: inline-block;
width: 300px;
height: 300px;
border-radius: 100%;
background: conic-gradient(red, red);
background-repeat: no-repeat;
background-size: cover;
background-position: center center;
background-size: auto;
-webkit-mask:radial-gradient(circle, transparent 50%, white calc(50% + 1px));
animation:
rotate
4.5s
ease-out
0s
infinite
none
running;
}
@keyframes rotate {
0% {
background-image: conic-gradient(red, red);
}
30% {
background-image: conic-gradient(red 70%, transparent);
}
70% {
background-image: conic-gradient(red 30%, transparent, transparent, transparent);
}
100% {
background-image: conic-gradient(red, transparent);
}
}
<div class="test"></div>
在这种特殊情况下,您可以像下面这样为背景色图层设置动画:
.test {
display: inline-block;
width: 300px;
height: 300px;
border-radius: 100%;
background:
conic-gradient(red, transparent)
red;
-webkit-mask:radial-gradient(circle, transparent 50%, white calc(50% + 1px));
animation:
rotate
4.5s
ease-out
0s
infinite
none
running;
}
@keyframes rotate {
100% {
background-color:blue;
}
}
<div class="test"></div>
更新
这是另一个结合不透明度和渐变动画的想法(有点小问题,但会尝试优化它)
.test {
display: inline-block;
width: 300px;
height: 300px;
border-radius: 100%;
background: conic-gradient(red 70%, transparent);
animation: grad1 3s linear infinite;
-webkit-mask: radial-gradient(circle, transparent 50%, white calc(50% + 1px));
position: relative;
}
.test::before {
content: "";
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
opacity: 1;
background: conic-gradient(red, red);
animation:
op 1s linear infinite,
grad 3s linear infinite;
}
@keyframes op {
100% {
opacity: 0;
}
}
@keyframes grad {
0%,
33.32% {
background: conic-gradient(red, red);
}
33.328%,
66.65% {
background: conic-gradient(red 70%, transparent);
}
66.658%,
100% {
background: conic-gradient(red 30%, transparent);
}
}
@keyframes grad1 {
0%,
33.32% {
background: conic-gradient(red 70%, transparent);
}
33.328%,
66.65% {
background: conic-gradient(red 30%, transparent);
}
66.658%,
100% {
background: conic-gradient(red, transparent);
}
}
<div class="test"></div>
截至 2020 年 12 月 3 日,这仅适用于 Chrome 或 Edge 95+
可以使用 CSS @property
.
@property --opacity {
syntax: '<percentage>';
initial-value: 100%;
inherits: false;
}
.test {
display: inline-block;
position: absolute;
border-radius: 100%;
background-image: conic-gradient(
red var(--opacity),
red 10%,
rgba(255, 0, 0, var(--opacity)),
transparent,
transparent
);
will-change: transform, background-image;
width: 200px;
height: 200px;
mask:radial-gradient(circle, transparent 47%, white calc(47% + 1px));
-webkit-mask:radial-gradient(circle, transparent 47%, white calc(47% + 1px));
animation:
conic-gradient
4.5s
ease-out
0s
infinite
none
running;
}
@keyframes conic-gradient {
50% {
--opacity: 0%;
}
85% {
--opacity: 100%;
}
}
<div class="test"></div>