animeJS 实现页面的简单动画

766 阅读1分钟

效果如下

动画3.gif

运用animeJS,可以很简单的实现页面中元素动画的控制

源码


页面html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
</head>

<body>
    <section>
        <img src="stars.png" id="stars">
        <img src="moon.png" id="moon">
        <img src="mountains_behind.png" id="mountains_behind">
        <h2 id="text">Anime<span>JS</span></h2>
        <a href="#" id="btn">Play</a>
        <img src="mountains_front.png" id="mountains_front">
    </section>


    <script src="anime.min.js"> </script>
    <script>
        // 使用默认参数创建时间轴
        var animation = anime.timeline({
            autoplay: false
        });

        var time = 500;

        animation
            .add({
                targets: '#btn',
                top: '2000px',
                duration: 500,
                easing: 'easeInOutSine',
            })

            .add({
                targets: '#stars',
                top: '0px',
                duration: time,
                easing: 'easeInOutSine',
            })

            .add({
                targets: '#mountains_behind',
                bottom: '0px',
                duration: time,
                easing: 'easeInOutSine',
            })

            .add({
                targets: '#moon',
                top: '0px',
                duration: time,
                easing: 'easeInOutBack',
            })

            .add({
                targets: '#mountains_front',
                bottom: '0px',
                duration: time,
                easing: 'easeInOutSine',
            })

            .add({
                targets: '#text',
                marginRight: '0px',
                duration: time,
                easing: 'easeInOutBack',
            })

            .add({
                targets: '#btn',
                top: '50%',
                duration: time,
                easing: 'easeInOutBack',
            })



        document.querySelector('#btn').onclick = animation.play
    </script>
</body>

</html>

css样式

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;

}

body {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    background: linear-gradient(#2b1055,#7597de);
}

section{
    position: relative;
    width: 100%;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;  /* 溢出隐藏 */
}
section img {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    pointer-events: none;
}

section img#moon {
    top: -100%;
    mix-blend-mode: screen; /*描述了元素的内容应该与元素的直系父元素的内容和元素的背景如何混合。 */
}

section img#stars {
    top: -100%;
    height: 100%;
    object-fit: cover; /* 图片在div中的展示方式 */
   
}

section img#mountains_behind {
    bottom: -100%;
    top: inherit;

}

section img#mountains_front {
    bottom: -100%;
    top: inherit;
    z-index: 100;
}

#text { 
    position: absolute;
    color: #fff;
    font-size: 8vw;
    font-weight: 200;
    z-index: 200;
    margin-right: -3000px;
}

#text span{ 
    font-weight: 700;
}
#btn {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    margin-top: 100px;
    text-decoration: none; /*下划线 无 */
    display: inline-block;
    padding: 8px 50px;
    background: #fff;
    border-radius: 40px;
    font-size: 1.4em;
    color: #2b1055;
    z-index: 200;
}