实际上提供一个 3 层对象很好,因为您可以根据自己的喜好为背面层着色。前层与后层的形状相同,但不透明度为零,因此您可以看到中间层上的文本。
我也不喜欢使用节点来处理事件,而是使用本机可用的 Raphael 事件处理机制。
下面的示例取自我网站上的示例之一,我已将其进一步删减,以便更容易理解。
另请注意,它是一个由前层和后层圆圈组成的按钮,两层之间夹有一个文本。它们可以很容易地成为矩形/圆角矩形。
该功能包括在鼠标悬停/鼠标悬停时反转颜色...
button = function (paper, xpos, ypos, r, labeltext)
{
this.backLayer = paper.circle(xpos, ypos, r).attr({ fill: "#FFFF00", 'fill-opacity': 0 , stroke: "#FF0000",'stroke-width':5, 'stroke-opacity':0});
/*The text automatically centres itself as this is the default text positioning for Raphael text*/
this.label = paper.text(xpos, ypos, labeltext).attr({fill:'#ff0000', 'font-size':18});
/*Now we make a copy for the front layer and we also make the back layer opaque. So that you can see it's yellow fill*/
this.frontLayer = this.backLayer.clone();
this.backLayer.attr({'fill-opacity': 1, 'stroke-opacity':1});
/*Now make the back layer and the text referencable for the mouseover, mouseout and click event of the front layer*/
this.frontLayer.backLayer= this.backLayer;
this.frontLayer.label = this.label;
/*Add a preferred cursor by referencing the underlying DOM object of the frontLayer*/
this.frontLayer.node.style.cursor = 'pointer';
/*Now you can interact with the lower layers behind the invisible covering layer ( frontLayer ) in it's event methods*/
this.frontLayer.mouseover(
function (e) {
this.backLayer.animate({ fill: "#FF0000", stroke: "#FFFF00" }, 1000);
this.label.animate({ fill: "#FFFF00" }, 1000);
}
);
this.frontLayer.mouseout(
function (e) {
this.backLayer.animate({ fill: "#FFFF00", stroke: "#FF0000" }, 1000);
this.label.animate({ fill: "#FF0000" }, 1000);
}
);
this.frontLayer.click(
function (e) {
alert("Creating loads of custom buttons is easy\n\nYou made this one with the following specification:\n"+
"Button Text : "+this.label.attr("text")+"\n"+
"Button Centre @ X: "+this.backLayer.attr("cx")+"\n"+
"Button Centre @ Y: "+this.backLayer.attr("cy")+"\n"+
"Button Radius : "+this.backLayer.attr("r"));
}
);
}
/*Create the button*/
rappyButton = new button(paper, 250, 200, 75, "Raphael");
希望有所帮助。