Javascript 褪色
我有一个html div,希望背景色在所有颜色中慢慢淡出。 示例:div从#000000开始,然后到#000001,然后到..02,..03,直到到达#ffffff 这是我到目前为止的代码 HTML:Javascript 褪色,javascript,html,css,Javascript,Html,Css,我有一个html div,希望背景色在所有颜色中慢慢淡出。 示例:div从#000000开始,然后到#000001,然后到..02,..03,直到到达#ffffff 这是我到目前为止的代码 HTML: 还有,我怎样才能让它停在不同的颜色,比如#001100我在自己制作的网站上做了类似的事情 //set interval for color change setInterval(function(){colorMate();},5000); //animate body backgro
还有,我怎样才能让它停在不同的颜色,比如#001100我在自己制作的网站上做了类似的事情
//set interval for color change
setInterval(function(){colorMate();},5000);
//animate body background using color picker function
function colorMate(){
$('body').animate({backgroundColor: pickColor()},2000);
}
//declare array of colors to be used when page loads
var colors = ['#206BA4','#BBD9EE',' #EBF4FA','#C0C0C0','#E7E4D3','#F1EFE2','#52ADDA','#68B8DF','#DBDBDB','#AACD4B','#C5AE87'];
var curcolor = 0;
//picks random color from array, different from current one
function pickColor(){
var rand = Math.floor(Math.random() * 11);
if (rand == curcolor){
pickColor();
}
else {
curcolor = rand;
return colors[rand];
}
}
我在自己制作的网站上做类似的事情
//set interval for color change
setInterval(function(){colorMate();},5000);
//animate body background using color picker function
function colorMate(){
$('body').animate({backgroundColor: pickColor()},2000);
}
//declare array of colors to be used when page loads
var colors = ['#206BA4','#BBD9EE',' #EBF4FA','#C0C0C0','#E7E4D3','#F1EFE2','#52ADDA','#68B8DF','#DBDBDB','#AACD4B','#C5AE87'];
var curcolor = 0;
//picks random color from array, different from current one
function pickColor(){
var rand = Math.floor(Math.random() * 11);
if (rand == curcolor){
pickColor();
}
else {
curcolor = rand;
return colors[rand];
}
}
下面的脚本将生成HTML页面中所有可能的颜色 循环相当重,因此需要时间执行。您可以根据需要更改循环的参数 我用不同的div显示了输出,每个div都有不同的颜色 这些div是动态创建的,可以使用css更改高度宽度
<html>
<head>
<style type='text/css'>
div{
height:5px;
width:5px;
float:left;
}
</style>
</head>
<body id='main' style='width:100%:height:100%;'>
</body>
<script type='text/javascript'>
/* Loop's Start here */
for(x=0;x<225;x++){ // changes the value the parameter to minminze the loop run
for(y=0;y<225;y++){
for(z=0;z<225;z++){
element = document.createElement("div"); //creating dynamic div
element.id ='id_'+x+'_'+y+'_'+z; //assing id to newly created div
document.body.appendChild(element); // append div to main body
document.getElementById('id_'+x+'_'+y+'_'+z).style.background='rgb('+x+','+y+','+z+')'; //change the color of the div
}
}
}
</script>
</html>
div{
高度:5px;
宽度:5px;
浮动:左;
}
/*我们从这里开始*/
下面的(x=0;x脚本将生成HTML页面中所有可能的颜色
循环相当重,因此需要时间执行。您可以根据需要更改循环的参数
我用不同的div显示了输出,每个div都有不同的颜色
这些div是动态创建的,可以使用css更改高度宽度
<html>
<head>
<style type='text/css'>
div{
height:5px;
width:5px;
float:left;
}
</style>
</head>
<body id='main' style='width:100%:height:100%;'>
</body>
<script type='text/javascript'>
/* Loop's Start here */
for(x=0;x<225;x++){ // changes the value the parameter to minminze the loop run
for(y=0;y<225;y++){
for(z=0;z<225;z++){
element = document.createElement("div"); //creating dynamic div
element.id ='id_'+x+'_'+y+'_'+z; //assing id to newly created div
document.body.appendChild(element); // append div to main body
document.getElementById('id_'+x+'_'+y+'_'+z).style.background='rgb('+x+','+y+','+z+')'; //change the color of the div
}
}
}
</script>
</html>
div{
高度:5px;
宽度:5px;
浮动:左;
}
/*我们从这里开始*/
对于(x=0;xYou意识到这是1600万种颜色,对吗?检查所有颜色需要一段时间,因为如果你想真正显示所有颜色,你必须以显示器的刷新率或更慢的速度进行迭代。在60赫兹的频率下,这是3.23天。@JimGarrison哦,我没想到,会检查每一个fith数字work@qwert只需制作一个颜色数组并循环。变换颜色,你就会得到介于两者之间的颜色。不,没有“每五种颜色”这样的东西。颜色实际上是三个组成部分,红色、绿色和蓝色,组合成一个24位整数。你想做什么,到目前为止你实际做了什么。人们通常不会为你编写所有的代码。你意识到这是1600万种颜色,对吗?需要一段时间才能完成所有颜色,因为如果你想采取行动的话你必须以显示器的刷新率或更慢的速度迭代显示所有这些内容。以60赫兹的频率迭代,这是3.23天。@JimGarrison哦,我没想到,会遍历每一个fith数字work@qwertie只需制作一个颜色数组并循环。转换颜色,你就会得到介于两者之间的颜色。不,没有没有“每五种颜色”这样的东西。颜色实际上是三个组成部分,红色、绿色和蓝色,打包成一个24位整数。您试图实现什么,到目前为止您实际尝试了什么。人们通常不会为您编写所有代码。这对我不起作用。这是一个链接,因为您没有添加jQuery和jQuery UI,也没有修改jQuery selector正确,我也会将Javascript移到Javascript字段:)这对我不起作用这里有一个链接,因为您没有添加jQuery和jQuery UI,没有正确修改jQuery选择器,我也会将Javascript移到Javascript字段:)这实际上是他想要的,但是这个循环很可怕:)+1这个循环确实有很好的效果,虽然当我运行它时,我不想循环每种颜色,所以我使用了(x+=10;y+=10
和z+=10
)。这实际上是他想要的,但是这个循环很可怕:)+1这个确实有很好的效果,虽然当我运行它时,我不想循环使用每种颜色,所以我使用了(x+=10;y+=10
和z+=10
)。