简体   繁体   English

是否可以使用Javascript动态修改作为资源加载的SVG?

[英]Is it possible to dynamically modify an SVG loaded as a resource with Javascript?

I have these icons and I'd like to procedurally add a drop shadow (basically, something, anything ) to them on hover so they don't look so hokey. 我有这些图标,我想在悬停时向它们添加一个阴影(基本上是一些东西 ),以使它们看起来不那么笨拙。

They are SVG's so in theory I can prepend something like this: 它们是SVG,因此从理论上讲,我可以添加以下内容:

<filter id="f1" x="0" y="0" width="200%" height="200%">
  <feOffset result="offOut" in="SourceAlpha" dx="20" dy="20" />
  <feGaussianBlur result="blurOut" in="offOut" stdDeviation="10" />
  <feBlend in="SourceGraphic" in2="blurOut" mode="normal" />
</filter>

and some javascript magic to apply it on mouse over. 以及一些将其应用到鼠标上的JavaScript魔术。 This could possibly save ages of design work. 这样可以节省设计工作的时间。

Problem is, the svg's are presented as <a style='background-image:url(icon.svg)' /> . 问题是,svg显示为<a style='background-image:url(icon.svg)' />

Is there a way to get into the SVG element? 有没有办法进入SVG元素?

What if you use jquery addclass and simply link to another svg that has your changes? 如果您使用jquery addclass并简单地链接到另一个进行了更改的svg,该怎么办?

You can also create an svg element with html code (right click on file --> view with notepad) and you will have the code there -- there are online converters that can make all neccessary tweaks and then you can enter the svg code with javascript 您还可以使用html代码创建svg元素(右键单击文件->使用记事本查看),然后在其中找到代码-在线转换器可以进行所有必要的调整,然后您可以输入svg代码, javascript

If you're using SVG as an image then you can't get to the image's DOM and manipulate it via javascript. 如果您将SVG用作图像,则无法访问图像的DOM并通过javascript对其进行操作。

While you could load them using XMLHTTPRequest and then insert them into the main document as inline data using the DOMParser object, this exposes you to the security issues that the browsers are trying to protect you from by locking down image access ie the image could change and you may be loading arbitrary javascript into your page. 虽然您可以使用XMLHTTPRequest加载它们,然后使用DOMParser对象将它们作为内联数据插入到主文档中,但这使您面临浏览器试图通过锁定图像访问来保护您的安全问题,即图像可能会更改并您可能正在向页面中加载任意JavaScript。

What would seem simplest and safest to me is if you just alter the image files directly using an editor and add the filter into them then use the modified images. 在我看来,最简单,最安全的方法是直接使用编辑器更改图像文件,然后在其中添加过滤器,然后使用修改后的图像。

I don't know how to do it in the way you show... as a background image... but if you can load the svg file inside a div as in this example you can you can use my importer (a modified version of others importers founded on github) that it's here: http://www.dariomac.com/Document/Raphael-Utils . 我不知道该怎么做,在你带路......作为背景图像...但如果你可以在加载SVG文件一个div里面的这个例子中,你可以,你可以用我的进口商(修改版位于github上的其他进口商)位于: http : //www.dariomac.com/Document/Raphael-Utils You can also see this storify where I describe all the steps followed by me to import SVG directly from file. 您还可以看到这个storify ,我描述了所有其次是我直接从文件导入SVG的步骤。

No, it's not directly possible. 不,这不是直接可能的。 A workaround if you need this would be to use inline svg in html, or reference the svg files with either <object>, <iframe> or <embed>. 如果需要,一种解决方法是在html中使用嵌入式svg,或使用<object>,<iframe>或<embed>引用svg文件。

An example of using inline svg and a filter effect on hover here . 此处使用内联svg和对悬停的滤镜效果的示例。 The svg part looks like this essentially: svg部分基本上如下所示:

<svg width="400" height="400" viewBox="-2 -2 36 32">
  <defs>
    <style>
      #stack polygon:hover { filter: url(#glow); }
    </style>
    <filter id="glow">
        <feMorphology radius="0.7"/>
        <feGaussianBlur stdDeviation="1"/>
        <feColorMatrix type="matrix"
         values="0 0 0 0   0
                 0 0 0 0.9 0
                 0 0 0 0.9 0
                 0 0 0 1   0"/>
    </filter>
  </defs>
  <g id="stack" class="icon" fill="#850508">
    <polygon points="0,20 16,24 32,20 32,24 16,28 0,24"/>
    <polygon points="0,12 16,16 32,12 32,16 16,20 0,16"/>
    <polygon points="0,4 16,0 32,4 32,8 16,12 0,8"/>
  </g>
</svg>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM