RaphaelJS:如果从页面哈希中找到,则更改变量属性

发布于 2024-11-30 18:39:38 字数 1498 浏览 1 评论 0原文

假设我有 var mycircle = paper.circle(0, 0, 20).attr({行程: "#ff0000"});

现在默认情况下,当有人访问我的页面时 www .url.com/index.html 他们得到这个带有红色笔划的圆圈。

但是,如果我想要怎么办 如果浏览时定义了#hash,则更改圆笔画,例如 已做出选择,并且您想将其链接到某人。说某人 现在浏览www.url.com/index.html#mycircle

使用: var hash = window.location.hash.substr(1) 我可以使变量哈希返回“mycircle”。

然而,通过做 hash.attr({lines: "#0000ff"}); 描边颜色不会变成蓝色!有没有办法解决这个问题,这样我就可以像这样改变圆圈的笔划?

// 编辑

我已经像这样设置了我的圈子:

var name1 = paper.circle(....);
name1.node.id = "name1";
name1.node.name = "Name";

var name2 = paper.circle(....);
name2.node.id = "name2";
name2.node.name = "Another Name";

等等。然后我将它们组合成一个集合:

nodes = paper.set();
nodes.push(name1, name2, name3, name4....);

然后使用 nodes.hover(function() { });nodes.click(function() { }); 来定义行动。 name.node.id 用于将正确的信息页面加载到 div 中,而当用户将鼠标悬停在节点上时,name.node.name 会出现在节点上方。

我尝试按照建议重新组织节点:

var mapObjects = {};

mapObjects['name1'] = paper.circle(....);
mapObjects['name1'].node.id = "name1";
mapObjects['name1'].node.name = "Name";

mapObjects['name2'] = paper.circle(....);
mapObjects['name2'].node.id = "name1";
mapObjects['name2'].node.name = "Another Name";

然后,我尝试像以前一样使用函数,只需将节点更改为 mapObjects mapObjects.hover(function() { }); 与单击相同。这些已经不起作用了!哈希技术确实有效,所以这是朝着正确方向迈出的一步。现在如何让我的悬停和单击功能正常工作?

Say I have var mycircle = paper.circle(0, 0, 20).attr({stroke: "#ff0000"});

Now by default when someone goes to my page at www.url.com/index.html they get this circle with the red stroke.

However, what if I wanted
the circle stroke to change if a #hash was defined when browsing, e.g.
a selection was made and you wanted to link it to someone. Say someone
browses to www.url.com/index.html#mycircle now.

With: var hash = window.location.hash.substr(1) I can make the variable hash return "mycircle".

However, by doing
hash.attr({stroke: "#0000ff"}); the stroke colour wont change to blue! Is there a way around this so I could change the stroke of the circle like so?

// EDIT

I have set up my circles like so:

var name1 = paper.circle(....);
name1.node.id = "name1";
name1.node.name = "Name";

var name2 = paper.circle(....);
name2.node.id = "name2";
name2.node.name = "Another Name";

And so on. Then I have combined them into a set:

nodes = paper.set();
nodes.push(name1, name2, name3, name4....);

I then use nodes.hover(function() { }); and nodes.click(function() { }); to define actions. The name.node.id is used for loading the right info page into a div while the name.node.name appears above the node when the user hovers over it.

I tried reorganizing my nodes as suggested:

var mapObjects = {};

mapObjects['name1'] = paper.circle(....);
mapObjects['name1'].node.id = "name1";
mapObjects['name1'].node.name = "Name";

mapObjects['name2'] = paper.circle(....);
mapObjects['name2'].node.id = "name1";
mapObjects['name2'].node.name = "Another Name";

I then tried to use the functions like I did before by simply changing nodes into mapObjects mapObjects.hover(function() { }); same with click. These didn't work anymore! The hash technique worked tho so that's a step to the right direction. How do I get my hover and click functions working now?

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

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

发布评论

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

评论(1

依 靠 2024-12-07 18:39:38

评估会有所帮助,但它会非常脆弱。更好的方法是记住关联数组中的圆:

var objects = {};
objects['mycircle'] = paper.circle(0, 0, 20).attr({stroke: "#ff0000"});
objects[hash].attr({stroke: "#0000ff"});

The eval would help, but it would be very vulnerable. The better way is to remember the circle in associative array:

var objects = {};
objects['mycircle'] = paper.circle(0, 0, 20).attr({stroke: "#ff0000"});
objects[hash].attr({stroke: "#0000ff"});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文