javascript - SVG defs 使用不同图形之间的关系 Firefox

标签 javascript html css firefox svg

我们有一个简单的代码,它可以跨浏览器工作:

<html>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
width="200" height="100">

    <circle id="circ" cx="120" cy="40" 
    r="30" fill="green"/>

    <rect id="rect" x="10" y="10"
    width="60" height="60" fill="blue">
        <set attributeName="fill-opacity" to="0.5"
        begin="circ.mouseover" end="circ.mouseout"/>
    </rect>
</svg>

当我尝试使用 defs block 中的元素时,我正在失去 Firefox 浏览器中两个矩形之间的关系。

<html>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
width="200" height="100">
    <defs>
        <circle id="circ" cx="120" cy="40" 
        r="30" fill="green"/>

        <rect id="rect" x="10" y="10"
        width="60" height="60" fill="blue">
            <set attributeName="fill-opacity" to="0.5"
            begin="circ.mouseover" end="circ.mouseout"/>
        </rect>
    </defs>

    <use id="use_circ.rectangles" xlink:href="#circ" />
    <use id="use_rect.rectangles" xlink:href="#rect" />
</svg>

我在这里阅读了一些关于关系的内容 http://www.petercollingridge.co.uk/data-visualisation/mouseover-effects-svgs ? - 其实这句话

"Note that this effect does not work in Firefox Firefox 6 and earlier (I think), which is perhaps the biggest drawback of this method. "

我需要按下按钮来移动(更改)另一个元素。 我也试过做这样的东西,设置关系效果:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Button</title>

    <script type="text/javascript" language="javascript">
        function turn_right(button, miliSec)
        {
            document.getElementById("circ_left_position").setAttribute('visibility','hidden');

            setTimeout(function() {
                button.parentNode.setAttribute('xlink:href','#rect.right_position');
            }, miliSec);

            document.getElementById("circ_right_position").setAttribute('visibility','visible ');
        }
        function turn_left(button, miliSec)
        {
            document.getElementById("circ_right_position").setAttribute('visibility','hidden ');

            setTimeout(function() {
                button.parentNode.setAttribute('xlink:href','#rect.left_position');
            }, miliSec);

            document.getElementById("circ_left_position").setAttribute('visibility','visible ');
        }
    </script>
</head>

<body style="margin:0; border:0">
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <svg xmlns="http://www.w3.org/2000/svg"
    xmlns:xlink="http://www.w3.org/1999/xlink"
    width="200" height="100">

        <defs>
            <g id="rect.left_position" onclick="turn_right(this,500)">
                <rect x="10" y="10" width="60" height="60" fill="blue"/>

                <circle id="circ_left_position" cx="120" cy="40" r="30" fill="green"/>

                <animateTransform id="trigger1" begin="click"
                attributeName="transform" type="rotate" additive="replace"
                from="0 70 70" to="10 70 70" dur="0.5s" fill="freeze" />
            </g>

            <g id="rect.right_position" onclick="turn_left(this,500)">
                <rect x="10" y="10" width="60" height="60" fill="blue" transform="rotate(10 70 70)"/>
                <circle id="circ_right_position" cx="120" cy="40" r="30" fill="green"/>

                <animateTransform id="trigger1" begin="click"
                attributeName="transform" type="rotate" additive="replace"
                from="0 70 70" to="-10 70 70" dur="0.5s" fill="freeze" />
            </g>
        </defs>

        <use id="positions.rectangles" xlink:href="#rect.left_position" />

    </svg>
</body>

在 Chrome 和 FF 中我们得到不同的结果。此外,我不了解浏览器的差异。我敢肯定,存在一个简单的解决方案。

最佳答案

使用不复制动画这不是 SVG 1.1 中的预期行为(这不是 firefox 问题)

SVG 2 中的 Smil 将解决这个问题。

您可以做的是使用脚本克隆每个动画并将它们附加到 use 标签。也许在 onload 事件上。但是我相信这不适用于您的特定代码集,因为指定了一个 id。

关于javascript - SVG defs 使用不同图形之间的关系 Firefox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24908834/

相关文章:

javascript - Jquery.change 在页面加载时触发

javascript - 为选择器中的每个元素触发事件,只需触发一次

javascript - 使用 jQuery 检测滚动方向的变化

javascript - 如何将多个 amcharts 导出为 PDF 文件?

javascript - Asp.Net 从 javascript 的弹出窗口中获取值

jQuery 多个处理程序

php - post数组空提交

css - 如何使 float 元素居中?

iphone - CSS 媒体查询 - 无法使其正常工作

javascript - 无法使用 Javascript 为 div #box 设置动画