我刚刚发现拉斐尔,并且喜欢它,但我不是一个javascript-er。现在我有三个重复的代码段在三个不同的div中绘制相同的圆圈。在Raphael中制作画布的默认设置通过ID找到一个元素,但是我希望有一组变量可以在所有div中使用“circle”类绘制圆圈。我认为必须有一种更有效的编码方式。这是我现在使用的代码:
window.onload = function () {
var paper = Raphael("c1", 26, 26); /* Make canvas 26*26px in div id "c1" */
var circle = paper.circle(13, 13, 10.5); /* Draw circle at the center of the canvas with radius 10.5 */
circle.attr("stroke", "#f1f1f1");
circle.attr("stroke-width", 2);
var text = paper.text(13, 13, "1"); /* Print text "1" inside the circle */
text.attr({'font-size': 15, 'font-family': 'FranklinGothicFSCondensed-1, FranklinGothicFSCondensed-2'});
text.attr("fill", "#f1f1f1");
var paper2 = Raphael("c2", 26, 26);
var circle2 = paper2.circle(13, 13, 10.5);
circle2.attr("stroke", "#f1f1f1");
circle2.attr("stroke-width", 2);
var text2 = paper2.text(12, 13, "2");
text2.attr({'font-size': 15, 'font-family': 'FranklinGothicFSCondensed-1, FranklinGothicFSCondensed-2'});
text2.attr("fill", "#f1f1f1");
var paper3 = Raphael("c3", 26, 26);
var circle3 = paper3.circle(13, 13, 10.5);
circle3.attr("stroke", "#f1f1f1");
circle3.attr("stroke-width", 2);
var text3 = paper3.text(12, 13, "3");
text3.attr({'font-size': 15, 'font-family': 'FranklinGothicFSCondensed-1, FranklinGothicFSCondensed-2'});
text3.attr("fill", "#f1f1f1");
};
测试网站是@ http://jesserosenfield.com/fluid/test.html
非常感谢你的帮助!
答案 0 :(得分:8)
定义一个函数,该函数接受div的参数,以便您可以自动化该过程:
function drawcircle(div, text) {
var paper3 = Raphael(div, 26, 26); //<<
var circle3 = paper3.circle(13, 13, 10.5);
circle3.attr("stroke", "#f1f1f1");
circle3.attr("stroke-width", 2);
var text3 = paper3.text(12, 13, text); //<<
text3.attr({'font-size': 15, 'font-family': 'FranklinGothicFSCondensed-1, FranklinGothicFSCondensed-2'});
text3.attr("fill", "#f1f1f1");
}
然后在你的window.onload:
window.onload = function () {
drawcircle("c1", "1");
drawcircle("c2", "2");
drawcircle("c3", "3");
};