SVG变量值的不透明度

问题描述 投票:0回答:1

当二进制变量设置为零时,我试图使svg元素动画消失(缓慢而优雅地),然后当二进制变量返回到1时再次出现(再次,好又慢)。我的图像消失了,但似乎无法弄清楚如何重新出现。这是JavaScript:

function go() {

    var max = 2;
    var random = Math.floor(Math.random() * Math.floor(max));;
    console.log("hi" + random);

    var elements = document.getElementsByTagName("animate");
    for (var i = 0; i < elements.length; i++) {
        if (random == 1) {
            elements[i].beginElement();
        }
        else {
            elements[i].endElement();

        }

    }
}

这是SVG元素

  <svg xmlns="http://www.w3.org/2000/svg" class="icons" width="40" height="40">
<g class="icon" transform="translate(-2 -2)">
  <path class="icon__sight" d="M38.255 20.182A16.353 16.353 0 0 0 23.818 5.745V2h-3.636v3.745A16.353 16.353 0 0 0 5.745 20.182H2v3.636h3.745a16.353 16.353 0 0 0 14.437 14.437V42h3.636v-3.745a16.353 16.353 0 0 0 14.437-14.437H42v-3.636h-3.745zM22 34.727A12.727 12.727 0 1 1 34.727 22 12.718 12.718 0 0 1 22 34.727z" fill="#fff"/>
  <circle class="icon__indicator" cx="8" cy="8" r="8" transform="translate(14 14)" fill="#88db29">
    <animate dur="1" attributeName="opacity" from="1" to="0" begin="move.end" repeatCount="0" fill="freeze" />
  </circle>/>
</g>

非常感谢任何提示或建议。善待,我是新来的:)

javascript svg jquery-animate opacity
1个回答
3
投票

这对于简单地转换SVG的不透明度来说似乎过于复杂。为什么不利用CSS通过JS操纵类来转换状态?

svg {
  transition: opacity 0.5s ease;
}

svg.hidden {
  opacity: 0;
}
var svg = document.querySelector('svg');
...
        if (random == 1) {
            svg.classList.remove('hidden');
        }
        else {
            svg.classList.add('hidden');

        }
© www.soinside.com 2019 - 2024. All rights reserved.