更新的答案(以下原文)
根据评论中的对话,您还可以为框分配 id,然后在鼠标移出时测试鼠标下的新对象是否是红色矩形,如下所示:
var Paper = new Raphael(0, 0, 400, 300);
var mat = Paper.rect(0, 0, 400, 300).attr({fill: "#CCC"});
mat.node.id = "mat";
// Hover in function
function hoverIn() {
newBox.show();
this.text.toFront();
this.animate({
height: 140,
width: 140,
x: 130,
y: 80
}, 100);
}
// Hover out function
function hoverOut(e) {
console.log(e, e.toElement);
if (e.toElement.id !== "redbox") {
newBox.hide();
this.animate({
height: 100,
width: 100,
x: 150,
y: 100
}, 100);
}
}
var rect = Paper.rect(150, 100, 100, 100).attr({fill: 'blue' });
rect.node.id = "bluebox";
var newBox = Paper.rect(140, 90, 120, 120).attr({fill: 'red'}).hide();
newBox.node.id = "redbox";
rect.attr({r:5});
rect.text = Paper.text(200, 150, 'Hover me!').attr('font-size', 12);
rect.hover(hoverIn, hoverOut);
jsFiddle
原始答案
我总是推荐一个不同的解决方案:而不是在一组复杂的情况下触发 hoverOut,只需在鼠标进入框外区域时触发它。
见:如何防止损失悬停在拉斐尔?
var Paper = new Raphael(0, 0, 400, 300);
//this should be colored white -- leaving as gray for clarity
var mat = Paper.rect(0, 0, 400, 300).attr({fill: "#CCC"});
// Hover in function
function hoverIn() {
console.log(newBox);
newBox.show();
rect.text.toFront();
rect.animate({
height: 140,
width: 140,
x: 130,
y: 80
}, 100);
}
// Hover out function
//notice "this" was replaced with "rect" since "this" now refers to mat
function hoverOut() {
newBox.hide();
rect.animate({
height: 100,
width: 100,
x: 150,
y: 100
}, 100);
}
var rect = Paper.rect(150, 100, 100, 100).attr('fill', 'blue');
var newBox = Paper.rect(140, 90, 120, 120).attr({fill: 'red'}).hide();
rect.attr({r:5});
rect.text = Paper.text(200, 150, 'Hover me!').attr('font-size', 12);
rect.mouseover(hoverIn);
mat.mouseover(hoverOut);
jsFiddle