gpt4 book ai didi

JavaScript -Change CSS color for 5 seconds - 如何添加缓动效果?

转载 作者:行者123 更新时间:2023-11-28 18:48:29 26 4
gpt4 key购买 nike

引用这个问题:-

JavaScript -Change CSS color for 5 seconds

答案的工作演示:-

http://jsfiddle.net/maniator/dG2ks/

我需要知道如何为其添加缓动效果,以便颜色慢慢地变得 100% 不透明并同样变得 100% 透明。

最佳答案

代码

function makeRGBStr(obj) {
if (obj.a == null) return "rgb(" + obj.r + "," + obj.g + "," + obj.b + ")";
else return "rgba(" + obj.r + "," + obj.g + "," + obj.b + "," + obj.a + ")";
}


window["highlight"] = function(obj, color) {
var highlightColor = color || {
"r": 255,
"g": 0,
"b": 0
};


var orig = obj.style.backgroundColor;
var curAlpha = 1;
obj.style.backgroundColor = makeRGBStr(highlightColor);
setTimeout(function() {
curAlpha -= 0.1;
var newColor = highlightColor;
newColor.a = curAlpha;
obj.style.backgroundColor = makeRGBStr(newColor);

if (curAlpha <= 0) {
obj.style.backgroundColor = orig;
}
else {

setTimeout(arguments.callee, 100);
}
});
}

jsFiddle:http://jsfiddle.net/dG2ks/32/

一些例子

关于JavaScript -Change CSS color for 5 seconds - 如何添加缓动效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9955109/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com