更新答案(下原)
每談話評論,你也可以分配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,只需在鼠標進入盒子外部時觸發它。
參見:How to prevent loss hover in Raphael?
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
出現此問題的原因很快的紅色方塊之間移動的藍色方形外發生時,沒有新的'mouseover'事件。爲了執行'mouseout'事件,必須在它之前發生'mouseover'事件,但是光標移動的速度阻止了這種事件。作爲原始答案的作者,這是一個有待克服的有趣問題。我沒有足夠的時間進行調試並查找其他解決方案,但希望能儘快返回修復程序。 – amustill