Restart background SVG animation

后端 未结 7 2183
广开言路
广开言路 2021-01-01 20:20

I have an SVG set as a background image of an element. The first time the element is displayed, the animation plays correctly.

On subsequent displays (e.g. if a dupl

7条回答
  •  没有蜡笔的小新
    2021-01-01 20:46

    This has worked for me, Simply adding and removing "play" class to a div element by adding setTimeout to delay classList.add('play') so the class can be removed and then applied

    CSS

    @keyframes play60 {
            0% {
                background-position: 0px 0px;
            }
            100% {
                background-position: -38400px 0px;
            }
        }
        .shapeshifter {
            animation-duration: 1000ms;
            animation-timing-function: steps(60);
            animation-fill-mode: forwards;
            width: 640px;
            height: 640px;
            background-repeat: no-repeat;
        }
        .shapeshifter.play {
            animation-name: play60;
        }
    

    JS:

    document.getElementById('shape').addEventListener('mouseover', () => {
        document.getElementById('shape').classList.remove('play')
        setTimeout(() => {
            document.getElementById('shape').classList.add('play');
        }, 1)
    })
    

    HTML

提交回复
热议问题