【发布时间】:2020-01-19 21:58:45
【问题描述】:
我目前正在为瓷砖地图制作一个正方形网格。我已经设置好了,所以单击图块会将其状态从未探索更改为已探索。我正在尝试使用它,以便用鼠标向下拖动会改变所有底层图块的状态,但是我似乎无法让它工作。
我尝试使用 mousedown 和 mouseup 事件来设置一个向下的布尔值,然后在鼠标悬停时检查该值。我尝试过以多种方式解决这个问题(即注释掉的代码)。当前代码可用于单击,但我真的希望能够进行拖动以更改倍数功能。
var tableString;
var width = 35;
var height = 15;
var cells = [];
var localX;
var localY;
function cell(x, y, c) {
positionX = x;
positionY = y;
category = c;
}
function createMap() {
for (var i = 0; i < height; i++) {
var row = [];
for (var j = 0; j < width; j++) {
let c = new cell();
c.category = "unexplored";
c.positionX = j;
c.positionY = i;
row.push(c);
}
cells.push(row);
}
}
function drawMap() {
tableString = "<table draggable='false'>";
for (var i = 0; i < height; i++) {
tableString += "<tr draggable='false'>";
for (var j = 0; j < width; j++) {
tableString += '<td draggable="false" class="' + cells[i][j].category + '" data-row="' + j + '" data-column="' + i + '"></td>';
}
tableString += "</tr>";
}
tableString += "</table>";
$("#mainContainer").html(tableString);
console.log("drew it");
}
function updateCellCategory(x, y, c) {
cells[x][y].category = c;
drawMap();
}
$(document).ready(function() {
createMap();
drawMap();
// var down = false;
// $(document,"td").mousedown(function () {
// down = true;
// })
// $(document,"td").mouseup(function () {
// down = false;
// });
// $(document,"td").on('mouseover','td',function () {
// if (down) {
// console.log("hovering and holding");
// localX = $(this).attr("data-row");
// localY = $(this).attr("data-column");
// updateCellCategory(localY, localX, "explored");
// }
// });
});
// $(document).on('mousedown',"td, documen",(function () {
// down = true;
// console.log(down);
// }));
// $(document).on('mouseup',"*",(function () {
// down = false;
// console.log(down);
// }));
// $(document).on('mouseover','td',function () {
// if (down) {
// console.log("hovering and holding");
// localX = $(this).attr("data-row");
// localY = $(this).attr("data-column");
// updateCellCategory(localY, localX, "explored");
// }
// });
$("*").delegate('td', 'click', function() {
localX = $(this).attr("data-row");
localY = $(this).attr("data-column");
updateCellCategory(localY, localX, "explored");
});
html,
body {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px;
}
#mainContainer {
max-width: 100%;
max-height: 90%;
width: 100%;
height: 90%;
display: flex;
align-items: center;
justify-content: center;
}
td {
width: 25px;
height: 25px;
border: .05px solid black;
}
.explored {
background-color: lightblue;
}
.unexplored {
background-color: lightcoral;
}
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<div id="mainContainer">
</div>
</body>
</html>
我在处理这个问题时发现的主要问题是,一些注释代码有时会起作用,但是第二次在 td 上发生拖动事件时,代码会中断,并且无法识别 mouseup,导致鼠标光标甚至会继续影响磁贴虽然没有按住鼠标。
【问题讨论】:
-
delegate已弃用。你最好更新你的代码。此外,如果您发布一些 HTML,我们也许可以将上述内容用于工作演示。 -
我刚刚把它做成了一个codepen codepen.io/JoelSchmidt/pen/VwZEGON
-
这里不需要使用codepen。事实上,如果您插入代码 sn-p,您可能会获得更多帮助。我已经改变了你的问题。
-
我很感激!
-
看我的回答。请向其中添加任何 cmets。这接近你想要的吗?
标签: javascript jquery html draggable drag