如何在Javascript/jQuery中获得触摸坐标?



下面是我使用的代码

$(document).on("mousemove touchmove",function(e){
var currentY = e.originalEvent.touches ?  e.originalEvent.touches[0].clientY : e.clientY;
var currentX = e.originalEvent.touches ?  e.originalEvent.touches[0].clientX : e.clientX;
//do stuff with the coordinates
});

它工作得很好,但是在移动设备上有一些延迟(在Android上测试)。

延迟的原因是导航栏,在移动浏览器的顶部(例如Android上的Chrome)。

如果用户向下滚动,导航栏将从视图中消失,触摸事件将完美地工作。

下面的代码禁用页面滚动,触摸事件工作完美。但是,我不想阻止用户滚动(因为它是必需的)

document.addEventListener('touchmove', function(e) {
e.preventDefault();
}, { passive: false }); 

当导航栏存在时,我如何防止延迟?

听起来触摸事件被地址栏隐藏的动作捕获,我不认为有办法阻止它,除了强制导航栏始终保持可见。你可以用Submachine23给出的答案中的css来完成这个任务,我将把它复制到这里,以供子孙后代使用。

html {
background-color: red;
overflow: hidden;
width: 100%;
}
body {
height: 100%;
position: fixed;
/* prevent overscroll bounce*/
background-color: lightgreen;
overflow-y: scroll;
-webkit-overflow-scrolling: touch;
/* iOS velocity scrolling */
width: 50%;
margin-left: 25%;
}

最新更新