我正在为一家教育公司创建一个学习模块,在那里我创建了25个动物精灵(带有图像的画布)并将它们放入一个农场(带有背景图像的div).然后我根据它们在背景上的位置对它们的z-index重新排序,以便更接近的精灵将位于更远的顶部(背景DIV和精灵都是位置:绝对;).
这是重新排列精灵的功能:
Array.prototype.sortIndices = function (func) {
var i = j = this.length,
that = this;
while (i--) {
this[i] = { k: i, v: this[i] };
}
this.sort(function (a, b) {
return func ? func.call(that, a.v, b.v) :
a.v < b.v ? -1 : a.v > b.v ? 1 : 0;
});
while (j--) {
this[j] = this[j].k;
}
}
function rearrangeSprites() {
var zSprites = new Array();
for (var i = 0; i < sprites.length; i++) …Run Code Online (Sandbox Code Playgroud)