vue2.0-transition配合animate.css

本文展示了一个使用Vue.js实现元素显示隐藏动画过渡效果的例子。通过点击按钮切换元素的显示状态,并利用animate.css库来定义不同的动画效果。该示例包括了如何设置Vue实例、绑定事件处理函数以及配置动画类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>智能社——http://www.zhinengshe.com</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <style>
        p{
            width:100px;
            height:100px;
            background: red;
            margin:0 auto;
        }
    </style>
    <script src="vue.js"></script>
    <link rel="stylesheet" href="animate.css">
    <script>
        window.onload=function(){
            new Vue({
                el:'#box',
                data:{
                    show:false
                }
            });
        };
    </script>
</head>
<body>
    <div id="box">
        <input type="button" value="点击显示隐藏" @click="show=!show">

        <transition enter-active-class="bounceInLeft" leave-active-class="bounceOutRight">
            <p v-show="show" class="animated"></p>
        </transition>
    </div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>智能社——http://www.zhinengshe.com</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <style>
        p{
            width:100px;
            height:100px;
            background: red;
            margin:0 auto;
        }
    </style>
    <script src="vue.js"></script>
    <link rel="stylesheet" href="animate.css">
    <script>
        window.onload=function(){
            new Vue({
                el:'#box',
                data:{
                    show:false
                }
            });
        };
    </script>
</head>
<body>
    <div id="box">
        <input type="button" value="点击显示隐藏" @click="show=!show">

        <transition enter-active-class="zoomInLeft" leave-active-class="zoomOutRight">
            <p v-show="show" class="animated"></p>
        </transition>
    </div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>智能社——http://www.zhinengshe.com</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <style>
        p{
            width:100px;
            height:100px;
            background: red;
            margin:0 auto;
        }
    </style>
    <script src="vue.js"></script>
    <link rel="stylesheet" href="animate.css">
    <script>
        window.onload=function(){
            new Vue({
                el:'#box',
                data:{
                    show:false
                }
            });
        };
    </script>
</head>
<body>
    <div id="box">
        <input type="button" value="点击显示隐藏" @click="show=!show">

        <transition enter-active-class="animated zoomInLeft" leave-active-class="animated zoomOutRight">
            <p v-show="show"></p>
        </transition>
    </div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>智能社——http://www.zhinengshe.com</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <style>
        p{
            width:100px;
            height:100px;
            background: red;
            margin:0 auto;
        }
    </style>
    <script src="vue.js"></script>
    <link rel="stylesheet" href="animate.css">
    <script>
        window.onload=function(){
            new Vue({
                el:'#box',
                data:{
                    show:false
                },
                transitions:{ //错的
                    enterActiveClass:'zoomInLeft',
                    leaveActiveClass:'zoomOutRight'
                }
            });
        };
    </script>
</head>
<body>
    <div id="box">
        <input type="button" value="点击显示隐藏" @click="show=!show">

        <transition name="transitions">
            <p v-show="show" class="animated"></p>
        </transition>
    </div>
</body>
</html>

 

### Vue3 中使用 Transition 组件与 Animate.css 结合实现动画 #### 安装 Animate.cssVue3 项目中使用 `animate.css`,首先需要通过 npm 或 yarn 来安装此库: ```bash npm install animate.css --save ``` 或者 ```bash yarn add animate.css ``` #### 导入 Animate.css 文件 完成安装之后,在项目的入口文件(通常是 main.js 或者 vite.config.ts 如果使用 Vite 构建工具),添加以下代码来全局引入 `animate.css`: ```javascript import 'animate.css'; ``` 这一步骤确保整个应用程序都可以访问到 `animate.css` 提供的所有动画效果。 #### 创建带有过渡效果的组件 创建一个新的 Vue 组件,并在其内部定义一个布尔类型的响应式属性用于控制显示/隐藏状态。接着应用 `<transition>` 标签包裹目标 DOM 节点,并指定进入和离开时所需的 CSS 类名[^2]。 下面给出一段完整的示例代码展示如何在一个按钮点击事件触发下切换表情符号的可见性以及伴随淡入淡出的效果[^3]: ```html <template> <button @click="toggle">Toggle</button> <!-- 使用 transition 包裹要执行动画的目标 --> <Transition name="custom-classes-transition" :enter-active-class="'animate__animated animate__fadeIn'" :leave-active-class="'animate__animated animate__fadeOut'" > <p v-if="show"> Hello, world! {{ emoji }} </p> </Transition> </template> <script setup lang="ts"> import { ref } from 'vue'; const show = ref(false); let emoji = ref('😊'); function toggle() { show.value = !show.value; } </script> ``` 在这个例子中,当点击按钮改变 `show` 变量的状态时,会触发表情文字区域按照设定好的方式逐渐显现或消失。这里特别需要注意的是对于版本大于等于 V4 的 `animate.css`,除了基本的动画类之外还需要额外加上 `animate__animated` 前缀才能让动画正常工作[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值