为什么 anime.js 对 span 元素不起作用

why does anime.js not work on span elements

我正在尝试使用 anime.js 为 header 中的每个字母设置动画,方法是将 header 中的每个字母放入一个跨度中。但出于某种原因,我无法为跨度设置动画。为什么会发生这种情况,我该如何解决?

index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>Header anime animation</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <div id="wrapper">
        <header>
            <h1 id="mainheader">
                <span class="header-text" id="f">f</span><span class="header-text" id="o-1">o</span><span class="header-text" id="o-2">o</span>
            </h1>
        </header>
    </div>

    <script src="https://unpkg.com/animejs@3.2.1/lib/anime.min.js"></script>
    <script src="script.js"></script>
</body>
</html>

style.css

div#wrapper {
    height: 90vh;
    width: 100%;
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

h1#mainheader {
    position: relative;
    top: 50px;
}

script.js

anime({
    targets: '.header-text',
    translateY: -50,
    duration: 1000,
    delay: anime.stagger(100),
    easing: "easeInOutExpo",
    autoplay: true
})

这是因为 span 元素是行内元素,但 anime.js 仅适用于 display: blockdisplay: inline-block。所以在style.css中,只需添加

span.header-text {
    display: inline-block
}