【发布时间】:2020-09-08 04:44:23
【问题描述】:
所以基本上,我正在尝试创建一个进度条。在这个例子中,我只是将我使用的颜色更改为red、green 和blue,因为这显然比加载十六进制值更容易理解。实际上,我想要的是让进度条有这个 RGB 渐变背景,给人的印象是渐变从左到右移动,表示仍然有活动(即网站没有冻结)。我尝试了一些东西,从设置background: linear-gradient(120deg, red, green, blue) 和动画background-position CSS 属性开始以模拟渐变移动。然而,一旦在动画结束时,进度条从大部分是蓝色(即渐变的结尾)跳回绿色......然后我尝试以rgbgr的形式手动反映渐变 - 即background: linear-gradient(120deg, red, green, blue, green, red),虽然这看起来更好,但仍然有跳跃性。最后,我尝试使用repeating-linear-gradient CSS 函数——即background: repeating-linear-gradient(120deg, red, green, blue, green, red)。这是最接近我的目标的,但在示例中,您可以看到渐变颜色“跳跃”,而不是流畅地制作动画
html, body{
height: 100%;
background: #222;
overflow: hidden;
}
body{
display: flex;
justify-content: center;
align-items: center;
}
*{
color: white;
font-family: 'Tahoma', sans-serif;
}
#wrapper {
height: 50px;
width: 400px;
position: relative;
background: #131313;
}
p{
text-align: center;
position: absolute;
width: 100%;
}
#bar {
background: repeating-linear-gradient(120deg, red,green,blue, green, red);
background-repeat:repeat-x;
height: 100%;
position: absolute;
background-size: 400% 100%;
-webkit-animation: AnimationName 3s linear infinite;
-moz-animation: AnimationName 3s linear infinite;
animation: AnimationName 3s linear infinite;
}
@-webkit-keyframes AnimationName {
0%{background-position:100% 50%}
100%{background-position:0% 50%}
}
@-moz-keyframes AnimationName {
0%{background-position:100% 50%}
100%{background-position:0% 50%}
}
@keyframes AnimationName {
0%{background-position:100% 50%}
100%{background-position:0% 50%}
}
<div id="wrapper">
<div id="bar" style="width: 50%"></div>
<p>Downloading 5 of 10</p>
</div>
我以前在很多网站上都看到过这种效果,所以我认为它在 CSS 中是可能的。如果有人能指出我正确的方向,我将不胜感激。谢谢!
【问题讨论】:
标签: html css background css-animations linear-gradients