这将是我今天的列表问题。是否可以在 jQuery 中为径向渐变设置动画(使用 .animate),如果可以,怎么做?
background: -webkit-gradient(
radial, 50% 50% ,0, 50% 50%, 70, from(rgb(25,25,25)), to(rgb(50,50,50))
);
最佳答案
默认情况下,在 jQuery 中您无法执行此操作,如果没有相应的插件,您甚至无法为平面颜色设置动画。
由于浏览器之间的语法差异,动画渐变很困难。我为一个非常具体的案例写了一个插件,可能对你有用。它适用于线性渐变,但您可以将其调整为径向。
jQuery.fx.step.gradient = function(fx) {
if (fx.state == 0) { //On the start iteration, convert the colors to arrays for calculation.
fx.start = fx.elem.style.background.match(/\d+/g); //Parse original state for numbers. Tougher because radial gradients have more of them
fx.start[0] = parseInt(fx.start[0]);
fx.start[1] = parseInt(fx.start[1]);
fx.start[2] = parseInt(fx.start[2]);
fx.end = fx.end.match(/\d+/g);
fx.end[0] = parseInt(fx.end[0]);
fx.end[1] = parseInt(fx.end[1]);
fx.end[2] = parseInt(fx.end[2]);
}
fx.elem.style.background = "-webkit-linear-gradient(rgb(" + [
Math.max(Math.min(parseInt((fx.pos * (fx.end[0] - fx.start[0])) + fx.start[0]), 255), 0),
Math.max(Math.min(parseInt((fx.pos * (fx.end[1] - fx.start[1])) + fx.start[1]), 255), 0),
Math.max(Math.min(parseInt((fx.pos * (fx.end[2] - fx.start[2])) + fx.start[2]), 255), 0)
].join(",") + ")," + "rgb(0,0,0))";
}
$(this).animate({"gradient": "rgb(0, 255, 0)"});
您可能想要创建两个函数,一个用于内部颜色 (jQuery.fx.step.innerColor
),一个用于外部 (jQuery.fx.step.outerColor
) 会这样调用:
$(this).animate({"innerColor": "rgb(25,25,25)",
"outerColor": "rgb(50,50,50)"});
关于javascript - 使用 jQuery 动画渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7533667/