为什么触摸事件不在手指按压的中间
Why is touchevent not in the middle of the finger press?
>我正在监听触摸事件,当我用layerX
/layerY
调整pageX
/pageY
值时,该点显示为我的触摸(我的手指)圆圈的顶部,而不是中间。
//pageX = 310, layerX = -9. This is exactly in the middle of the touch circle horizontally
touchEvent.changedTouches[0].pageX + touchEvent.layerX
//pageY = 90, layerY = -34. This is exactly at the top of the touch circle!
touchEvent.changedTouches[0].pageX + touchEvent.layerX
当我检查触摸圆的半径时,我看到的只是一个半径 1:webkitRadiusX: 1
, webkitRadiusY: 1
(来自console.dir( touchEvent);
)。
我有三个问题:
为什么触摸屏幕(Windows 8/Chrome)时半径为"1",显示一个半径较大的圆圈,而"x"位于中间?
为什么我需要组合图层和页面坐标才能获得与仅使用mouseEVent的图层坐标获得的值相同的值?
在触摸事件与鼠标事件上获取元素内点的正确方法是什么?
代码
索引.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Touch Test</title>
<script src="touch.js" type="text/javascript"></script>
</head>
<body>
<div id="touchBox" class="touchBox" style="position: relative; top: 19px; left: 0px; width: 638px; height: 142px; border: solid 1px #000000;"></div>
</body>
</html>
触摸.js
window.Touch = {
init: function()
{
Touch.dom = document.getElementById( "touchBox" );
//Start listening
Touch.dom.addEventListener( "touchstart", Touch.mouseDown, false );
Touch.dom.addEventListener( "mousedown", Touch.mouseDown, false );
},
getPoint: function(touchEvent)
{
var x, y;
//Using touch events
if ( touchEvent.changedTouches && touchEvent.changedTouches[ 0 ] )
{
//Get the offset of the dom object
var offsety = Touch.dom.offsetTop || 0;
var offsetx = Touch.dom.offsetLeft || 0;
//The points within the dom object
x = touchEvent.changedTouches[0].pageX - offsetx + touchEvent.layerX;
y = touchEvent.changedTouches[0].pageY - offsety + touchEvent.layerY;
}
//Get points relative to the layer
else if ( touchEvent.layerX || 0 == touchEvent.layerX )
{
x = touchEvent.layerX;
y = touchEvent.layerY;
}
//Get the points relative to the dom object
else if ( touchEvent.offsetX || 0 == touchEvent.offsetX )
{
x = touchEvent.offsetX;
y = touchEvent.offsetY;
}
return { x: x, y: y };
},
mouseDown: function(mouseEvent)
{
//Make sure it doesn't move the page
mouseEvent.preventDefault();
mouseEvent.stopPropagation();
//Get the point
var point = Touch.getPoint( mouseEvent );
//Draw the point
var pointDiv = document.createElement( "div" );
pointDiv.style.width = "3px";
pointDiv.style.height = "3px";
pointDiv.style.backgroundColor = "#000000";
pointDiv.style.left = point.x + "px";
pointDiv.style.top = point.y + "px";
pointDiv.style.position = "absolute";
Touch.dom.appendChild( pointDiv );
//Print the point
console.dir( point );
}
};
window.addEventListener( "load", Touch.init );
问题是偏移量。不需要。偏移量似乎已合并到layerX/Y
中。因此,将触摸事件的代码更改为:
//Using touch events
if ( touchEvent.changedTouches && touchEvent.changedTouches[ 0 ] )
{
//The points within the dom object
x = touchEvent.changedTouches[0].pageX + touchEvent.layerX;
y = touchEvent.changedTouches[0].pageY + touchEvent.layerY;
}
相关文章:
- 为什么grunt contrib connect的中间件选项的第三个参数是未定义的
- 什么'在Express中路由时,应用程序级中间件和路由器级中间件之间的区别是什么
- Redux中间件在next()之前更改状态
- 遗漏中间部分的单页点导航
- 用两个手指旋转谷歌地图javascript中的地图
- 在屏幕中间打开一个弹出窗口
- Regex加在中间
- 快速的套接字 io 实例/中间件
- JS/CSS旋转的DIVS在任何屏幕的中间相遇
- javascript可拖动滑块,可在mouseUp上捕捉回中间位置
- Javascript和Canvas绘制中间有孔的圆
- 如何使用Express'正确加载图像;s的静态中间件
- 将滚动条移动到页面中间的控制位置
- 如何在快递中制作中间件的动态链.js
- Highstock,错误:未捕获的类型错误:w[(中间值)(中间值)(中间值)]不是构造函数
- 在 Redux 中,在商店中应用中间件未正确称为化简器
- 如何使用JS在中间输入文本中动态插入字符
- 如何在HTML / CSS中填充垂直和滚动中间窗格(Chrome可以工作,但不能Firefox)
- 引导弹出框在中间对齐
- 为什么触摸事件不在手指按压的中间