我尝试创建渐变颜色改变横幅,像在这个站点上,但似乎遇到了严重的问题,颜色带。有人能告诉我,如果不使用画布元素,这个网站上的颜色改变效果是可能的吗?
抱歉,我是新手。
如有任何反馈,将不胜感激。
这个小提琴必须在Firefox中运行。很抱歉。
#solid {
position: absolute;
width: 100%;
height: 380px;
background: -webkit-linear-gradient(top left, rgb(105, 80, 102), #2E8ECE);
background: -o-linear-gradient(top left, rgb(105, 80, 102), #2E8ECE);
background: linear-gradient(to bottom right, rgb(105, 80, 102), #2E8ECE);
}发布于 2015-12-30 13:29:27
您可以使用linear-gradient背景图像和JavaScript来完成它,如下面的片段所示。所需要的就是使用JS rgb() setInterval方法不断地更改渐变的setInterval颜色值。
注意:编码是这样完成的,在rgb()值达到一定的阈值后,它们立即返回到原来的状态。您还可以修改代码,使其递增,直到达到某个级别,然后递减,使其在高阈值和低阈值之间振荡。
var el = document.querySelector('.gradient-div');
/* Set the initial rgb() color values for the start and end colors */
var startColorRed = 62,
startColorGreen = 79,
startColorBlue = 216,
endColorRed = 251,
endColorGreen = 38,
endColorBlue = 103;
/* set the original gradient as the element's background image */
el.style.backgroundImage = 'linear-gradient(90deg, rgb(' + startColorRed + ', ' + startColorGreen + ', ' + startColorBlue + ') 10% , rgb(' + endColorRed + ', ' + endColorGreen + ', ' + endColorBlue + ') 90%)';
/* function to change the gradient's colors */
function changeGrad() {
/* do whatever math operation that is required on the rgb values of the color */
if (endColorRed >= 151) endColorRed--;
else endColorRed = 251;
if (startColorBlue >= 116) startColorBlue--;
else startColorBlue = 216;
if (endColorBlue <= 203) endColorBlue++;
else endColorBlue = 103;
if (startColorGreen <= 179) startColorGreen++;
else startColorGreen = 79;
if (endColorGreen <= 138) endColorGreen++;
else endColorGreen = 38;
el.style.backgroundImage = 'linear-gradient(90deg, rgb(' + startColorRed + ', ' + startColorGreen + ', ' + startColorBlue + ') 10% , rgb(' + endColorRed + ', ' + endColorGreen + ', ' + endColorBlue + ') 90%)';
}
/* Call the changeGrad function at regular intervals to change the gradient's colors */
window.setInterval(changeGrad, 500);div {
height: 200px;
}<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<div class='gradient-div'></div>
https://stackoverflow.com/questions/34529921
复制相似问题