html - SVG foreignObject 的行为就像在 Webkit 浏览器中绝对定位一样

标签 html css google-chrome webkit svg

我正在 HTML 文档中使用 SVG。由于某些原因在 Chrome 中,任何 <foreignObject> 中的任何内容元素出现在 <svg> 的左上角元素的父元素;好像 <foreignObject>元素是绝对定位的什么的。我在 Firefox 中没有这个问题。

这可能是什么原因造成的?我该如何解决?

这是我的测试用例:( the example is also on JsFiddle )

<!DOCTYPE html>
<html>
<head>
<title>SVG bug in Chrome?</title>
<style type="text/css">
code {
    background: #FFFAEE;
}
pre code {
    display:block;
}
.widget-body {
    background:yellow;
    position: relative; /* This is the problem! */
}
</style>
<body>
<h1>SVG bug in Chrome?</h1>
<div>
    <p>
        The elemts in the &lt;foreignObject&gt; are not positioned properly unless the <code>.widget-body</code> rule is changed to:
                <pre><code>.widget-body {
    background:yellow;
/*  position: relative; /* This is the problem! */
    position: static;
}</code></pre>
    </p>
        <h2>The Example:</h2>
    <svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="1000" height="800">
    <g>
    <g transform="scale(1) translate(100, 200)" style="cursor: move;"><foreignobject pointer-events="fill" width="300" height="350">
    <body xmlns="http://www.w3.org/1999/xhtml" style="margin: 0px; height: 100%;">
    <table style="border-collapse: collapse; font-size: 11px; color: rgb(119, 68, 0); font-family: Arial,Helvetica; font-weight: normal; border-style: none;">
    <tbody>
    <tr>
        <td style="text-align: center; vertical-align: middle; white-space: nowrap;">
            <div style="width:300px;height:350px;position:static;">
                <div class="widget" style="width: 300px;">
                    <div style="-moz-user-select: none;">
                        <span>My Widget Title</span>
                    </div>
                    <div>
                        <div class="widget-body" style="width: 298px; height: 323px;">
                            <div style="width: 298px; height: 323px;">
                                <div style="width: 298px; height: 323px;">
                                    This position of this yellow square <br />should approximately (100, 200)
                                    <div style="position:absolute;bottom:0;right:0;background:red;color:white;font-weight:bold;">
                                        This red square <br />should be <br />in the bottom right corner <br />of the yellow square.
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </td>
    </tr>
    </tbody>
    </table>
    </body>
    </foreignobject></g>
    </g>
    </svg>
</div>
</body>
</html>

我希望看到的(这是我在 FireFox 中看到的)是这样的:

The behavior of my example in FireFox

我在 Chrome(Fedora 和 Android 平板电脑上的 15.0.874.121)中得到的是这样的:

The behavior of my example in Chrome. The contents of the foreignObject are not in the right place.

我对我的 HTML 内容几乎没有控制权,因为我同时使用 JavaScript 框架来构建丰富的应用程序和预先存在的小部件。

最佳答案

这个问题不是 Chrome 特有的,因为我可以在适用于 Macintosh 的 Chrome 15.0.874.121 和 Safari 5.1.2 中重现它。 (它也发生在 Firefox for Mac 的旧版本中,例如版本 3.6.8,但该行为在当前版本中是正确的。)This currently outstanding Webkit bug很可能是问题的原因。全局坐标错误地用于 foreignObject 内的元素,这意味着当使用绝对定位时,这些元素是相对于主文档流而不是 foreignObject 容器放置的,因此 SVG 转换不适用于这些元素。

我无法找到解决此问题的通用方法。

对于这个具体的例子,这将修复所有上述浏览器的布局:

.widget {
    position: relative;
    left: 100px;
    top: 200px;
}

Demonstration on jsfiddle.

关于html - SVG foreignObject 的行为就像在 Webkit 浏览器中绝对定位一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8185845/

相关文章:

javascript - 在我的案例中如何检测 div 冲突?

html - CSS Polaroid 样式图像/标题缩放问题

google-chrome - 检测 Chrome 扩展程序的浏览器 "back"输入

.net - 单击一次的Google Chrome扩展程序?

javascript - 获取 map 区域的位置(html)?

html - 如何将一个 div 放置在另一个具有 float :right 的 div 下

php - 需要插入表 "doctors"(id 是自动增量)。不知道查询出了什么问题。我收到错误 -"webpage not available "。帮助解决它

javascript - Navbar 下拉列表始终保持在其他下拉列表下方,增加 z-index 等不起作用

javascript - 自动完成功能无法正常工作

javascript - 为什么非 dojo javascript 错误似乎是由 dojo.xd.*.js 在 chrome 中发起的?