Raphael JS:如何删除事件?

时间:2011-06-11 10:50:47

标签: events mouseover raphael

我使用Raphael .mouseover()和.mouseout()事件来突出显示SVG中的一些元素。 这工作正常,但在我点击一个元素后,我希望它停止突出显示。

Raphael documentation我找到了:

  

取消绑定事件使用带有“un”前缀的相同方法名称,即element.unclick(f);

但我不能让它工作,我也不理解'f'参数。

这不起作用,但是什么呢?

obj.click( function() {
  this.unmouseover();
});

1 个答案:

答案 0 :(得分:5)

好的,您需要做的是将处理程序函数传递给unmouseover请求:

// Creates canvas 320 × 200 at 10, 50
var paper = Raphael(10, 50, 320, 200);
// Creates circle at x = 50, y = 40, with radius 10
var circle = paper.circle(50, 40, 10);
// Sets the fill attribute of the circle to red (#f00)
circle.attr("fill", "#f00");
// Sets the stroke attribute of the circle to white
circle.attr("stroke", "#fff");

var mouseover = function (event) {
    this.attr({fill: "yellow"});
}
var mouseout = function (event) {
    this.attr({fill: "red"});
}

circle.hover(mouseover, mouseout);
circle.click(function (event) {
    this.attr({fill: "blue"});
    this.unmouseover(mouseover);
    this.unmouseout(mouseout);
});

http://jsfiddle.net/GexHj/1/

这就是f的含义。您也可以使用unhover()

circle.click(function (event) {
    this.attr({fill: "blue"});
    this.unhover(mouseover, mouseout);
});

http://jsfiddle.net/GexHj/2/