为什么anime.js 不适用于跨度元素

问题描述

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

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
}