我们有一个简单的代码,可以跨浏览器运行:
<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>
在Firefox浏览器中,当我尝试使用defs块中的元素时,两个矩形之间的关系会丢失。
<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 - 实际上是这些话:
请注意,此效果不适用于Firefox Firefox 6及更早版本(我想),这可能是此方法最大的缺点。
我需要按下按钮才能移动(更改)另一个元素。我还尝试创建类似于此的东西,以设置关系效果:
<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中,我们得到不同的结果。此外,我不理解浏览器之间的差异。我相信,存在一个简单的解决方案。
<use>
元素的兼容性不是很好。 - Robert Longson