Webkit 浏览器中 SVG 高度计算不正确

发布于 2024-12-06 21:58:18 字数 734 浏览 1 评论 0原文

我遇到了特定于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

征﹌骨岁月お 2024-12-13 21:58:18
svg { max-height: 100%; }

WebKit 错误记录如下: https://bugs.webkit.org/show_bug.cgi?id= 82489

我还将解决方法添加到了错误跟踪器中。

svg { max-height: 100%; }

WebKit bug documented here: https://bugs.webkit.org/show_bug.cgi?id=82489

I also added the workaround to the bug tracker.

青春如此纠结 2024-12-13 21:58:18

我在 Safari 上也遇到了类似的问题。情况是 svg 宽度和高度被渲染为 dom 元素属性(在我的例子中 width="588.75px" height="130px")。在 css 中定义宽度和高度无法覆盖此尺寸设置。

为了修复 Safari 的此问题,我从 SVG 文件中删除了宽度和高度信息,同时保持 viewBox 完整(您可以使用任何文本编辑器编辑 .svg 文件)。

我的 .svg 文件的 Git diff 片段:

    xmlns:svg="http://www.w3.org/2000/svg"
    xmlns="http://www.w3.org/2000/svg"
    viewBox="0 0 588.75 130"
-   height="130"
-   width="588.75"
    xml:space="preserve"
    version="1.1"

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:

    xmlns:svg="http://www.w3.org/2000/svg"
    xmlns="http://www.w3.org/2000/svg"
    viewBox="0 0 588.75 130"
-   height="130"
-   width="588.75"
    xml:space="preserve"
    version="1.1"
甜味超标? 2024-12-13 21:58:18

我只是在 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/

宽度=“1200”高度=“235.5”

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/

width="1200" height="235.5"

知足的幸福 2024-12-13 21:58:18

我很难准确理解你想要的例子是什么,或者什么不是你想要的。但是,一般来说,如果您使用具有百分比宽度和高度容器的布局,并且希望内容填充这些容器,则需要将它们从流中取出(在内容上使用 position:absolute以及容器上的 position:relativeposition:absolute)。

下面是一个可以在 Chrome 和 Safari 中查找的简单示例:
http://phrogz.net/SVG/size-to-fill.xhtml

#foo div 的高度和宽度占主体的百分比。它有一个永远不会被看到的红色背景,因为 SVG 在其中是 position:absolute 并设置为完全填充它,并且有一个绿色背景。如果你看到红色,那就是一个错误。

#foo, svg { position:absolute }
#foo { left:20%; width:30%; top:5%; height:80%; background:red; }
svg  { top:0; left:0; width:100%; height:100%;  background:green; }
<div id="foo"><svg ...></svg></div>

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 and position:relative or position: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 is position:absolute inside it and set to fill it completely, and have a green background. If you ever see red, it is an error.

#foo, svg { position:absolute }
#foo { left:20%; width:30%; top:5%; height:80%; background:red; }
svg  { top:0; left:0; width:100%; height:100%;  background:green; }
<div id="foo"><svg ...></svg></div>
不爱素颜 2024-12-13 21:58:18

这是一个已知问题,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

独守阴晴ぅ圆缺 2024-12-13 21:58:18

我知道如何修复它,你只需将其放在 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

旧情勿念 2024-12-13 21:58:18

正如之前指出的,WebKit 对 SVG 的缩放最近有所改进。在我看来,它在当前的 Safari(版本 5.1,WebKit 534)中仍然很糟糕。我做了一些实验并将我的发现记录在我的网站上:
http://www.vlado-do.de/svg_test/
简而言之:用 嵌入 svg在 WebKit 535 的大多数条件下都可以工作。对于较旧的 WebKit,我将其嵌入到 中。标签。如果您不想在 svg 中添加链接,那也没关系(但它在较旧的 Gecko 中不起作用,而且奇怪的是在当前的 Chromium 中也存在问题)。

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).

奈何桥上唱咆哮 2024-12-13 21:58:18

我发现添加“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?

猫性小仙女 2024-12-13 21:58:18

我遇到了 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 than document.ready.

document.ready fires when the DOM is ready, but images have not necessarily been rendered at this point. At the point where window.load fires, images will have been loaded (and thus the browser will be able to access their dimensions correctly).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文