我制作了一个页面,画布应该填满窗口。它有效,但出于某种原因出现滚动条。我不知道,错误是什么。
<!doctype html>
<html>
<head>
<title>test</title>
<meta charset="utf-8" />
<style type="text/css">
#canvas {
background-color: #EEEEEE;
}
* {
margin: 0px;
}
</style>
<script type="text/javascript">
function getSize() {
var myWidth = 0, myHeight = 0;
if( typeof( window.innerWidth ) == 'number' ) {
//Non-IE
myWidth = window.innerWidth;
myHeight = window.innerHeight;
} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
//IE 6+ in 'standards compliant mode'
myWidth = document.documentElement.clientWidth;
myHeight = document.documentElement.clientHeight;
} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
//IE 4 compatible
myWidth = document.body.clientWidth;
myHeight = document.body.clientHeight;
}
return {x:myWidth,y:myHeight};
}
window.onload = function () {
canvas = document.getElementById('canvas');
var size = getSize();
canvas.width = size.x;
canvas.height = size.y;
};
</script>
</head>
<body>
<canvas id="canvas" width="200" height="200"></canvas>
</body>
</html>
答案 0 :(得分:3)
将#canvas转换为块为我做了诀窍:
#canvas {
display: block;
background-color: #EEEEEE;
}
答案 1 :(得分:1)
你的CSS可能有问题。
我将'所有元素'移到了顶部,并使画布绝对定位以确保安全。
让我知道这是否有效:
<style type="text/css">
* {
margin: 0px;
padding:0px;
}
#canvas {
position:absolute;
left:0px;
top:0px;
background-color: #EEEEEE;
}
</style>