【发布时间】:2016-12-02 22:00:02
【问题描述】:
我在尝试使用 jQuery 为 SVG 图形设置动画时遇到了麻烦。
我需要的 CSS 如下所示:
svg rect:hover {
fill: blue;
transition: .15s;
}
在 jQuery 中需要它,因为我希望“填充”是随机颜色而不是“蓝色”。我的问题是如何删除 mouseout/mouseleave 上的随机颜色并显示 SVG 矩形默认颜色。我所能做的就是选择另一种颜色,在示例中为紫色......
$("svg").find("rect").hover(function(){
var hue = 'rgb(' + (Math.floor((256-199)*Math.random()) + 150) + ','
+ (Math.floor((256-199)*Math.random()) + 150) + ','
+ (Math.floor((256-199)*Math.random()) + 150) + ')';
$(this).attr("fill", hue);
});
$("svg").find("rect").mouseout(function(){
$(this).attr("fill", "purple");
});
【问题讨论】: