【问题标题】:How do I reference a JS var in HTML?如何在 HTML 中引用 JS var?
【发布时间】:2018-10-09 23:07:07
【问题描述】:

我以前从未做过 JS,现在我只是在学习 CSS,但偶然发现了这个动画教程并想尝试一下。 http://goinkscape.com/how-to-animate-icons-with-inkscape-and-snap-svg/ 它不起作用:document.getElementById('iconDiv').innerHTML = s;,但没有奏效。 任何帮助表示赞赏。

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Inkscape Animated Icon Snap</title>
<!--We need to add the Snap.svg script to our document-->
        <script src="snap.svg-min.js"></script>
        <script>
//Run script right away
            window.onload = function () {
//We'll be appending the icon to this DIV later
                var s = Snap("#iconDiv");
//Have Snap load the SVG file
                Snap.load("icon.svg", function(f) {
//Assign the white rectangle
                    whiteRect = f.select("#whiteRect");
//Assign the whole icon group
                    icon = f.select("#icon");
//When the icon is hovered over, have the white rectangle move up slightly with elastic properties
                    icon.hover(function() {
                        whiteRect.animate({y:960}, 500, mina.elastic);
                    },
//And return to original position when not hovered over
                               function() {
                        whiteRect.animate({y:977.36218}, 500, mina.elastic);
                    }
                    );
//Finally append the icon to iconDiv in the body
                s.append(f);
                });          
            };
        </script>
    </head>
    <body>
<!--Here's the DIV that will hold the animated SVG icon-->
<div id="iconDiv"></div>        
    </body>
</html>

我回去又做了一次如下:

  1. 制作了一个 100x100 像素的蓝色方块。设置 ID:blueRect,标签:#blueRect。
  2. 制作了一个 50x50 像素的白色方块。设置 ID:whiteRect,标签:#whiteRect。
  3. 将它们分组并设置 ID:icon,标签:#icon。
  4. 将 icon.svg 作为普通 SVG 保存在我的 index.html 文件所在的同一文件夹中。
  5. 已下载 Snap.svg 并将 snap.svg-min.js 放在我的 index.html 文件所在的同一文件夹中。
  6. 在记事本中打开 icon.svg:

    "id="whiteRect" 宽度=“13.229167” 高度=“13.229167” x="6.6145835" y="277.15625"

我不知道为什么这些是数字(与我第一次试验时相同),因为这次我专门将我的方格放在 0,0 处……我什至保存了一个不同名称的副本。仍然得到这些数字,但无论如何......

  1. 直接从网站复制粘贴html代码,先把y改成277.15625,再改成0。

  2. 将另一个 y 设置为 270,然后设置为 -15。

icon.hover(函数() { whiteRect.animate({y:-15}, 500, mina.elastic);

两次,都是空白页。

【问题讨论】:

  • 快速投反对票,但对我的问题甚至没有丝毫暗示......真的很有帮助......
  • 你的投票被否决了,因为你还没有发布 Minimal, Complete, and Verifiable example
  • @bejado 您可以在 cmets 中键入 [mcve] 并呈现相同的效果。 :)
  • @Raksha — 您需要在问题本身中提供minimal reproducible example(不是所有代码的复制/粘贴,只是演示问题所需的内容)以及 a明确的问题陈述(“它不起作用”不是)。做一些调试。引用浏览器开发者工具控制台中显示的任何消息。使用“网络”选项卡确保正在加载所有外部文件。等
  • 教程没有错。根据您提供的信息,我们无法判断您做错了什么。

标签: javascript html animation svg inkscape


【解决方案1】:

您查看控制台了吗?您的控制台是否显示以下错误?

Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

这是由您尝试通过 html 加载本地文件的行 &lt;script src="snap.svg-min.js"&gt;&lt;/script&gt; 引起的。如果人们可以将他们的本地文件加载到网站上,那将是一个安全风险。要解决此问题,请运行启用了 cors(跨源资源共享)的 HTTP 服务器。这里我使用的是可以通过 npm 安装的 http-server。我正在监听端口 8000,并从与文件相同的目录(注意句点)运行服务器。

http-server . -o --cors -p 8000

另外,在您的 index.html 中,您需要将所有对本地目录的引用更改为 http://localhost:8000/&lt;file name here&gt;。之后,您无需在本地打开 index.html,而是浏览到 http://127.0.0.1:8000/ 来查看动画。这对我有用,至少在 IE 上。 Chrome 似乎对 cors 更严格,我还没有弄清楚如何通过它。我没试过火狐。

【讨论】:

  • 我不知道在哪里可以看到这些消息。我正在使用 PyCharm 进行编辑,因为这是我除了记事本之外唯一拥有的东西 >.> ...而且我无法在 PyCharm <. html ei .... cuz chrome ... activex>
  • 感谢您的帮助。至少现在我知道我没有疯XD ...现在要弄清楚你所说的一切,因为我真的不知道这意味着什么+.+ ...但至少现在我大致知道问题出在哪里是。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-18
  • 2020-03-02
相关资源
最近更新 更多