【发布时间】:2023-04-03 10:15:01
【问题描述】:
我有画布
<canvas id="canvas" width="1700" height="679" style="background-color:#ffffff;width:100%;overflow:hidden;margin-top: -7px;"></canvas>
它在 chrome 和 firefox 上运行良好。但是,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";
}
}
适用于 document.ready 的 re-size 窗口
window.onresize = function(event) {
resizeIE();
};
【问题讨论】:
-
你不能使用 css 应用高度?
-
@sajay 我将 css 内联画布添加到 height:100% 和 height:auto。结果更小,调整大小时保持不变
-
感谢您推荐使用 css :)
-
@user1128331 CSS 不适用于画布,因为它会拉伸画布。也请参阅 markE 的答案。
-
@user1128331 删除了您的编辑。不要靠近主题。人们正在利用自己的时间来帮助您,因此请考虑下面提供的解决方案。如果您自己找到了解决方案,您可以将其发布为答案。见stackoverflow.com/help/self-answer