简体   繁体   English

无法更改 Raphael 中的填充颜色?

[英]can't change the fill color in Raphael?

I'm trying to change the circle color from green to red but i can't here.我正在尝试将圆圈颜色从绿色更改为红色,但我不能在这里。 After long time research i found one issue, which is when i use class in Raphael i can't change the color.经过长时间的研究,我发现了一个问题,那就是当我在 Raphael 中使用 class 时,我无法改变颜色。 i need to use the class to change the color of the cirle.我需要使用该类来更改圆的颜色。

var paper = new Raphael('myPaper',500,500);

var SCircle = paper.circle(300,100,50).data('id','green');
SCircle.node.setAttribute('class','green'); 

$("#test").click(function () {   
    SCircle.animate({fill:'pink'},200); //color is not filling
} )

Here is the JSFiddle Link这是JSFiddle 链接

Any suggestions should be appreciated..任何建议都应该受到赞赏..

The problem appears to be css precedence.问题似乎是 css 优先级。 The css class takes precedence over the fill during the animate call.在 animate 调用期间,css 类优先于填充。 Read this SO post for more discussion which advises not using CSS styles for styling Raphael objects.阅读这篇SO 帖子以获取更多讨论,其中建议不要使用 CSS 样式来设置 Raphael 对象的样式。

Applying the attributes explicitly without css works.在没有 css 的情况下显式应用属性有效。

var paper = new Raphael('myPaper',500,500);    
var SCircle = paper.circle(300,100,50); 
SCircle.node.setAttribute('fill','green');
SCircle.node.setAttribute('stroke','black');
SCircle.node.setAttribute('stroke-width','3');

$("#test").click(function() {   
    SCircle.animate({'fill':'pink'},200);
}); 

setAttribute is known to be buggy for DOM attributes in some browsers.众所周知, setAttribute在某些浏览器中对于 DOM 属性是有问题的。 Try the simple className attribute instead:尝试使用简单的className属性:

SCircle.node.className = 'green';

This is known to work in all browsers.众所周知,这适用于所有浏览器。

There is an easier way to do this without using the node and maybe avoiding css .有一种更简单的方法可以在不使用node情况下执行此操作,并且可能避免css Look at the DEMO :演示

var paper = new Raphael('myPaper',500,500);

var SCircle = paper.circle(300,100,50).data('id','green');
SCircle.attr({fill: 'green', stroke: '#000', "stroke-width": 3}); 

$("#test").click(function () {   
    SCircle.animate({fill:'pink'},200); //good to go
} )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM