如何使用 jQuery 将 CSS 样式应用到 Raphael.js 对象?

发布于 2024-10-10 14:20:50 字数 1709 浏览 6 评论 0原文

有人对 Raphael.js SVG 库有任何经验吗?

我正在使用 Raphael.js 创建 SVG 地图(用于智能手机),但在打开 Raphael 创建的地图对象以通过 jQuery 进行外部交互并通过 css 进行样式设置时遇到问题。

var R = Array();  
    R[1] = new Raphael("level1", 408, 286);  
    R[2] = new Raphael("level2", 408, 286);  
    R[3] = new Raphael("level3", 408, 286);  
    R[4] = new Raphael("level4", 408, 286);  
    R[5] = new Raphael("level5", 408, 286);  

var attr = {  
    fill: "#ccc",  
    stroke: "#000",  
    "stroke-width": 0.5,  
    "stroke-linecap": "square",  
    "stroke-linejoin": "miter"  
};  

// loop through a bunch of objects (not shown for brevity)
    // in the end, I end up with a couple hundred objects drawn by Raphael  

    var o = R[fID].path(coordstring).attr(attr);  

    // creates an #o[ID] id value that jQuery can target 
    o.node.id = "o"+$(this).attr('id'); // id value comes from data source  

    o.click(function(){  
        highlightMapObject(this.node.id.substr(1));                             
    );  

// end loop  

// accessed from the o.click and from outside in jQuery
function highlightMapObject(oid){  
    var $target = $("#o"+oid);  
    $target.addClass('highlight');  
}  

我似乎遇到的问题是,尝试向 Raphael 对象添加类不起作用,或者如果它起作用,则不会应用该类的 CSS 属性(例如更改的背景颜色等)。

因此,如果我的 .highlight 类是:

.highlight { background-color: #f00; }

那要么没有被应用,要么没有覆盖原始 Raphael 属性中的 fill:"ccc" 。我的猜测是,因为 jQuery 的目标 ID 位于 Raphael 对象 NODE 上,而不是对象本身,所以这可能是问题的一部分。

我见过很多在处理 Raphael 时完全避免使用节点的建议,但这似乎是我发现打开 Raphael 对象到外部目标的唯一方法(在本例中通过 jQuery)。

我不必使用 CSS 来实现这些对象的样式更改,但我必须能够从外部实现该更改(通过 jQuery - 响应外部突出显示请求等)比所有内部(通过拉斐尔并且仅响应对象点击)

有想法吗?

谢谢!

Does anybody have any experience with the Raphael.js SVG library?

I'm using Raphael.js to create an SVG map (for use on smartphones) but I'm having trouble opening the map objects that Raphael creates up to outside interaction by jQuery and styling by css.

var R = Array();  
    R[1] = new Raphael("level1", 408, 286);  
    R[2] = new Raphael("level2", 408, 286);  
    R[3] = new Raphael("level3", 408, 286);  
    R[4] = new Raphael("level4", 408, 286);  
    R[5] = new Raphael("level5", 408, 286);  

var attr = {  
    fill: "#ccc",  
    stroke: "#000",  
    "stroke-width": 0.5,  
    "stroke-linecap": "square",  
    "stroke-linejoin": "miter"  
};  

// loop through a bunch of objects (not shown for brevity)
    // in the end, I end up with a couple hundred objects drawn by Raphael  

    var o = R[fID].path(coordstring).attr(attr);  

    // creates an #o[ID] id value that jQuery can target 
    o.node.id = "o"+$(this).attr('id'); // id value comes from data source  

    o.click(function(){  
        highlightMapObject(this.node.id.substr(1));                             
    );  

// end loop  

// accessed from the o.click and from outside in jQuery
function highlightMapObject(oid){  
    var $target = $("#o"+oid);  
    $target.addClass('highlight');  
}  

The issue I seem to be running into is that trying to add a class to the Raphael object doesn't work, or if it is working, the CSS attributes of that class (like changed background colors, etc) aren't being applied.

So if my .highlight class is:

.highlight { background-color: #f00; }

That is either not being applied, or isn't overwriting the fill:"ccc" from the original Raphael attrs. My guess is that because the ID being targeted by jQuery is on the Raphael object NODE rather than the object itself, that's probably part of the problem.

I've seen lots of advice to avoid node altogether when dealing with Raphael, but it seems to be the only way I've found to open a Raphael object up to outside targeting (in this case via jQuery).

I don't have to use CSS to achieve the style change on these objects, but I do have to be able to achieve that change externally (via jQuery - in response to external highlight requests, etc) rather than all internally (via Raphael and only in response to object clicks).

Ideas?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

等待圉鍢 2024-10-17 14:20:50

我不太确定你的代码在做什么,但是如果你想从 Raphael 对象中获取 jQuery 对象,那么可以这样做:

var $jQueryObject = $(raphaelObject.node);

从那里你可以使用 jQuery 添加一个类:

$jQueryObject.addClass('highlight');

I am not exactly sure what you code is doing, but if you want to get a jQuery object out of a Raphael object then do this:

var $jQueryObject = $(raphaelObject.node);

From there you can use jQuery to add a class:

$jQueryObject.addClass('highlight');
深空失忆 2024-10-17 14:20:50

我还发现,如果在用 raphael 渲染路径后删除内联样式。

$('#somediv path').removeAttr('fill').removeAttr('stroke');

然后你可以使用 css 随意设置它们的样式

#somediv path { fill: white; }
#somediv:hover path { fill: blue; }

I also found that if you remove the inline styles after rendering the path with raphael.

$('#somediv path').removeAttr('fill').removeAttr('stroke');

then you can style them how ever you want using css

#somediv path { fill: white; }
#somediv:hover path { fill: blue; }
星光不落少年眉 2024-10-17 14:20:50

或者您添加一个类作为属性

$jQueryObject.attr('class', 'highlight');

这将起作用而不是

$jQueryObject.addClass('highlight');

Or you add a class as an attribute

$jQueryObject.attr('class', 'highlight');

This will work instead of

$jQueryObject.addClass('highlight');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文