所以,我最終要實現的是:三的div並排導致問題
https://s17.postimg.org/7zkpt511r/imgdivs.png
是我到目前爲止有:
<style type="text/css">
.wrapper{
height: 100%;
width: 100%;
}
.left{
float: left;
background: silver;
height: 100%;
}
.right{
background: silver;
float: right;
height: 100%;
}
canvas{
position: absolute;
height: 100%;
}
</style>
<div class = "wrapper">
<div class = "left"></div>
<canvas id="canvas"></canvas>
<div class = "right">
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
var WIDTH = 200; //could be anything, comes from previous calculations
$('#canvas').css("width", WIDTH);
$('#canvas').css("left", (window.innerWidth - WIDTH)/2);
$('.left').css("width", (window.innerWidth - WIDTH)/2);
$('.right').css("width", (window.innerWidth - WIDTH)/2);
var canvas = document.getElementById("canvas");
ctx = canvas.getContext("2d");
ctx.fillStyle = "red";
ctx.fillRect(0, 0, WIDTH, window.innerHeight);
</script>
那麼一點解釋,畫布在中間是動態的寬度(用javascript計算)並預先決定了它的高度(100%),它放在頁面的中間,兩邊的div應該相應地拉伸並填充頁面的其餘部分。我的代碼中有很多問題; div有時會溢出一個在另一個之下,由於某種原因,canvas不會填充整個維度等等。
我害怕嘗試在flexbox中做這件事,因爲舊的瀏覽器不支持它,我需要所有的瀏覽器支持。感謝您的時間。
爲了得到正確的填充使用:http://stackoverflow.com/questions/27736288/how-to-fill-the-whole-canvas-with-specific-color我已經刪除浮動和位置:從畫布絕對自你不需要它(全部放在CSS表中):https://jsfiddle.net/zd75wq2e/3/ – sinisake