拖放产生不良结果
Drag and Drop producing undesirable results
我的程序中遇到了一个我似乎无法弄清楚的错误。基本上,当我的游戏开始时,我有一个循环遍历所有将被拖放的部分。但是,当我实际测试它时,会定期(并且该错误似乎取决于我首先尝试拖动哪个元素,然后影响之后的元素)我会去拖动一个项目,当我尝试放下它时,如果我转到 table 的左上角元素并从左向右拖动每个元素,则不会发生 drop 事件,最终所有部分都可以拖放,但只能在一个具体顺序。我一直在努力解决这个问题,但 运行 没有想法。这是问题的现场演示,它开始于我 select 第一个五边形,这不会发生在每场比赛中...https://www.youtube.com/watch?v=-izy_tuxmdg
我正在使用的循环是这样的,它是我在程序启动时调用的对象的一部分:
enablePieces: function() {
for(var i = 0; i < this.dragPieces.length; i++) {
this.dragPieces[i].ondragstart = this.dragStart;
this.targetPieces[i].ondragover = this.allowDrop;
this.targetPieces[i].ondrop = this.doDrop;
};
},
当我调用方法时,它是这样的:
Perfection.start = function() {
newGame();
*Perfection.view.enablePieces();*
Perfection.controls.startButton.onclick = beginGame;
Perfection.controls.newButton.onclick = newGame;
};
我还应该提到,当我从 DOM 中获取元素时,它是这样的:
dragPieces : document.querySelectorAll(".piecetray td img"),
targetPieces : document.querySelectorAll(".gametray td img"),
当我在控制台中输入 Perfection.view.dragPieces 时,我得到:
Perfection.view.dragPieces
NodeList
[ <img>, <img>, <img>, <img>, <img>, <img>, <img>, <img>, <img>, <img>, 26 more… ]
但是,如果我创建一个数组 var dragArray = [ ];
,然后将 "NodeList" 中的所有元素推入该数组,然后将该数组输入控制台,我得到:
dragArray
Array [ <img>, <img>, <img>, <img>, <img>, <img>, <img>, <img>, <img>, <img>, 26 more… ]
我觉得这两者之间没有什么区别,但现在我在质疑自己,似乎在使用我创建的数组时它发生得更少,但这可能只是缺乏测试一个比其他,谁能告诉我为什么会这样?
拖放功能也贴在这里:
dragStart : function(event) {
var piece = event.currentTarget;
piece.classList.add("selected");
},
allowDrop : function(event) {
event.preventDefault();
},
doDrop : function(event) {
var bestTime = 0;
event.preventDefault();
var currentPiece = document.querySelector(".selected");
var targetPiece = event.currentTarget;
var targetParent = targetPiece.parentElement;
if (getName(currentPiece) === getName(targetPiece)) {
targetParent.appendChild(currentPiece);
currentPiece.classList.remove("selected");
currentPiece.classList.add("matched");
matchedCount++;
};
if ((matchedCount === 36) && (Perfection.timeLeft !== 0))
if(Perfection.timeLeft > bestTime) {
Perfection.view.updateScore("Best time " + timer.innerHTML);
bestTime = Perfection.timeLeft;
clearInterval(Perfection.timer);
};
问题是行:
currentPiece.classList.remove("selected");
因为它被放置在条件中,class 只有在匹配时才被删除,但无论如何,在每次放置事件中它都应该被删除。
我的程序中遇到了一个我似乎无法弄清楚的错误。基本上,当我的游戏开始时,我有一个循环遍历所有将被拖放的部分。但是,当我实际测试它时,会定期(并且该错误似乎取决于我首先尝试拖动哪个元素,然后影响之后的元素)我会去拖动一个项目,当我尝试放下它时,如果我转到 table 的左上角元素并从左向右拖动每个元素,则不会发生 drop 事件,最终所有部分都可以拖放,但只能在一个具体顺序。我一直在努力解决这个问题,但 运行 没有想法。这是问题的现场演示,它开始于我 select 第一个五边形,这不会发生在每场比赛中...https://www.youtube.com/watch?v=-izy_tuxmdg
我正在使用的循环是这样的,它是我在程序启动时调用的对象的一部分:
enablePieces: function() {
for(var i = 0; i < this.dragPieces.length; i++) {
this.dragPieces[i].ondragstart = this.dragStart;
this.targetPieces[i].ondragover = this.allowDrop;
this.targetPieces[i].ondrop = this.doDrop;
};
},
当我调用方法时,它是这样的:
Perfection.start = function() {
newGame();
*Perfection.view.enablePieces();*
Perfection.controls.startButton.onclick = beginGame;
Perfection.controls.newButton.onclick = newGame;
};
我还应该提到,当我从 DOM 中获取元素时,它是这样的:
dragPieces : document.querySelectorAll(".piecetray td img"),
targetPieces : document.querySelectorAll(".gametray td img"),
当我在控制台中输入 Perfection.view.dragPieces 时,我得到:
Perfection.view.dragPieces
NodeList
[ <img>, <img>, <img>, <img>, <img>, <img>, <img>, <img>, <img>, <img>, 26 more… ]
但是,如果我创建一个数组 var dragArray = [ ];
,然后将 "NodeList" 中的所有元素推入该数组,然后将该数组输入控制台,我得到:
dragArray
Array [ <img>, <img>, <img>, <img>, <img>, <img>, <img>, <img>, <img>, <img>, 26 more… ]
我觉得这两者之间没有什么区别,但现在我在质疑自己,似乎在使用我创建的数组时它发生得更少,但这可能只是缺乏测试一个比其他,谁能告诉我为什么会这样?
拖放功能也贴在这里:
dragStart : function(event) {
var piece = event.currentTarget;
piece.classList.add("selected");
},
allowDrop : function(event) {
event.preventDefault();
},
doDrop : function(event) {
var bestTime = 0;
event.preventDefault();
var currentPiece = document.querySelector(".selected");
var targetPiece = event.currentTarget;
var targetParent = targetPiece.parentElement;
if (getName(currentPiece) === getName(targetPiece)) {
targetParent.appendChild(currentPiece);
currentPiece.classList.remove("selected");
currentPiece.classList.add("matched");
matchedCount++;
};
if ((matchedCount === 36) && (Perfection.timeLeft !== 0))
if(Perfection.timeLeft > bestTime) {
Perfection.view.updateScore("Best time " + timer.innerHTML);
bestTime = Perfection.timeLeft;
clearInterval(Perfection.timer);
};
问题是行:
currentPiece.classList.remove("selected");
因为它被放置在条件中,class 只有在匹配时才被删除,但无论如何,在每次放置事件中它都应该被删除。