gpt4 book ai didi

javascript - 一段时间后更改线性渐变背景

转载 作者:太空狗 更新时间:2023-10-29 15:21:04 26 4
gpt4 key购买 nike

我想在一段时间后更改背景。如果背景具有“清晰”颜色,则没有问题,但如果颜色是渐变设置,则代码不起作用。有解决办法吗?

background: -webkit-linear-gradient(rgba(39,49,67,1), rgba(226,228,209,1)); /*For Safari 5.1 to 6.0 */
background: -o-linear-gradient(rgba(39,49,67,1), rgba(226,228,209,1)); /*For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(rgba(39,49,67,1),rgba(39,49,67,1), rgba(226,228,209,1)); /*For Firefox 3.6 to 15 */
background: linear-gradient(rgba(39,49,67,1),rgba(51,90,109,1),rgba(83,142,144,1), rgba(226,228,209,1)); /*Standard syntax */

jsfiddle对于正常的颜色变化

最佳答案

Dbugger 说的是真的 - 你不能用 css 动画来动画背景图像。

但是,您可以使用 4 级渐变来伪造它(巧妙地定位您的颜色停止 - 我建议使用渐变生成器作为 Colorzilla 或类似的 - 将使您的工作更轻松) - 因为渐变被视为 background-image,您可以使用background-position 为它的位置设置动画。为了给位置设置动画,您需要增加它的大小,这样渐变的一部分就在您的容器之外。

您可以使用animation-delay 设置动画开始前的初始延迟。

html, body {width:100%;height:100%;margin:0;}
div {
width: 100%;
height: 100%;
background: -moz-linear-gradient(top, rgba(30,87,153,1) 0%, rgba(118,191,36,1) 25%, rgba(224,117,35,1) 50%, rgba(242,38,42,1) 75%, rgba(130,100,70,1) 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(30,87,153,1)), color-stop(25%,rgba(118,191,36,1)), color-stop(50%,rgba(224,117,35,1)), color-stop(75%,rgba(242,38,42,1)), color-stop(100%,rgba(130,100,70,1)));
background: -webkit-linear-gradient(top, rgba(30,87,153,1) 0%,rgba(118,191,36,1) 25%,rgba(224,117,35,1) 50%,rgba(242,38,42,1) 75%,rgba(130,100,70,1) 100%);
background: linear-gradient(to bottom, rgba(30,87,153,1) 0%,rgba(118,191,36,1) 25%,rgba(224,117,35,1) 50%,rgba(242,38,42,1) 75%,rgba(130,100,70,1) 100%);
background-size: 100% 400%;
background-position:0 0;
-webkit-animation: animateGradient 5s ease 1;
-moz-animation: animateGradient 5s ease 1;
animation: animateGradient 5s ease 1;
-webkit-animation-delay: 2s;
-moz-animation-delay: 2s;
animation-delay: 2s;
}


@-webkit-keyframes animateGradient {
0% {background-position: 0 0;}
50% {background-position: 0 100%;}
100% {background-position: 0 0;}
}
@-moz-keyframes animateGradient {
0% {background-position: 0 0;}
50% {background-position: 0 100%;}
100% {background-position: 0 0;}
}
@keyframes animateGradient {
0% {background-position: 0 0;}
50% {background-position: 0 100%;}
100% {background-position: 0 0;}
}
<div></div>


备选方案:您可以采用的另一种方法是将一个元素叠加在另一个元素之上,在顶部设置初始渐变,在底部元素设置结束渐变,然后创建一个不透明度动画,淡出一定时间后的顶部元素(opacity: 0)

下面是关于如何使用标记中的单个元素执行此操作的方法(依赖于 :after:before 伪元素)。以下方法具有更多的跨设备兼容性:

html, body {width:100%;height:100%;margin:0;}
.gradient {
position:relative;
width: 100%;
height: 100%;
background: -webkit-linear-gradient(rgba(39,49,67,1), rgba(226,228,209,1));
background: -o-linear-gradient(rgba(39,49,67,1), rgba(226,228,209,1));
background: -moz-linear-gradient(rgba(39,49,67,1),rgba(39,49,67,1), rgba(226,228,209,1));
background: linear-gradient(rgba(39,49,67,1),rgba(39,49,67,1), rgba(226,228,209,1));
}
.gradient:after {
content:"";
position:absolute;
top:0;
left:0;
width: 100%;
height: 100%;
background: -webkit-linear-gradient(rgba(226,228,209,1), rgba(39,49,67,1));
background: -o-linear-gradient(rgba(226,228,209,1), rgba(39,49,67,1));
background: -moz-linear-gradient(rgba(226,228,209,1), rgba(39,49,67,1));
background: linear-gradient(rgba(226,228,209,1), rgba(39,49,67,1));
opacity: 0;
-webkit-animation: animateGradient 3s linear 1;
-moz-animation: animateGradient 3s linear 1;
animation: animateGradient 3s linear 1;
}

@-webkit-keyframes animateGradient {
0% {opacity:1;}
100% {opacity:0;}
}
@-moz-keyframes animateGradient {
0% {opacity:1;}
100% {opacity:0;}
}
@keyframes animateGradient {
0% {opacity:1;}
100% {opacity:0;}
}
<div class="gradient"></div>

关于javascript - 一段时间后更改线性渐变背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27493773/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com