VUE3 之 使用标签实现动画与过渡效果(下) - 这个系列的教程通俗易懂,适合新手

1. 概述

毛毛虫效应:

有这样一个实验,将许多毛毛虫放在一个花盆边缘,使它们首尾相接,围成一个圈。然后在离花盆很近的地方撒了一些毛毛虫的食物。

此时,毛毛虫并不会向食物的方向爬去,而是在花盆边缘,一个跟着一个的转圈,直到7天后,因为饥饿和精疲力尽相继死去。

这是毛毛虫的一种特性吧,它们喜欢跟随前者的路线行走,这也是固化思维的一种表现。

因此解放思维,有创新精神是很重要的。

 

言归正传,今天我们继续聊聊使用标签实现动画与过渡的效果的相关知识。

 

2. 使用标签实现动画与过渡的效果

 

2.1 推荐两个不错的动画相关的网站 

2.1.1 Animate.css

网站地址:https://animate.style/

里面有很多不错的、封装好的 css 样式供使用,省了程序员很多开发工作量

 

2.1.2 greensock

网站地址:https://greensock.com

此网站的主产品 GSAP ,是一套脚本动画制作工具,是对 JavaScript 的封装,解决了无数浏览器的兼容性问题,比 jQuery 快 20 倍。

 

2.2 自定义样式名称

之前我们通过在 transition 标签上增加 name 属性,来自定义样式的名称,这种做法不够灵活,我们换一种更加灵活的写法,样式的名称可以完全自定义

<style>
        @keyframes leftRight {
            /* 进度为 X% 时,元素的横坐标位置 */
            0% {
                transform: translateX(0px);
            }
            33% {
                transform: translateX(-100px);
            }
            66% {
                transform: translateX(100px);
            }
            100% {
                transform: translateX(0px);
            }
        }

        /* 居中 */
        .center {
            text-align: center;
        }

        /* 入场起始样式 */
        .my-en-from {
            opacity: 0;  /* 透明度 */
        }
        /* 入场过渡效果 */
        .my-en-active {
            transition: 4s opacity ease-out;
            animation : leftRight 4s;
        }
        /* 入场结束样式 */
        .my-en-to {
            opacity: 1;
        }

        /* 出场起始样式 */
        .my-leave-from {
            opacity: 1;  /* 透明度 */
        }
        /* 出场过渡效果 */
        .my-leave-active {
            transition: 4s opacity ease-out;
            animation : leftRight 4s;
        }
        /* 出场结束样式 */
        .my-leave-to {
            opacity: 0;
        }

 

<body>
    <div id="myDiv"></div>
</body>
<script>
    const app = Vue.createApp({
        data(){
            return {
                show : false
            }
        },
        methods : {
            myClick() {
                this.show = !this.show;
            }

        },
        template:`
            <div class="center">
                <transition 
                    enter-from-class="my-en-from" 
                    enter-active-class="my-en-active" 
                    enter-to-class="my-en-to" 
                    leave-from-class="my-leave-from" 
                    leave-active-class="my-leave-active"
                    leave-to-class="my-leave-to"
                >
                    <div v-show="show">hello world</div>
                </transition>
                <button @click="myClick">切换</button>
            </div>
        `
    });

    const vm = app.mount("#myDiv");

在 transition 标签中指定样式

enter-from-class:入场起始样式

enter-active-class:入场过渡效果

enter-to-class:入场结束样式

leave-from-class:出场起始样式

leave-active-class:出场过渡效果

leave-to-class:出场结束样式

 

2.3  Animate.css 例子

<link
        rel="stylesheet"
        href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css"
    />
<script src="../vue.global.js"></script>

 

<body>
    <div id="myDiv"></div>
</body>
<script>
    const app = Vue.createApp({
        data(){
            return {
                show : false
            }
        },
        methods : {
            myClick() {
                this.show = !this.show;
            }

        },
        template:`
            <div class="center">
                <transition 
                    enter-active-class="animate__animated animate__backInDown" 
                    leave-active-class="animate__animated animate__backOutUp"
                >
                    <div v-show="show"><h1>hello world</h1></div>
                </transition>
                <button @click="myClick">切换</button>
            </div>
        `
    });

    const vm = app.mount("#myDiv");

这个例子中,我们把入场、出场的样式直接指定为 Animate.css 的样式,不需要自己去写了

 

2.4 使用 greensock JavaScript 框架实现动画

<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.9.1/gsap.min.js"></script>

 

    <style>
        body {
            margin: 30px;
        }

        .box {
            width: 100px;
            height: 20px;
            background: blue;
            margin-top: 20px;
        }
    </style>

 

const app = Vue.createApp({
        data(){
            return {
                show : false
            }
        },
        methods : {
            myClick() {
                this.show = !this.show;
            },
            myBeforeEnter(element) { 
                gsap.set(element, {
                    scaleX: 0.8,
                    scaleY: 1.2
                });
            },
            myEnter(element, done) { 
                gsap.to(element, {
                    duration: 1,
                    scaleX: 1.5,
                    scaleY: 0.7,
                    opacity: 1,
                    x: 150,
                    ease: 'elastic.inOut(2.5, 1)',
                    onComplete: done
                });
            },
            myAfterEnter(element) {

            },
            myBeforeLeave(element) {

            },
            myLeave(element, done) {
                gsap.to(element, {
                    duration: 0.7,
                    scaleX: 1,
                    scaleY: 1,
                    x: 300,
                    ease: 'elastic.inOut(2.5, 1)'
                });
                gsap.to(element, {
                    duration: 0.2,
                    delay: 0.5,
                    opacity: 0,
                    onComplete: done
                });
            },
            myAfterLeave(element) {
                
            }

        },

        template:`
            <div>
                <button @click="myClick">切换</button>
                <transition 
                    :css="false"
                    @before-enter="myBeforeEnter" 
                    @enter="myEnter" 
                    @after-enter="myAfterEnter" 
                    @before-leave="myBeforeLeave" 
                    @leave="myLeave" 
                    @after-leave="myAfterLeave" 
                >
                    <div v-if="show" class="box">hello world</div>
                </transition>
                
            </div>
        `

这个例子里面有两个知识点。

一个是在 transition 标签中使用 JavaScript 实现动画时,需要使用 :css="false" 声明css失效,然后使用 钩子函数 实现动画。

另一个知识点就是在 钩子函数 中使用 gsap 框架,实现动画。

 

2.5 使用 type 属性解决动画和过渡时间不一致问题

有这么一种情况,动画的时间或过渡的时间比较长,我们希望两者的时间一致,可以这么做

         /* 入场起始样式 */
        .v-enter-from {
            color:blue;
        }
        /* 入场过渡效果 */
        .v-enter-active {
            transition: 10s all ease-in; /* all代表任何属性改变都过渡 */
            animation : leftRight 4s;
        }
        /* 入场结束样式 */
        .v-enter-to {
            color:red;
        }

        /* 出场起始样式 */
        .v-leave-from {
            color:red;
        }
        /* 出场过渡效果 */
        .v-leave-active {
            transition: 10s all ease-out;
            animation : leftRight 4s;
        }
        /* 出场结束样式 */
        .v-leave-to {
            color:blue;
        }

 

        template:`
            <div class="center">
                <transition type="animation">
                    <div v-show="show"><h3>hello world</h3></div>
                </transition>
                <button @click="myClick">切换</button>
            </div>
        `

在 transition 标签使用 type=“animation”,指定以 animation 的时间为准,这个例子中,动画和过渡都会在 4 秒完成

 

2.6 使用 duration 属性解决动画和过渡时间不一致问题

        template:`
            <div class="center">
                <transition :duration="1000">
                    <div v-show="show"><h3>hello world</h3></div>
                </transition>
                <button @click="myClick">切换</button>
            </div>
        `

使用 :duration 指定运行时间,单位毫秒,:duration="1000" 代表动画和过渡都会在1秒结束

 

2.7 duration 属性使用表达式

        template:`
            <div class="center">
                <transition :duration="{enter:2000, leave:5000}">
                    <div v-show="show"><h3>hello world</h3></div>
                </transition>
                <button @click="myClick">切换</button>
            </div>
        `

:duration="{enter:2000, leave:5000} 代表进场动画和过渡都是2秒,出场都是5秒

 

3. 综述

今天聊了一下 VUE3 中使用标签实现动画与过渡的效果的另一部分知识,希望可以对大家的工作有所帮助,下一节我们继续讲 Vue 中 动画 的相关知识,敬请期待

欢迎帮忙点赞、评论、转发、加关注 :)

关注追风人聊Java,这里干货满满,都是实战类技术文章,通俗易懂,轻松上手。

 

4. 个人公众号

追风人聊Java,欢迎大家关注

posted @ 2022-03-06 22:13  追风人聊Java  阅读(606)  评论(0编辑  收藏  举报