使用 Raphael 和 JavaScript 实现数组悬停

发布于 2024-09-25 08:56:17 字数 2068 浏览 2 评论 0原文

使用 Raphael,当您将鼠标悬停在形状附近的不可见区域上时,我试图显示一个形状。以下代码不起作用 - 我哪里出错了?

每个形状都有两条与其关联的拉斐尔路径:一条用于可见形状,另一条用于不可见区域。

<script type="text/javascript" charset="utf-8">
 $("document").ready(function() {
        var RM  = Raphael("canvas", 1000, 500);

        var attr = {     // for the visible shapes
            fill: "#bbb",      "fill-opacity": 1,
            stroke: "#222",    "stroke-width": 0.3,
        };
        var attr2 = {    // for the invisible hovering areas
            fill: "green",     "fill-opacity": 0.0,
            stroke: "red",     "stroke-width": 0,
        };
        var things = {};

        /* Square */    things.square     = [ RM.path("m 154.21525,71.431259 74.32805,0 0,70.496711 -74.32805,0 0,-70.496711 z").attr(attr),
                                              RM.path("m 271.25132,77.933263 58.07304,0 0,56.409037 -58.07304,0 0,-56.409037 z").attr(attr2)   ];
        /* Triangle */  things.triangle   = [ RM.path("m 154.02932,222.44063 36.78089,-58.23641 34.48208,58.2364 -71.26297,1e-5").attr(attr),
                                              RM.path("m 271.25132,165.71032 58.07304,0 0,56.40903 -58.07304,0 0,-56.40903 z").attr(attr2)   ];
        for (var shape in things) {
            shape[0].color = Raphael.getColor();

shape[1].onmouseover = function () {
 shape[0].animate({fill:shape[0].color, stroke:"#ccc"}, 500);
 document.getElementById(shape)[0].style.display = "block";
 shape[0].toFront();   R.safari();
};
shape[1].onmouseout = function () {
 shape[0].animate({fill:"#bbb", stroke:"#222"}, 500);
 document.getElementById(shape)[0].style.display = "none";
 shape[0].toFront();   R.safari();
};
        } // end for every member of things
    }); // end the document ready function
</script>

Raphael 网站上的示例使用了更复杂的 javascript: http://raphaeljs.com/australia.html

我没有使用此代码,因为我不不理解他们使用的函数语法:

(function (a, b) {
    ----
})(c, d);

但我仍然不明白我的版本有什么问题。

Using Raphael, I'm trying to get a shape to appear when you hover over an invisible area near the shape. The following code does not work - where am I going wrong?

Each shape has two Raphael paths associated with it: one for the visible shape, and the other for the invisible area.

<script type="text/javascript" charset="utf-8">
 $("document").ready(function() {
        var RM  = Raphael("canvas", 1000, 500);

        var attr = {     // for the visible shapes
            fill: "#bbb",      "fill-opacity": 1,
            stroke: "#222",    "stroke-width": 0.3,
        };
        var attr2 = {    // for the invisible hovering areas
            fill: "green",     "fill-opacity": 0.0,
            stroke: "red",     "stroke-width": 0,
        };
        var things = {};

        /* Square */    things.square     = [ RM.path("m 154.21525,71.431259 74.32805,0 0,70.496711 -74.32805,0 0,-70.496711 z").attr(attr),
                                              RM.path("m 271.25132,77.933263 58.07304,0 0,56.409037 -58.07304,0 0,-56.409037 z").attr(attr2)   ];
        /* Triangle */  things.triangle   = [ RM.path("m 154.02932,222.44063 36.78089,-58.23641 34.48208,58.2364 -71.26297,1e-5").attr(attr),
                                              RM.path("m 271.25132,165.71032 58.07304,0 0,56.40903 -58.07304,0 0,-56.40903 z").attr(attr2)   ];
        for (var shape in things) {
            shape[0].color = Raphael.getColor();

shape[1].onmouseover = function () {
 shape[0].animate({fill:shape[0].color, stroke:"#ccc"}, 500);
 document.getElementById(shape)[0].style.display = "block";
 shape[0].toFront();   R.safari();
};
shape[1].onmouseout = function () {
 shape[0].animate({fill:"#bbb", stroke:"#222"}, 500);
 document.getElementById(shape)[0].style.display = "none";
 shape[0].toFront();   R.safari();
};
        } // end for every member of things
    }); // end the document ready function
</script>

The example on the Raphael website uses more complicated javascript:
http://raphaeljs.com/australia.html

I didn't use this code because I don't understand the function syntax they use:

(function (a, b) {
    ----
})(c, d);

But I still don't see what's wrong with my version.

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

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

发布评论

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

评论(1

温馨耳语 2024-10-02 08:56:17
for (var shape in things) { ...

这不像 foreach,而是更像 Perls:

foreach my $shape (keys $things) { ...

所以你的代码应该是:

for (var shape in things) {
    things[shape][0].color = Raphael.getColor();
    ...

附加答案

至于函数语法,它相当简单,在现代 js 代码中很常见,所以你应该熟悉它。让我们逐步构建该函数。

我们知道:

function foo (x,y) {};

声明一个函数,但是在javascript中函数可以是匿名的:

function (x,y) {};

是一个没有名字的函数。现在,在声明中,这是相当无用的,但如果我们在表达式中执行此操作,它会生成对该函数的引用。那么我们如何将其变成一个表达式呢?只需在其左侧添加一个 = 符号:

var foo = function (x,y) {}

是一个函数表达式,将函数分配给变量 foo。

除了 = 符号右侧之外,还有其他地方代码被视为表达式。其中之一是代码发生在大括号内。它实际上来自数学:

var x = a * (b + c);

= 符号右侧的所有内容都是一个表达式,但 (b+c) 很特殊,因为它是一个子表达式首先评价。 () 语法使 b+c 本身成为一个表达式。类似地:

(function (x,y) {})

强制最初看起来像函数声明的内容成为函数表达式。该规则来自前面的数学表达式。

正如我之前提到的,函数表达式的计算结果是函数引用。您可以使用函数引用做的事情之一是使用它来调用函数:

var foo = function (x,y) {};
foo();

这里,foo 之后的 () 意味着不同的东西:它用于进行函数调用。由于我们可以使用函数引用进行函数调用,并且由于函数表达式的计算结果为函数引用,因此我们可以调用该函数而不将其分配给任何变量:

(function (x,y) {})();  // think of it as foo() where foo=(function(x,y){})
for (var shape in things) { ...

This does not behave like foreach, instead it behaves more like Perls:

foreach my $shape (keys $things) { ...

So your code should be:

for (var shape in things) {
    things[shape][0].color = Raphael.getColor();
    ...

Additional answer

As for the function syntax, it's rather simple and is very common in modern js code so you should familiarize yourself with it. Let's construct that function step by step.

We know:

function foo (x,y) {};

declares a function, but in javascript functions can be anonymous:

function (x,y) {};

is a function with no name. Now, in a declaration, this is rather useless but if we do this in an expression it yields a reference to that function. So how do we make it an expression? just put an = sign to the left of it:

var foo = function (x,y) {}

is a function expression assigning the function to variable foo.

Apart from being to the right of the = sign, there are other places where code is considered expressions. One of them is when code happens inside braces. It comes from math really:

var x = a * (b + c);

everything to the right hand side of the = sign is an expression but (b+c) is special because it is a sub-expression that is evaluated first. It's the () syntax that makes b+c an expression in their own right. Similarly:

(function (x,y) {})

forces what initially looks like a function declaration to become a function expression. The rule comes from the math expression earlier.

As I mentioned earlier, a function expression evaluates to a function reference. And one of the things you can do with a function reference is to use it to call the function:

var foo = function (x,y) {};
foo();

here, the () after foo means something different: it is used to make a function call. Since we can make a function call using a function reference and since a function expression evaluates to a function reference we can call the function without assigning it to any variable:

(function (x,y) {})();  // think of it as foo() where foo=(function(x,y){})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文