我想有人告诉我什么是使网页背景颜色(整个页面的背景颜色)改变(淡入淡出过渡)每30秒到一个给定的颜色变化的代码。是不是很简单?我猜css会让它变得更容易?
我已经在互联网上搜索过了,我只找到了梯度,这不是我想要的。提前谢谢你。我在codepen和jsfiffle中搜索过示例,但没有人有这么简单的东西:/
示例:浏览页面时,我希望背景颜色从蓝色变为绿色,然后变为橙色,再变为蓝色,依此类推……:)
发布于 2015-01-05 19:01:19
这里是一个jQuery方法,来完成Bogdan的回答,它有3个参数:selector (例如,".container“或"div"),colors (要在其间切换的颜色数组)和time (控制bgd颜色更改的频率)。我将它设置为3秒(3000),这样您就可以更容易地看到它的实际效果,但您可以将其增加到30000 (30秒)。
jQuery(function ($) {
function changeColor(selector, colors, time) {
/* Params:
* selector: string,
* colors: array of color strings,
* every: integer (in mili-seconds)
*/
var curCol = 0,
timer = setInterval(function () {
if (curCol === colors.length) curCol = 0;
$(selector).css("background-color", colors[curCol]);
curCol++;
}, time);
}
$(window).load(function () {
changeColor(".container", ["green", "yellow", "blue", "red"], 3000);
});
});.container {
background-color: red;
height:500px;
-webkit-transition: background-color 0.5s ease-in-out;
-moz-transition: background-color 0.5s ease-in-out;
-o-transition: background-color 0.5s ease-in-out;
-khtml-transition: background-color 0.5s ease-in-out;
transition: background-color 0.5s ease-in-out;
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="container"></div>
发布于 2015-01-05 02:39:55
使用CSS3动画也可以在没有任何JavaScript的情况下实现这一点。
html,
body {
height: 100%;
}
body {
-webkit-animation: background 5s cubic-bezier(1,0,0,1) infinite;
animation: background 5s cubic-bezier(1,0,0,1) infinite;
}
@-webkit-keyframes background {
0% { background-color: #f99; }
33% { background-color: #9f9; }
67% { background-color: #99f; }
100% { background-color: #f99; }
}
@keyframes background {
0% { background-color: #f99; }
33% { background-color: #9f9; }
67% { background-color: #99f; }
100% { background-color: #f99; }
}
发布于 2015-01-04 22:17:28
像this fiddle这样的东西
CSS:
body {
background: blue; /* Initial background */
transition: background .5s; /* .5s how long transitions shoould take */
}Javascript:
var colors = ['green', 'orange', 'blue']; // Define Your colors here, can be html name of color, hex, rgb or anything what You can use in CSS
var active = 0;
setInterval(function(){
document.querySelector('body').style.background = colors[active];
active++;
if (active == colors.length) active = 0;
}, 30000);https://stackoverflow.com/questions/27766343
复制相似问题