简体   繁体   English

如何使用 JavaScript 触发 SVG 动画? (没有jQuery)

[英]How to trigger an SVG animation with JavaScript? (No jQuery)

I have an SVG line path animated with this sample I found:我有一个使用我发现的示例动画的 SVG 线路径:

 svg { position: absolute; width: 100%; height: 100%; left: 0%; top: 0%; display: block; background: transparent; } .path { stroke: black; stroke-dasharray: 290; stroke-dashoffset: 130; animation: dash 6s 0s forwards infinite; stroke-width: 2px; stroke-linecap: round; stroke-linejoin: round; } @keyframes dash { from { stroke-dashoffset: 290; } to { stroke-dashoffset: 0; } }
 <svg viewbox="0 0 25 100" xmlns="http://www.w3.org/2000/svg"> <path class="path" d="M0 50 L 12 50, 12 0, 25 0" fill="transparent"></path> </svg>

It works fine but it's triggered when the page loads, is there a way (let's say with a button) to trigger this animation using Javascript?它工作正常,但在页面加载时触发,有没有办法(比如用按钮)使用 Javascript 触发这个动画?

I can handle JS but I'm a noob with CSS and SVG animations.我可以处理 JS,但我是 CSS 和 SVG 动画的菜鸟。

Can anybody give me an example of how can I make it with my actual CSS?谁能给我一个例子,说明如何使用我的实际 CSS 来制作它?

Thanks.谢谢。

You could also use SMIL animation syntax instead of CSS animation.您还可以使用 SMIL 动画语法代替 CSS 动画。 This admittedly has the downside of not running in Microsoft browsers (both IE and Edge).不可否认,这具有不能在 Microsoft 浏览器(IE 和 Edge)中运行的缺点。

 var animation = document.getElementById("dash"); function showSVG() { animation.beginElement(); }
 svg { position: relative; width: 100%; height: 150px; left: 0%; top: 0%; display: block; background: transparent; } .path { stroke: black; stroke-dasharray: 290; stroke-dashoffset: 290; stroke-width: 2px; stroke-linecap: round; stroke-linejoin: round; }
 <button id="button" onclick="showSVG()">Click</button> <svg id="svg" viewbox="0 0 25 100" xmlns="http://www.w3.org/2000/svg"> <path class="path" d="M0 50 L 12 50, 12 0, 25 0" fill="transparent"> <animate id="dash" attributeName="stroke-dashoffset" from="290" to="0" begin="indefinite" dur="6s" fill="freeze" /> </path> </svg>

This animation runs once every time you click.每次单击时,此动画都会运行一次。 If you want it to repeat in fixed intervals, like CSS animation-repeat: infinite prescribes, use如果您希望它以固定的间隔重复,例如 CSS animation-repeat: infinite prescribes,请使用

<animate id="dash" attributeName="stroke-dashoffset"
    from="290" to="0"
    begin="indefinite" dur="6s" repeatCount="indefinite" />
svg {
    position: absolute;
    width: 100%;
    height: 100%;
    left: 0%;
    top: 0%;
    display: block;
    background: transparent;
}

.path {
    stroke: black;
    stroke-dasharray: 290;
    stroke-dashoffset: 130;
    stroke-width: 2px;
    stroke-linecap: round;
    stroke-linejoin: round;
    stroke-dashoffset: 290;
}

.animated {
    animation: dash 6s 0s forwards infinite;
    stroke-dashoffset: 0;
}

@keyframes dash {
    from {
        stroke-dashoffset: 290;
    }
    to {
        stroke-dashoffset: 0;
    }
}

Then you can add the class .animated to your path with js whenever you need it.然后,您可以在需要时使用 js 将 .animated 类添加到您的路径中。

You should include your animation in a css class:您应该将动画包含在 css 类中:

    .dash-animate {
        animation: dash 6s 0s forwards infinite;
    }

    @keyframes dash {
      from {
        stroke-dashoffset: 290;
      }
      to {
        stroke-dashoffset: 0;
      }
    }

And then simply apply that class when/where you want using js:然后只需在需要使用 js 的时间/地点应用该类:

var button = getElementById('some-button');

button.addEventListner('click', function() {
  var elements = document.querySelectorAll('.path');
  Array.prototype.forEach.call(elements, function(el) {
    el.classList.add('dash-animate');
  });
});

 var svgPattern = document.getElementById("svg") svgPattern.style.display = "none"; function showSVG(){ svgPattern.style.display = "block"; }
 svg{ position:absolute; width:100%; height:100%; left:0%; top:0%; display:block; background:transparent; } .path { stroke:black; stroke-dasharray: 290; stroke-dashoffset: 130; animation: dash 6s 0s forwards infinite; stroke-width:2px; stroke-linecap:round; stroke-linejoin:round; } @keyframes dash { from { stroke-dashoffset: 290; } to { stroke-dashoffset: 0; } }
 <button id ="button" onclick="showSVG()">Click</button> <svg id="svg" viewbox="0 0 25 100" xmlns="http://www.w3.org/2000/svg"> <path class="path" d="M0 50 L 12 50, 12 0, 25 0" fill="transparent"></path> </svg>

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

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