我想知道如何在最佳实践/性能方面改进这段 js。
JS代码:
var treeGroupTypes, treeType, leftLeafClass, rightLeafClass;
treeGroupTypes = ["tree-group-small", "tree-group-avg", "tree-group-large", "tree-group-large", "tree-group-avg", "tree-group-small", "tree-group-small", "tree-group-avg", "tree-group-large", "tree-group-large", "tree-group-avg", "tree-group-small"];
treeType = ["small-tree", "avg-tree", "large-tree"];
leftLeafClass = "left-leaf";
rightLeafClass = "right-leaf";
//Both the above arrays have css classes in them, and then the 2 variables as well. Basically the whole js codes builds some trees and appends leaves to them.
buildTrees(treeGroupTypes, treeType, leftLeafClass, rightLeafClass);
buildTrees: function (treeGroupTypes, treeType, leftLeafClass, rightLeafClass) {
for (j = 0; j < treeGroupTypes.length; j++) {
var treeGroup;
treeGroup = $(document.createElement("div"));
treeGroup.addClass(treeGroupTypes[j]).appendTo(".trees")
for (i = 0; i < treeType.length; i++) {
var leftLeaf, rightLeaf, leftIcon, rightIcon;
leftLeaf = $(document.createElement("span"));
rightLeaf = leftLeaf.clone();
leftIcon = $(document.createElement("i"));
rightIcon = leftIcon.clone();
leftLeaf.addClass(leftLeafClass).append(leftIcon);
rightLeaf.addClass(rightLeafClass).append(rightIcon);
var tree = $(document.createElement("div"));
tree.addClass(treeType[i]).appendTo(treeGroup);
leftLeaf.appendTo(tree);
if (treeGroupTypes[j] == "tree-group-large" && treeType[i] == "large-tree") {
for (l = 1; l < 4; l++) {
var more = rightLeaf.clone();
more.css("top", (tree.height() / 4) * l).appendTo(tree)
}
}
else if (treeGroupTypes[j] == "tree-group-avg" && treeType[i] == "large-tree") {
for (l = 1; l < 3; l++) {
var more = rightLeaf.clone();
more.css("top", ((tree.height() / 3) * l) + 10).appendTo(tree)
}
}
else {
rightLeaf.css("top", tree.height() / 3).appendTo(tree)
}
}
}
}
需要 CSS:
有 3 个树组 - avg, large, small ,根据高度,显示在小提琴中。按组,这意味着它共有 3 棵树,每组中的这 3 棵树进一步细分为 large-tree、avg-tree、small-tree
.trees { padding:0 10px;}
.tree-group-avg {margin:0 8px; display:inline-block;}
.tree-group-avg div {position:relative; display:inline-block; margin:0 10px 0 0; background:#83919F; width:2px; vertical-align:bottom;}
.tree-group-avg .large-tree { height:120px; }
.tree-group-avg .avg-tree { height:90px;}
.tree-group-avg .small-tree { height:60px;}
.tree-group-large {margin:0 8px; display:inline-block;}
.tree-group-large div {position:relative; display:inline-block; margin:0 10px 0 0; background:#83919F; width:2px; vertical-align:bottom;}
.tree-group-large .large-tree { height:150px; }
.tree-group-large .avg-tree { height:120px;}
.tree-group-large .small-tree { height:90px;}
.tree-group-small {margin:0 8px; display:inline-block;}
.tree-group-small div {position:relative; display:inline-block; margin:0 10px 0 0; background:#83919F; width:2px; vertical-align:bottom;}
.tree-group-small .large-tree { height:90px; }
.tree-group-small .avg-tree { height:60px;}
.tree-group-small .small-tree { height:30px;}
/下面是附加到树的叶子类,左叶类意味着它将在树的左侧,右叶在右侧/
.left-leaf i{width:10px; height:10px; border-radius:0 10px 0 10px; display:inline-block; background:#ACCF37; position:relative;behavior: url(css/PIE.htc); }
.left-leaf {position:absolute; left:-10px;}
.right-leaf i{width:10px; height:10px; border-radius:10px 0 10px 0; display:inline-block; background:#ACCF37; position:relative; behavior: url(css/PIE.htc);}
.right-leaf {position:absolute;}
HTML:
<section class="trees"></section>
它产生的 jsfiddle 链接:http: //jsfiddle.net/5NrfQ/