【发布时间】:2012-07-22 07:21:07
【问题描述】:
我正在尝试调整svg 元素的大小以尽可能大地适应浏览器,同时保持100x50 的比例。当我检查对 DOM 所做的更改时,一切正常,但更新后的尺寸似乎没有呈现。我该如何解决这个问题?
http://jsfiddle.net/Wexcode/SE6d3/show/
var svg = document.getElementById("svg");
resize.call(svg);
window.onresize = function() {
resize.call(svg);
}
function resize() {
this.setAttribute("width", scale(100));
this.setAttribute("height", scale(50));
function scale(value) {
return Math.min(window.innerWidth / 100, window.innerHeight / 50) * value;
}
}
编辑: sq2's answer,将代码从
this.setAttribute("width", scale(100));
this.setAttribute("height", scale(50));
到
this.style.width = scale(100);
this.style.height = scale(50);
提供了修复,但在 Firefox 中仍然存在问题。
【问题讨论】:
-
我想我误解了一些东西。我在最新的 Firefox 中打开演示,尝试调整浏览器窗口的大小,黑框总是调整大小,使其宽度和高度保持 2:1 的比例。应该怎么做?
-
@duri - 以上版本在 Chrome 中不起作用。使用
this.style[attribute]修复 Chrome,但在 Firefox 中不起作用。见:jsfiddle.net/Wexcode/SE6d3/16/show -
我可能遗漏了一些东西,但你为什么不直接通过 CSS 调整大小呢? jsfiddle.net/RqUY3
-
@Duopixel - 不会产生 100x50 的比例。
-
使用 Math.min 而不是 'minimum'
标签: javascript html css firefox svg