我正在使用html5画布和一些javascript(onmousedown / move / up)在网页上创建简单的绘图板。
在Opera,Firefox,Chrome等桌面PC上测试时,它工作得很好。但是如果我使用iPhone访问此页面,在尝试在画布上绘制时,它会拖动或滚动页面。
对于其他页面内容来说,这没问题,在移动浏览器中通过向上或向下滑动页面可以像平常一样导航。但是否有一种方法可以禁用画布上的此行为,以便移动访问者也能够在其上实际绘制一些东西?
以下是一个最简示例供您参考:
<html><head><script type='text/javascript'>
function init()
{
var canvas = document.getElementById('MyCanvas');
var ctx = canvas.getContext('2d');
var x = null;
var y;
canvas.onmousedown = function(e)
{
x = e.pageX - canvas.offsetLeft;
y = e.pageY - canvas.offsetTop;
ctx.beginPath();
ctx.moveTo(x,y);
}
canvas.onmouseup = function(e)
{
x = null;
}
canvas.onmousemove = function(e)
{
if (x==null) return;
x = e.pageX - canvas.offsetLeft;
y = e.pageY - canvas.offsetLeft;
ctx.lineTo(x,y);
ctx.stroke();
}
}
</script></head><body onload='init()'>
<canvas id='MyCanvas' width='500' height='500' style='border:1px solid #777'>
</canvas></body></html>
在canvas中,我是否需要添加特殊的样式或事件来避免在滑动canvas时拖动/滚动页面?
e.preventDefault();
,规范链接为http://www.w3.org/TR/touch-events/#the-touchstart---------event - lapo