断开 Raphael 元素事件
我将 onmouseout 事件附加到 Raphael 圆形元素,如下所示:
(function(el,iElPos,col){
el.mouseout(function elmouseout(){el.animate({"fill":col,"r":ELEMENT_RADIUS},150);
alert("first");
fadeTag();
});
)(c,i,elementColour);
c 是元素。然后,我稍后希望断开该事件的连接并附加另一个事件:
(function (el){
el.attr("fill",EXCLUDED_COLOUR);
el.unmouseout(elmouseout);
el.mouseout(function elmouseout(){
alert("second");
el.animate({"fill":EXCLUDED_COLOUR,"r":ELEMENT_RADIUS},150);
fadeTag();
});
})(setMainSeries[iPos]);
但这会附加两个事件。两个警报都会触发,后附加的事件首先触发。我想完全断开第一个事件。我是否错误地使用了 unmouseout() ?
更新
我尝试了 echo-flow 和 lincolnk 的建议,但都不起作用。我认为回声流可能更有效。我像这样分解了建议的函数...
function elmouseoutDefault(el){
el.animate({"fill":ELEMENT_COLOUR,"r":ELEMENT_RADIUS},150);
alert("first");
fadeTag();
};
然后在创建每个元素时附加了这样的事件...
el.mouseout(elmouseoutDefault);
但是这失败了,因为我没有传递 el
。所以我将其更改为...
el.mouseout(elmouseoutDefault(el));
这似乎在添加时调用 elmouseoutDefault
。我只希望它在鼠标移出事件时触发。
I attach an onmouseout event to a Raphael circle element like this:
(function(el,iElPos,col){
el.mouseout(function elmouseout(){el.animate({"fill":col,"r":ELEMENT_RADIUS},150);
alert("first");
fadeTag();
});
)(c,i,elementColour);
c is the element. I then later wish to disconnect the event and attach another thuswise:
(function (el){
el.attr("fill",EXCLUDED_COLOUR);
el.unmouseout(elmouseout);
el.mouseout(function elmouseout(){
alert("second");
el.animate({"fill":EXCLUDED_COLOUR,"r":ELEMENT_RADIUS},150);
fadeTag();
});
})(setMainSeries[iPos]);
But this attaches both events. Both alerts are firing, the later-attached event fires first. I want to disconnect the first event totally. Am I using unmouseout() incorrectly?
UPDATE
I tried suggestions from echo-flow and lincolnk but neither worked. I think echo-flow's may be more likely to work. I broke out the function as suggested like so...
function elmouseoutDefault(el){
el.animate({"fill":ELEMENT_COLOUR,"r":ELEMENT_RADIUS},150);
alert("first");
fadeTag();
};
then attached the event like this as I created each element...
el.mouseout(elmouseoutDefault);
However this failed as I am not passing el
. So I changed it to...
el.mouseout(elmouseoutDefault(el));
This seems to call elmouseoutDefault
as it is being added. I only want it to trigger on the mouseout event.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这看起来像是在哪里声明函数名称的问题 - 我认为当您使用名称内联定义函数时,事情会变得很棘手。这应该使函数更可靠地引用。编辑:这是一个范围的事情 - 您对
elmouseout
的定义仅存在于自调用函数的范围内。我正在考虑有关函数名称的另一个问题。不过代码应该仍然可以工作。it looks like an issue of where you're declaring the function name- I think things can get tricky when you define the function inline with a name. this should keep a function to the reference more reliably.edit: it's a scope thing- your definition of
elmouseout
exists only within the scope of the self-invoking function. i was thinking of a different issue concerning function names. the code should still work though.我想根据您的工作代码验证这一点,但根据您发布的代码,我认为正在发生的情况如下:在两个代码片段中,您都声明了一个函数“elmouseout”。您使用的函数声明语法在局部作用域中声明函数(相当于编写“var elmouseout = function(){...}”),并且因为您在单独的函数闭包内部声明每个变量,所以变量“第二个代码块中的“elmouseout”指的是第二个代码块中的函数“elmouseout”,而不是第一个代码块,这将产生所需的行为。我建议分解函数声明,如下所示:
I'd like to verify this against your working code, but based on the code you posted, here's what I think is happening: in both code snippets, you are declaring a function "elmouseout". The function declaration syntax you are using declares the function in the local scope (the equivalent of writing "var elmouseout = function(){...}"), and because you are declaring each inside of a separate function closure, the variable "elmouseout" in the second code block refers to function "elmouseout" from the second code block, as opposed to the first code block, which would yield the desired behaviour. I would recommend breaking out the function declarations, as follows: