javascript - How to remove touch offset on webpage?
问题描述
I'm experimenting with some touch integration with my web app and i'm noticing that with the code posted below the block that moves on touch has a costant offset and I can temporarly remove it by going in f11 mode. How do i remove it such that the block is always under the finger?
<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1.0, user-scalable=no">
<head>
<title>A title</title>
</head>
<style>
body {
width: 100vw;
height: 100vh;
position: absolute;
top: -8px;
left: -8px;
overflow: hidden;
}
</style>
<body>
<div id="bup" style="position:absolute; background-color:blue;width:100px;height:100px;"></div>
<script>
var touch = [];
document.body.addEventListener('touchmove', function(event) {
event.preventDefault();
touch = event.touches;
document.getElementById('bup').style.top= (touch[0].screenY-50) + 'px';
document.getElementById('bup').style.left= (touch[0].screenX-50) + 'px';
}, false);
</script>
</body>
</html>
(the -50 is there just to center the block on my finger considering that the block it self is 100x100px)
ty
解决方案
Alright i solved it my self.
the issue was the touch[0].screenX
. I shoud have used touch[0].clientX
, same for the Y.
Hope this helps someone, see ya
推荐阅读
- java - 在 Spring AOP 中是否可以有多个具有不同参数的切入点?
- sql - 使用 MS Access 更新表:操作必须使用可更新查询
- postgresql - 如何从远程 pc 连接到本地 postgreSQL 数据库
- swift - Swift performSegue 未执行
- javascript - 在单独的 .js 文件中向数组添加值
- jax-rs - IBM JAX-RS 2.0 客户端和 baseClients
- python - 使用条件计算数据框中列的平均值
- javascript - 日期选择器作为表格
- xamarin.forms - 飞溅后应用程序中断,但视觉工作室仍在运行
- arrays - Excel VB用字符串替换0和1 - 但是字符串包含1和0,需要if语句来检查长度是否= 1然后替换否则跳过