I am trying to implement drag and drop functionality using javscript and following code is working fine in my local but, it is not working in the server. Debug information is given below:
$(document).on("mousedown touchstart", "#canvas", function (evt) {
//ClearCanvas();
var canvasSide = isMobile ? 320 : 600;
if ($("img").is(":animated") === false) {
var iconSize = canvasSide / board.boardSize;
var xCoord, yCoord;
if (evt.type === "mousedown") {
xCoord = evt.offsetX;
yCoord = evt.offsetY;
} else {console.log(evt.type);console.log(evt);
xCoord =
parseInt(evt.touches[0].clientX) -
(parseInt(evt.target.offsetLeft) +
parseInt(evt.target.offsetParent.offsetLeft));
yCoord =
parseInt(evt.touches[0].clientY) -
(parseInt(evt.target.offsetTop) +
parseInt(evt.target.offsetParent.offsetTop));
}
var col = Math.floor(xCoord / iconSize);
var row = Math.floor(yCoord / iconSize);
var img = document
.querySelectorAll("[data-position='" + col + "-" + row + "']")
.item(0);
if (img != null) {
$(img).css("z-index", 2);
var top = parseInt($(img).css("top"));
var left = parseInt($(img).css("left"));
dragDropInfo = {
candyImg: img,
initCol: col,
initRow: row,
initTop: top,
initLeft: left,
initXCoord: xCoord,
initYCoord: yCoord,
};
}
}
});
Error is throwing at ‘evt.touches[0]’. Because evt does’t have touches information in server.
2
Answers
Try this bellow:
You can grab the
originalEvent
from your screenshot. If all you care is to grab the first touch point regardless, blast it with chaining:do the above for both Y and X. For instance your xCoord would be: