javascript - 将 SVG 文件插入 HTML

标签 javascript html svg

我有 3 个文件:test.html、test.js 和 test.svg

我试图将不同的文件调用为 HTML,但文件 svg 不起作用

测试.html

<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Using SVG as an object</title>
    <link href="index.css" rel="stylesheet" />
</head>
<body>
    <h1>Test</h1>
    <script type="text/javascript" src="test.js"></script>


    <object data="test.svg" width="300" height="300"> </object>  <!-- Not working -->


    <input type="button" value="Start Animation" onclick="startAnimation();">
    <input type="button" value="Stop Animation" onclick="stopAnimation();"> 
</body>
</html>

测试.js

var timerFunction = null;

    function startAnimation() {
        if(timerFunction == null) {
            timerFunction = setInterval(animate, 20);
        }
    }

    function stopAnimation() {
        if(timerFunction != null){
            clearInterval(timerFunction);
            timerFunction = null;
        }
    }

    function animate() {
        var circle = document.getElementById("circle1");
        var x = circle.getAttribute("cx");
        var newX = 2 + parseInt(x);
        if(newX > 500) {
            newX = 20;
        }
        circle.setAttribute("cx", newX);
    }

测试.svg

<svg width="500" height="100">
    <circle id="circle1" cx="20" cy="20" r="10"
            style="stroke: none; fill: #ff0000;"/>
</svg>

我不明白为什么我无法插入带有对象的 svg 文件

感谢您的帮助

最佳答案

查看开发帖子:<load-file> Web Component


使用现代的、原生的 W3C 标准 Web 组件 <load-svg>

  • 它将 SVG 读取为文本
  • 将 SVG 作为 DOM 元素添加到 ShadowDOM
  • 样式元素从 lightDOM 移动到 shadowDOM
    因此样式应用于一个 SVG

<load-svg shadowRoot src="//graphviz.org/Gallery/directed/fsm.svg">
  <style>
    svg { height:150px } text { stroke: green } path { stroke: red ; stroke-width:3 }
  </style>
</load-svg>
<load-svg src="//graphviz.org/Gallery/directed/fsm.svg">
<!-- all HTML here is overwritten -->
</load-svg>

<script>
  customElements.define('load-svg', class extends HTMLElement {
    async connectedCallback() {
      this.style.display = 'none'; // prevent FOUC (provided Custom Element is defined ASAP!)
      let src = this.getAttribute("src");
      let svg = await (await fetch(src)).text();
      if (this.hasAttribute("shadowRoot")) {
        this.attachShadow({mode:"open"}).innerHTML = svg;
        this.shadowRoot.append(this.querySelector("style") || []);
      } else {
        this.innerHTML = svg;
      }
      this.style.display = 'inherit';
    }
  });
</script>

更复杂的示例:How to make an svg interactive to gather comments/annotations on depicted elements

关于javascript - 将 SVG 文件插入 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67718921/

相关文章:

javascript - 为什么 `true ||1 === 1/3` 在 JavaScript 中返回 true?

html - 关于需要特定 HTML 结构的混合的想法?

html - 图片上的链接在 Internet Explorer 中显示图片周围的边框

javascript - 使用 FabricJS 克隆组的问题(复制和粘贴)

javascript - 单击时显示幻灯片的不同片段

javascript - Angular 2.使用ngFor在对象内部循环数组

html - 如何使一个元素低于另一个 float 元素

javascript - 如何在 Rmarkdown 文档中使用 JavaScript 添加下载按钮

javascript - 使用 JS 缩放和居中 SVG

python - 如何获取(网络)字体中单个字符的大小?