Sha*_*had 2 javascript svg css3
好的,我花了一整天的时间来尝试将CSS样式与在inkscape中创建的svg文件“连接”起来。我有带地图和几个路径的svg文件,当我将鼠标悬停在该路径上时,我想更改填充颜色。拜托,你能帮我吗?:/。如果我在创造
<img src="example.svg" />
Run Code Online (Sandbox Code Playgroud)
,比我以CSS样式举例:
<style type="text/css">
#path6666
{ fill: green;
}
</style>
Run Code Online (Sandbox Code Playgroud)
什么都没发生。我不想使用jquery,仅使用javascript和CSS。请帮忙 !..
更新:
这是svg代码:
<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"
id="svg2985"
version="1.1"
inkscape:version="0.48.4 r9939"
width="1176"
height="617"
sodipodi:docname="mainMapa.svg">
<metadata
id="metadata2991">
<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></dc:title>
</cc:Work>
</rdf:RDF>
</metadata>
<defs
id="defs2989">
<inkscape:path-effect
effect="spiro"
id="path-effect3794"
is_visible="true" />
<inkscape:path-effect
effect="spiro"
id="path-effect3790"
is_visible="true" />
<inkscape:path-effect
effect="spiro"
id="path-effect3786"
is_visible="true" />
<inkscape:path-effect
effect="spiro"
id="path-effect3782"
is_visible="true" />
<inkscape:path-effect
effect="spiro"
id="path-effect3778"
is_visible="true" />
<inkscape:path-effect
effect="spiro"
id="path-effect2997"
is_visible="true" />
</defs>
<sodipodi:namedview
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1"
objecttolerance="10"
gridtolerance="10"
guidetolerance="10"
inkscape:pageopacity="0"
inkscape:pageshadow="2"
inkscape:window-width="1280"
inkscape:window-height="962"
id="namedview2987"
showgrid="false"
inkscape:zoom="0.97789116"
inkscape:cx="374.5729"
inkscape:cy="311.15508"
inkscape:window-x="-8"
inkscape:window-y="-8"
inkscape:window-maximized="1"
inkscape:current-layer="svg2985" />
<image
width="1176"
height="617"
xlink:href="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/
2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0a
HBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIy
BRRRQAUUUUAFFFFAB
RRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAFF
FFABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRRQAUUUUAFFFFABRRRQAUUU
UAFFFFABRRRQAUUUUAf/2Q==
"
id="image2993"
x="0"
y="0" />
<path
style="fill:#2b54e6;stroke:#281300;stroke-width:1px;stroke-linecap:butt;
stroke-linej oin:miter;stroke-opacity:1 ;fill -opacity:1"
d="m 308.82783,502.72348 3.57913,4.60174 5.36869,2.04521 5.11305,
3.57914 4.34608,3.83478 -0.76695,3.32348 -4.34609,4.09043 -3.57913,
1.27826 -4.09044,
-1.78956 -3.83478,-2.04522 -3.57913,-0.51131 -1.78956,-4.09043 -0.51131,
-5.62435 1.53391, -6.3913 z"
id="path3833"
inkscape:connector-curvature="0" />
</svg>
Run Code Online (Sandbox Code Playgroud)
我开始放弃了 我不明白:inkscape是非常流行的免费软件。为什么没有任何教程该怎么做?
您似乎在这里遇到三个问题:
Inkscape #path3833在嵌入式样式声明中定义路径的填充。这比您的样式表规则更具体,因此它会覆盖它。
为了解决您的问题,请在样式表!important中的声明上使用Inkscape的内联样式:
#path3833 {
fill:green !important; /* override inline style */
}
Run Code Online (Sandbox Code Playgroud)
注意罗伯特·朗森的答案,是使用而不是准确。您不能更改标签中托管的SVG的任何部分。<object type="image/svg+xml" data="[your file]"><img>img
要链接样式表,您可以将以下XML声明添加到SVG文档中:
<?xml-stylesheet type="text/css" href="your_style_sheet.css"?>
Run Code Online (Sandbox Code Playgroud)
它应位于开头的XML声明之间<?xml version="1.0" ...,但位于root svg元素之前。通常,这意味着它应该是文件的第二行。
要通过Javascript添加样式表,您必须创建XML处理指令(即<? ... ?>标记)。SVG document对象公开了执行此操作的方法。但是,获取SVG文档的参考非常棘手。你必须经历window.frames。这意味着您必须知道父HTML文档的哪个“框架”包含SVG。如果您有一个SVG文件,这很容易。否则,您可能必须使用反复试验。
无论如何,这大致应该是这样的:
var frameNumber = 0; //this may vary depending on your page
var svgDoc = window.frames[frameNumber].document;
var procInstruction = svgDoc.createProcessingInstruction(
'xml-stylesheet', // the type of processing instruction
'type="text/css" href="your_style_sheet.css"' // the "data" of the PI
);
svgDoc.insertBefore( // the PI must come *before* the SVG root element
procInstruction, svgDoc.childNodes[0]
);
Run Code Online (Sandbox Code Playgroud)
这个小提琴用数据URI而不是文件演示了以上两种技术。我不能使用动态部分来处理数据URI,但是如果可以使用文件,它将很好地工作。(Firefox v26 / Win7)