我有帆布化妝畫布高度自動
<canvas id="canvas" width="1700" height="679" style="background-color:#ffffff;width:100%;overflow:hidden;margin-top: -7px;"></canvas>
它工作於Chrome和Firefox的罰款。然而,也就是可以用寬度只工作:100%,但不改變其高度(上679高度)
我試着高度爲汽車和100%,但得到wose
編輯:終於來了!我知道了。確實,畫布的內容在寬度100%上不會很好。 然而,對於高度(IE9以上工作),你必須設置高度風格
$("#canvas").attr('style','background-color:#ffffff; width:100%;height:679px;overflow:hidden;margin-top: -7px;');
而且使用jQuery重新大小重新大小的窗口畫布
function resizeIE()
{
canvas = document.getElementById("canvas");
if($.browser.msie) //only IE
{
$("#canvas").attr('style','background-color:#ffffff; width:100%;height:679px;overflow:hidden;margin-top: -7px;');
//set the height style first
if(window.innerWidth<960) //for other device (only for me)
{
var height_ie = (window.innerWidth*39.941176470588235294117647058824)/100;
//to make the ratio of canvas find the pencentage
//ex. canvas height: 1700px canvas width: 679px;
//(679*100)/1700 = 39.941 <-- use this one
//best solution
}
else
{
var height_ie = window.innerHeight-160; //just for the logo for my web
}
canvas.style.height = height_ie+"px";
}
}
適用於文件。準備好
window.onresize = function(event) {
resizeIE();
};
使用CSS,你可以不適用高度? – sajay
@sajay我添加CSS內聯畫布到高度:100%和高度:自動。結果是更小,並保持不變,當調整大小 – user1128331
謝謝你推薦使用CSS :) – user1128331