【发布时间】:2019-05-10 02:47:50
【问题描述】:
我对此相当缺乏经验,所以要温柔;-)。
我正在尝试在 Inkscape 中制作类似动画图标集的东西。 为了向矩形“符号”添加行为,我向其中添加了一些 Javascript。到目前为止,一切都很好。如果我在“使用”标签的帮助下克隆“符号”并将鼠标悬停在矩形上,它会改变颜色。
问题是:如果我使用“use”标签创建第二个克隆,如果我将鼠标悬停在其中一个上,两个副本都会改变颜色。
这不是我想要的。我希望“use1”能够独立于“use2”改变颜色。同时我希望脚本成为“符号”标签的一部分,而不是“使用”标签的一部分。
示例代码(不成功):
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="210mm"
height="297mm"
viewBox="0 0 210 297"
version="1.1"
id="svg8"
inkscape:version="0.92.1 r15371"
sodipodi:docname="rectangle.svg">
<script
type="text/javascript"
href="svg.js"
id="script5609" />
<defs
id="defs2">
<symbol
id="symbol7630"
onmouseover="console.log(evt.target)"
onmouseout="evt.target.style.fill='blue'">
<rect
style="fill:#ff0000;stroke:#00fb00;stroke-width:3.16499996;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
id="BigRect"
width="57.452377"
height="36.285713"
x="61.988094"
y="47.535706" />
<rect
style="fill:#ff0000;stroke:#00fb00;stroke-width:3.16499996;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
id="SmallRect"
width="21.166666"
height="35.529762"
x="143.63095"
y="45.267857" />
<script
type="text/javascript"
id="script5613"><![CDATA[
var element = SVG.get('SmallRect')
element.style('fill', 'yellow')
element.click(function() {
this.style('fill', 'green')
})
element.mouseover(function() {
this.style('fill', 'red')
})
element.mouseout(function() {
this.style('fill', 'blue')
})
//element.attr('fill', '#c06')
//element.fill('#c06')
//element.stroke(
]]></script>
</symbol>
</defs>
<sodipodi:namedview
id="base"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageopacity="0.0"
inkscape:pageshadow="2"
inkscape:zoom="0.75722503"
inkscape:cx="104.33497"
inkscape:cy="561.25984"
inkscape:document-units="mm"
inkscape:current-layer="layer1"
showgrid="false"
inkscape:window-width="1920"
inkscape:window-height="1017"
inkscape:window-x="-8"
inkscape:window-y="-8"
inkscape:window-maximized="1" />
<metadata
id="metadata5">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title />
</cc:Work>
</rdf:RDF>
</metadata>
<g
inkscape:label="Layer 1"
inkscape:groupmode="layer"
id="layer1">
<!-- Specify the place where the animation library svg.js can be found -->
<use
xlink:href="#symbol7630"
id="use16221"
transform="translate(-15.72353,1.3976471)"
x="0"
y="0"
width="100%"
height="100%" />
<use
id="use3984"
xlink:href="#symbol7630"
x="0"
y="0"
width="100%"
height="100%"
transform="translate(-20.449326,79.41301)" />
<use
id="use4008"
xlink:href="#symbol7630"
x="0"
y="0"
width="100%"
height="100%"
transform="translate(-37.570503,138.11419)" />
</g>
</svg>
【问题讨论】:
-
这是意料之中的事。如果您将行为添加到
<symbol>,它仍然是模板的一部分,并将平等地应用于所有<use>克隆。要获得每个克隆的个体行为,需要将行为附加到个体克隆。为了帮助您解决问题,我们需要更详细地描述您正在努力实现的目标以及到目前为止您已经尝试过的目标,最好是 minimal reproducible example。 -
你好ccprog,非常感谢你的回答。 “要获得每个克隆的个体行为,需要将行为附加到个体克隆。” --> 这就是我想要实现的目标,但 scipt 必须驻留在“符号”中。我没有样品,因为我不知道该怎么做。我们的想法是拥有一个带有脚本的 SVG 库,它作用于符号的各个实例而不是符号本身。
-
我添加了到目前为止我所做的代码......它作用于
,而不是 -
我可以在
中有一个脚本检查 DOM 中的 ,然后将脚本复制到所有 中,但这有点难看......我不这样做;不知道能不能搞定……