对于 Firefox、Safari 和 Chrome 中发生的奇怪行为,我一定已经杀死了一个多小时。这是一些代码:
<!doctype html>
<html>
<head>
<title>Watch me grow scrollbars!</title>
</head>
<body onload="resizeCanvas()">
<canvas id="canvas"></canvas>
</body>
</html>
<script type="application/javascript">
function resizeCanvas() {
var canvas = document.getElementById("canvas");
canvas.setAttribute("height", window.innerHeight);
canvas.setAttribute("width", window.innerWidth);
}
</script>
基本上,画布似乎总是“过多”地最大化,并且窗口的两侧都增加了滚动条。我尝试使用各种获取文档尺寸的方法,包括在 100% 宽和高的绝对定位 div 中嵌套画布,以及提取 document.documentElement.clientWidth/Height 属性。
为了确保我不会发疯,我在画布上放置了一个图像,瞧,同样的代码可以完美地将图像拉伸到文档尺寸。
我在这里做错了什么?我太累了无法理解。必须..喝..东西。