我有一个SVG文件,它指定了一个渐变和一个如下所示的圆圈。嵌入式脚本切换渐变onClick()的方向,该方法适用于除IE9之外的所有当前浏览器。我怀疑IE不会重绘渐变。我尝试了一些方法,例如首先将填充设置为纯色,然后重新分配(改变的)渐变,以触发重绘但到目前为止没有骰子。我的问题是,是否有人可以解决这个问题,或者甚至更好地解决它。感谢。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" height="70" width="190">
<script type="text/javascript">
<![CDATA[
function flipGrad(evt) {
var g=document.getElementById('grad1');
var y1 = g.getAttribute('y1');
var y2 = g.getAttribute('y2');
g.setAttribute('y2', y1);
g.setAttribute('y1', y2);
}
]]>
</script>
<defs>
<linearGradient id="grad1" x1="0%" y1="0%" x2="0%" y2="100%">
<stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
<stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
</linearGradient>
</defs>
<circle cx="30" cy="30" r="20" stroke="black" stroke-width="2" fill="url(#grad1)" onclick="flipGrad(evt)" />
</svg>
编辑:
编辑停靠点帮助,以便可行。但事实是,我的实际文件看起来更像是以下内容,即Inkscape svg输出,其中渐变分为颜色部分和几何部分,只有几何体与from和object相关联,但颜色在多个其他渐变中重复使用。交换停靠点的方法会影响链接到该颜色渐变的所有对象:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" height="70" width="190">
<script type="text/javascript">
<![CDATA[
function flipGrad(evt) {
// var g=document.getElementById('gradGeometry');
// var y1 = g.getAttribute('y1');
// var y2 = g.getAttribute('y2');
// g.setAttribute('y2', y1);
// g.setAttribute('y1', y2);\
var s1=document.getElementById('stop1');
var s2=document.getElementById('stop2');
var s1s = s1.getAttribute('style');
var s2s = s2.getAttribute('style');
s1.setAttribute('style', s2s);
s2.setAttribute('style', s1s);
}
]]>
</script>
<defs>
<linearGradient id="gradColour">
<stop id="stop1" offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
<stop id="stop2" offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
</linearGradient>
<linearGradient id="gradGeometry1" x1="0%" y1="0%" x2="0%" y2="100%" xlink:href="#gradColour" />
<linearGradient id="gradGeometry2" x1="0%" y1="0%" x2="100%" y2="0%" xlink:href="#gradColour" />
</defs>
<circle cx="30" cy="30" r="20" stroke="black" stroke-width="2" fill="url(#gradGeometry1)" onclick="flipGrad(evt)" />
<circle cx="90" cy="30" r="20" stroke="black" stroke-width="2" fill="url(#gradGeometry2)" onclick="flipGrad(evt)" />
</svg>
答案 0 :(得分:0)
在IE9上测试一下后,似乎梯度向量在IE中定义后是不可变的。您唯一的选择是使用id'd渐变停止,这是可变的。
<svg xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" height="70" width="190">
<script type="text/javascript">
<![CDATA[
function flipGrad(evt) {
var s1=document.getElementById('stop1');
var s2=document.getElementById('stop2');
var s1s = s1.getAttribute('style');
var s2s = s2.getAttribute('style');
s1.setAttribute('style', s2s);
s2.setAttribute('style', s1s);
}
]]>
</script>
<defs>
<linearGradient id="grad1" x1="0%" y1="0%" x2="0%" y2="100%">
<stop id="stop1" offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
<stop id="stop2" offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
</linearGradient>
</defs>
<circle id="bubble" cx="30" cy="30" r="20" stroke="black" stroke-width="2" fill="url(#grad1)" onclick="flipGrad(evt)" />
</svg>