javascript - SVG 填充背景图像

标签 javascript css svg svg-filters svg-animate

所以我已经阅读了很多问题,但到目前为止没有一个对我有用。

      <svg height="100%" width="100%" class="myclass">
        <polygon points="0,0 513,0 0,513" style="fill:red;" />
      </svg>

所以我的 svg 是红色的。但是,我想将填充更改为图像。

问题是,我希望能够覆盖此图像。例如,onClick 更改 SVG 的背景图像。

我一直在挣扎。

谢谢!

最佳答案

试试这个 https://jsfiddle.net/2Lzo9vfc/62/

HTML

<svg height="100%" width="100%" class="hover">
   <polygon class="shape" points="0,0 513,0 0,513" style="fill :red;" />
</svg>

<svg height="100%" width="100%" class="click">
   <polygon class="shape" points="0,0 513,0 0,513" style="fill :red;" />
</svg>

JS

$('.hover').hover(function(){
  $(this).find('polygon').css('fill', 'black');
},function(){
    $(this).find('polygon').css('fill', 'red');
});

$('.click').click(function() {
    $(this).find('polygon').css('fill', 'black');
});

第一个例子是悬停时的变化,第二个是点击时的变化。

关于javascript - SVG 填充背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33651001/

相关文章:

javascript - javascript中的语法高亮

html - 如何在不改变布局的情况下居中表格

html - 有没有办法避免 CSS 类名冲突?

javascript - 如何使用此 js svg 解析器将 svg 文件或元素中路径的小数位更改为 n 位小数?

javascript - 文本未转换为正确的 svg 代码 - Opentype Javascript

javascript - html onclick加载外部javascript不起作用

javascript - 区分文件下载和页面更改的 onbeforeunload

html - SVG 在 Chrome 中无法正确缩放

javascript - react Redux : Reload the component from ACTION when the props change

html - 使用 DIV 容器作为带有图像的平面按钮