我已经在线尝试了许多解决方案,但是由于某些原因,在画布上移动触摸时,此页面仍会垂直移动:
<html>
<head>
<style>
html, body {
overflow-x: hidden;
}
body {
background-color: lightgray;
position: relative;
}
body.noScroll {
overflow: hidden;
}
</style>
</head>
<body>
<canvas id="myCanvas" style="touch-action: none;" width="800" height="800"></canvas><br>
<script>
document.body.addEventListener("touchmove", getTouchPos)
var canvas = document.getElementById('myCanvas')
var context = canvas.getContext('2d')
context.fillStyle = "#000";
context.fillRect(0, 0, canvas.width, canvas.height);
function getTouchPos(evt) {
evt.preventDefault();
}
</script>
</body>
</html>
在iOS Safari上测试。
答案 0 :(得分:2)
您是否找到了此信息? Disable scroll/swipe action for html canvas drawing on ios
它导致了以下链接。看来您必须考虑多个事件。您还必须将画布作为dom元素引用才能评估目标
http://bencentra.com/code/2014/12/05/html5-canvas-touch-events.html