我正在使用鼠标事件来绘制自由线(使用Raphael),它可以很好地处理鼠标事件,但不能处理触摸事件。如果是触摸事件,是否需要将 event.pageX 替换为 event.screenX 或 event.clientX?
var board = $("#board");
board.bind("mousedown", _mousedownHandler);
board.bind("touchstart", _mousedownHandler);
function _drawFreeLineBegin(x, y) {
board.lineEl.path = _drawOptions.paper.path("M"
+ (x - _drawOptions.offset.left) + ","
+ (y - _drawOptions.offset.top));
_setNewElementProperties(board.lineEl.path,
Configuration.getProperties("freeLine"));
board.bind("mousemove.mmu", _mousemoveHandler);
board.one("mouseup.mmu", _mouseupHandler);
board.bind("touchmove.mmu", _mousemoveHandler);
board.one("touchend.mmu", _mouseupHandler);
}
function _mousedownHandler(event) {
if(event.type == "touchstart"){
event = event.touches[0];
}
_drawFreeLineBegin(event.pageX, event.pageY);
return false;
}
function _mousemoveHandler(event) {
if(event.type == "touchmove"){
event = event.touches[0];
}
board.lineEl.path
.attr(
"path",
board.lineEl.path.attr("path")
+ "L"
+ (event.pageX - )
+ ","
+ (event.pageY ));
return true;
}
function _mouseupHandler(event) {
board.unbind(".mmu");
board.lineEl.path = null;
event.stopPropagation();
event.preventDefault();
}
最佳答案
解决了这个问题,我需要替换 event.touches[0];与 event.originalEvent.touches[0];
关于javascript - 尝试处理触摸事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17713055/