1
我做了一個頁面,畫布應該填滿窗口。它可以工作,但出於某種原因滾動條出現。我不知道,錯誤是什麼。當我調整畫布大小時,爲什麼會出現滾動條?
<!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>
特別是,畫布是一個內聯元素,所以它有一個基線,就像所有其他內聯元素一樣。因此,代碼創建了一個與窗口大小相同的畫布,但它必須位於塊底部之上的基線上。在這種情況下,更改畫布的垂直對齊方式也會起作用,但是'display:block;'是更好的解決方案。 – Neil 2012-02-12 21:45:39