【问题标题】:javascript animated SVG not working in Wordpressjavascript动画SVG在Wordpress中不起作用
【发布时间】:2021-09-15 00:06:22
【问题描述】:

我的 javascript 动画 SVG 无法在 WordPress 中运行。任何人都可以提供如何制作动画 SVG 的最佳实践吗?

我的第一个想法是 - 它是否因为在 SVG 文件中包含 javascript 的安全性而无法正常工作...

文件按原样工作: https://aau.gagarindigital.dk/wp-content/uploads/2021/07/stack-test-menu_3x.svg

我的 SVG 文件如下所示:

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 912.83 882.07">

   <defs>
    <style>
 :root {
        --color-1: #F2B705;
        --color-2: #C30F0E;
        --color-3: #264ABA;
        --color-4: #009CD2;
        --color-5: #E25727;

        
        --color-11: #C29204;
        --color-22: #990B0B;
        --color-33: #172E73;
        --color-44: #006B8E;
        --color-55: #BA4620;
  }
#c1 {
  fill: var(--color-1);
}
#c2 {
  fill: var(--color-2);
}
#c3 {
  fill: var(--color-3);
}

#c4 {
  fill: var(--color-4);
}

#c5 {
  fill: var(--color-5);
}
        .cls-7 {
        fill: #a1ac75;
      }
        
circle:hover:not(.active) {
  stroke: #999;
  stroke-width: 11.5;
}

circle.active {
  stroke: black;
  stroke-width: 11.5;
}

line {
  stroke: gold;
  stroke-width: 4;
}

line.highlight {
  stroke: black;
}
    </style>
    
  </defs>
    <polygon id="fem-kant" class="cls-7" points="456.29 7.49 898.87 329.05 729.82 849.33 182.76 849.33 13.71 329.05 456.29 7.49"/>

      <line class="c3 c5" x1="656.32" y1="748.57" x2="133.5" y2="368.62"/>
      <line class="c2 c4" x1="258.55" y1="747.22" x2="779.33" y2="368.43"/>
      <line class="c1 c4" x1="258.55" y1="747.22" x2="457.43" y2="133.5"/>
      <line class="c1 c3" x1="457.43" y1="133.5" x2="657.03" y2="748.57"/>
      <line class="c5 c2" x1="133.5" y1="368.62" x2="779.33" y2="368.43"/>
      <line class="c1 c2" x1="779.33" y1="368.43" x2="457.43" y2="133.5"/>
      <line class="c2 c3" x1="779.33" y1="368.43" x2="657.03" y2="748.57"/>
      <line class="c3 c4" x1="258.55" y1="747.22" x2="657.03" y2="748.57"/>
      <line class="c4 c5" x1="258.55" y1="747.22" x2="133.5" y2="368.62"/>
      <line class="c5 c1" x1="133.5" y1="368.62" x2="457.43" y2="133.5"/>

  <a href="#0">
      <circle id="c1" cx="455.9" cy="133.5" r="133"/>
        </a>
  <a href="#0">
      <circle id="c2" cx="779.33" cy="368.43" r="133"/>
      </a>
  <a href="#0">
      <circle id="c3" cx="656.32" cy="748.57" r="133"/>
      </a>
  <a href="#0">
      <circle id="c4" cx="256.18" cy="748.57" r="133"/>
      </a>
  <a href="#0">
      <circle id="c5" cx="133.5" cy="368.62" r="133"/>
      </a>

<script>
<![CDATA[
var  allCircles = document.querySelectorAll("circle");

window.addEventListener('DOMContentLoaded', (event) => {

    // Add an click handler to every circle that
    // adds the class "active" to the clicked circle.
    allCircles.forEach(element => {
        element.addEventListener("click", clickHandler);
        element.addEventListener("mouseover", mouseoverHandler);
        element.addEventListener("mouseout", mouseoutHandler);
    });
    
});

function  clickHandler(evt) {
    // Clear current selection (remove class "active" from any circle)
    allCircles.forEach((circle) => circle.classList.remove("active"));
    // Mark clicked circle selected
    evt.target.classList.add("active");
    // Clear any currently highlighted lines
    clearHighlightedLines();
}

function  mouseoverHandler(evt) {
    let activeCircle = document.querySelector("circle.active");
    let hoveredCircle = evt.target;
    if (activeCircle && (activeCircle != hoveredCircle)) {
        // Get the line that has classes matching both the actibve and hovered circle
        let line = document.querySelector("line."+activeCircle.id+"."+hoveredCircle.id);
        // Add the class "highlight" to that line
        if (line)
            line.classList.add("highlight");
    }
}

function  mouseoutHandler(evt) {
    clearHighlightedLines();
}

function  clearHighlightedLines() {
    // Find the line with class "highlight" (if any)
    var line = document.querySelector("line.highlight");
    // Remove the class "highlight"
    if (line)
        line.classList.remove("highlight");
}
    //]]>
    </script>
</svg>

但是 javascript 不起作用,并且 SVG 文件在 WordPress 中看起来像这样: https://aau.gagarindigital.dk/sample-page/

如果脚本应该在 SVG 文件之外 - 我可以链接到放置在站点根目录的脚本吗? SVG 文件中的链接是什么样的?

【问题讨论】:

    标签: javascript wordpress svg


    【解决方案1】:

    我在my answer to your last question 中犯了一个小错误。

    var  allCircles = document.querySelectorAll("circle");
    

    部分应该在DOMContentLoaded 事件内。我相信如果你能解决这个问题,它会有所作为。

    【讨论】:

    • 嗨,波尔。谢谢你在那里。没有动画停止使用该更改。
    • 我们可能会碰壁,我可以提供帮助,因为我不熟悉 WordPress 的细节。根据页面中包含 SVG 的方式,您可能需要以不同的方式初始化脚本。当我手动编辑实时页面时,它开始工作。所以看起来脚本没有在正确的时间初始化。
    • 现在正在加载脚本。我对 WordPress 是问题的担忧现在已经消除。 SVG 文件被放置为内联,而 javascript 和 css 被放置在 SVG 文件之外。这一切都很好。当“#0”替换为真正的“/zxy”链接时,javascript 处理链接的方式有所不同——知道为什么吗?我希望能够为活动的圆圈(=活动页面)设置样式。我正在尝试这个:element.addEventListener('focus', (event) =&gt; { event.target.style.border = ‘black; }, true); ...但这并没有成功。我该怎么做?
    • 没有必要使用&lt;a&gt;元素。它们带有自己的默认浏览器行为,可能会干扰您自己的行为。只需将点击处理程序直接应用于圈子。或者,如果您需要环绕圆圈,您可以随时将 &lt;a&gt; 替换为简单的组 &lt;g&gt; 元素。
    • 如果您使用&lt;a&gt; 的原因是为了获取手指指针图标,那么您可以将cursor: pointer 添加到圆形CSS 中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-23
    • 2013-02-18
    • 2015-09-05
    • 2013-02-24
    • 2017-10-10
    • 2016-01-19
    • 2018-02-26
    相关资源
    最近更新 更多