0
所以我试图编写一个像JavaScript的珠宝类游戏,但不是点击一个宝石,然后点击另一个宝石,让他们切换位置,我希望玩家点击一颗宝石然后拖到另一颗宝石上,让它们转换位置。在代码中,我使用变量orb作为宝石。拖动div元素,然后用另一个元素切换位置
function makeOrb(orb, x, y) {
orb.className = "orb";
var bejeweledarea = document.getElementById("bejeweledarea");
bejeweledarea.appendChild(orb);
var random = Math.round(Math.random() * (colors.length - 1));
orb.style.backgroundColor = colors[random];
orb.style.position = "absolute";
orb.style.top = y + "px";
orb.style.left = x + "px";
orb.onmousedown = activate;
if (activeSwitching) {
orb.onmouseover = switchOrbs(activeOrb, orb);
}
}
function activate() {
activeSwitching = true;
activeOrb = this;
}
function switchOrbs(orb, otherOrb) {
var oldTop = orb.style.top;
var oldLeft = orb.style.left;
orb.style.top = otherOrb.style.top;
orb.style.left = otherOrb.style.left;
otherOrb.style.top = oldTop;
otherOrb.style.left = oldLeft;
}
我可以将activeSwitching注册为true,但由于某种原因,mouseover事件无法正常工作。
是的,你是对的。我想让其他球体拥有onmouseover事件。我如何着手将事件添加到所有球体?你能再详细一点吗? – user2995240
我添加了一个伪大纲。 – Roemer
非常感谢帮助! – user2995240