我有一个2x4的盒子使用' div'在HTML中,并在CSS中格式化。 this
<body>
<div class="container">
<div class="box rightSpacing">
<!--Box 1--->
</div>
<div class="box rightSpacing" id="green">
<!--Box 2--->
</div>
<div class="box rightSpacing">
<!--Box 3--->
</div>
<div class="box">
<!--Box 4--->
</div>
<div class="box rightSpacing">
<!--Box 5--->
</div>
<div class="box rightSpacing" id="yellow">
<!--Box 6--->
</div>
<div class="box rightSpacing">
<!--Box 7--->
</div>
<div class="box">
<!--Box 8--->
</div>
</div>
</body>
我需要以类似的风格制作动画&#39; div id =&#34; green&#34;&#39;在它之后直接变成绿色,持续相似的时间长度。这是我需要帮助的地方,我被困住了。感谢
这是我在javascript中已经用于动画的代码。
lerp = function(a, b, u) {
return (1 - u) * a + u * b;
};
fade = function(element, property, start, end, duration) {
var interval = 10;
var steps = duration / interval;
var step_u = 1.0 / steps;
var u = 0.0;
var theInterval = setInterval(function() {
if (u >= 1.0) {
clearInterval(theInterval)
}
var r = parseInt(lerp(start.r, end.r, u));
var g = parseInt(lerp(start.g, end.g, u));
var b = parseInt(lerp(start.b, end.b, u));
var colorname = 'rgb(' + r + ',' + g + ',' + b + ')';
el.style.setProperty(property, colorname);
u += step_u;
}, interval);
};
el = document.getElementById('yellow');
property = 'background-color';
startColor = {
r: 255,
g: 255,
b: 255,
};
endColor = {
r: 255,
g: 255,
b: 0
};
fade(el, 'background-color', startColor, endColor, 10);
setTimeout(function() {
fade(el, 'background-color', endColor, startColor, 10);
}, 1000);
答案 0 :(得分:1)
您可以采取以下方式。
el = document.getElementById('yellow');
el1 = document.getElementById('green');
setTimeout(function() {
fade(el, 'background-color', endColor, startColor, 10);
fade(el1, 'background-color', greeColor, startColor, 2000);
}, 1000);
改变功能
el.style.setProperty(property, colorname);
要
element.style.setProperty(property, colorname);
<强> Working Fiddle 强>