我有画布。
<canvas id="canvas" width="1700" height="679" style="background-color:#ffffff;width:100%;overflow:hidden;margin-top: -7px;"></canvas>
在谷歌浏览器和火狐浏览器上工作正常。然而,在IE中只能使用width:100%,但不能改变高度(高度为679)
我尝试将高度设置为自动和100%,但效果更差了
编辑:终于!我明白了。 的确,在宽度为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();
};