Webkit 浏览器中 SVG 高度计算不正确
我遇到了特定于 Webkit 浏览器(Mac 和 PC 上的 Safari 和 Chrome)的问题。
我使用 Raphael JS 渲染 SVG 数据,并使用响应式布局通过浏览器窗口缩放 SVG。使用 JQuery 将 SVG 设置为 100% 宽度/高度。包含元素的宽度以百分比设置,以在页面调整大小时保持布局的比率。
问题是 Webkit 无法正确计算高度,它会在 SVG 图像周围添加额外的像素(有时数百个);这打破了布局。
如果您在 Webkit 浏览器中打开以下链接,您将看到绿色的额外像素区域。如果您在 safari 中使用开发人员检查器,您会看到报告的 SVG 大小大于显示的 SVG 大小。
http://e-st.glam.ac.uk/simulationgames/svgheightbug /index.html
如果您在 Firefox 或 Opera 中打开链接,您将看到正常的布局(这里的绿色是由我故意设置的边距造成的)。
IE8 也有类似的问题,使用 height:auto 修复了,但这在 Webkit 中不起作用。
还有其他人遇到过这个问题吗?有人有解决办法吗?
我认为这可能是一个 Webkit 错误(已经检查了夜间构建,问题仍然存在),但在我记录它之前,我想先检查一下以确保其他人没有解决方案。
I have a issue specific to Webkit browsers (Safari & Chrome, on Mac & PC).
I'm using Raphael JS to render SVG data and using a responsive layout to scale the SVGs with the browser window. The SVGs are set to 100% width/height using JQuery. The containing elements have their widths set in percentages to maintain the ratios of the layout as the page resizes.
Trouble is Webkit doesn't calculate the height correctly, it adds extra pixels (sometimes hundreds) around the SVG image; which breaks the layout.
If you open the following link in a Webkit browser you'll see the green extra pixel areas. If you use the developer inpspector in safari you'll see the reported size for the SVG is bigger than the SVG displayed.
http://e-st.glam.ac.uk/simulationgames/svgheightbug/index.html
If you open the link in Firefox or Opera you'll see the layout as it should work (the green here is caused by margins I have deliberately set).
IE8 was having a similar problem which using height:auto fixed, but this won't work in Webkit.
Has anybody else had this problem? Anybody have a solution?
I think it may be a Webkit bug (checked the nightly build already, issue persists), but before I log it I thought check to make sure nobody else has a solution first.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
WebKit 错误记录如下: https://bugs.webkit.org/show_bug.cgi?id= 82489
我还将解决方法添加到了错误跟踪器中。
WebKit bug documented here: https://bugs.webkit.org/show_bug.cgi?id=82489
I also added the workaround to the bug tracker.
我在 Safari 上也遇到了类似的问题。情况是 svg 宽度和高度被渲染为 dom 元素属性(在我的例子中 width="588.75px" height="130px")。在 css 中定义宽度和高度无法覆盖此尺寸设置。
为了修复 Safari 的此问题,我从 SVG 文件中删除了宽度和高度信息,同时保持 viewBox 完整(您可以使用任何文本编辑器编辑 .svg 文件)。
我的 .svg 文件的 Git diff 片段:
I had a similar problem for Safari. Case was that the svg width and height were rendered as dom element attributes (in my case width="588.75px" height="130px"). Defining width and height in css could not overwrite this dimension setting.
To fix this for Safari I removed the width and height information from the SVG file while keeping viewBox intact (you can edit .svg files with any text editor).
Git diff snippet of my .svg file:
我只是在 svg 中将高度设置为非常大的尺寸以保持纵横比。使用 100% 会带来太多问题。这对我来说效果更好,因为我不想使用 js。
完整道具:
http://www.seowarp.com/blog/2011/06/svg-scaling-problems-in-ie9-and-other-browsers/" seowarp.com/blog/2011/06/svg-scaling-problems-in-ie9-and-other-browsers/
I just set the height to a very large size in the svg to maintain the aspect ratio. Using 100% comes with too many problems. This works better for me because I did not want to use js.
Full props to:
http://www.seowarp.com/blog/2011/06/svg-scaling-problems-in-ie9-and-other-browsers/
我很难准确理解你想要的例子是什么,或者什么不是你想要的。但是,一般来说,如果您使用具有百分比宽度和高度容器的布局,并且希望内容填充这些容器,则需要将它们从流中取出(在内容上使用
position:absolute
以及容器上的position:relative
或position:absolute
)。下面是一个可以在 Chrome 和 Safari 中查找的简单示例:
http://phrogz.net/SVG/size-to-fill.xhtml
#foo
div 的高度和宽度占主体的百分比。它有一个永远不会被看到的红色背景,因为 SVG 在其中是position:absolute
并设置为完全填充它,并且有一个绿色背景。如果你看到红色,那就是一个错误。It's hard for me to understand exactly what you want with your example, or what is not as you intend it. However, in general, if you are using a layout with percentage widths and height containers and you want content to fill those containers, you need to take them out of the flow (using
position:absolute
on the content andposition:relative
orposition:absolute
on the containers).Here's a simple example that works find in Chrome and Safari:
http://phrogz.net/SVG/size-to-fill.xhtml
The
#foo
div has its height and width as a percentage of the body. It has a red background that will never be seen because the SVG isposition:absolute
inside it and set to fill it completely, and have a green background. If you ever see red, it is an error.这是一个已知问题,Chromium 团队已在版本 15.0.874.121 中修复。我今天亲自验证了此修复。
http://code.google.com/p/chromium/issues /detail?id=98951#c27
This is a known issue that has been fixed by the Chromium team with version 15.0.874.121. I verified this fix myself just today.
http://code.google.com/p/chromium/issues/detail?id=98951#c27
我知道如何修复它,你只需将其放在 svg 文件的开头:“preserveAspectRatio =“xMinYMin none”它必须像这样放入 svg 标签中:
问题将得到解决
i know how to fix it, you have just to put this at the begining of your svg file: "preserveAspectRatio="xMinYMin none" it must be into svg tag like this:
Problem will be fix
正如之前指出的,WebKit 对 SVG 的缩放最近有所改进。在我看来,它在当前的 Safari(版本 5.1,WebKit 534)中仍然很糟糕。我做了一些实验并将我的发现记录在我的网站上:
http://www.vlado-do.de/svg_test/
简而言之:用
As previously pointed out WebKit's scaling of SVG improved recently. It is still quite broken in the current Safari (version 5.1, WebKit 534), in my opinion. I did a few experiments and recorded my findings on my website:
http://www.vlado-do.de/svg_test/
In short: embedding svg with <object> works under most conditions in WebKit 535. For older WebKit I embed it in an <img> tag. That's fine if you don't want links inside your svg (but it does not work in older Gecko and strangely also is problematic in current Chromium).
我发现添加“position:absolute;”到图像元素(如果它位于也绝对定位的父元素内),它调用了我的 .svg,使“高度:100%;”声明变得相对于其容器而不是页面/浏览器/设备。
在适用于 iOS 7.1 的 Chrome 和 Safari(移动 webkit)上对此进行了测试,它解决了我的问题(.svg 文件超出了其容器的范围)。
希望这对于其他遇到麻烦的人来说是一个可靠的解决方案。值得一试吗?
I found that adding "position: absolute;" to the image element (if it's within a parent that's also absolutely positioned), which had my .svg being called, made the "height: 100%;" declaration become relative to its container instead of the page/browser/device.
Tested this on both Chrome and Safari (mobile webkit) for iOS 7.1, and it fixed my problem (the .svg file was going way outside of its container).
Hopefully this a somewhat reliable fix for others here who were having trouble. Worth a shot?
我遇到了 Javascript 返回错误的 SVG“高度”值的问题,我发现解决方案只是在
window.load
上运行我的脚本(需要访问高度的位),而不是文档.ready
。document.ready
在 DOM 准备就绪时触发,但此时图像不一定已渲染。在window.load
触发时,图像将被加载(因此浏览器将能够正确访问其尺寸)。I was having a problem with Javascript returning incorrect "height" values for SVGs, and I found the solution was simply to run my script (the bit that needed to access the height) on
window.load
rather thandocument.ready
.document.ready
fires when the DOM is ready, but images have not necessarily been rendered at this point. At the point wherewindow.load
fires, images will have been loaded (and thus the browser will be able to access their dimensions correctly).