初识 Vue(28)---(Vue 中的动画封装)

简介: Vue 中的动画封装功能:点击按钮,hello world 出现,再点击隐藏 Vue 中的动画封装 .

Vue 中的动画封装

功能:点击按钮,hello world 出现,再点击隐藏

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Vue 中的动画封装</title>
    <script src = './vue.js'></script>
    <style>
     .v-enter,.v-leave-to{
      opacity:0;
     }
     .v-enter-active,.v-leave-active{
      transition: opacity 1s;
     }
   </style>
</head>
<body>
    <div id ='root'>
      <transition>
        <div v-show="show" >
        hello world
        </div>
      </transition>
      <button @click="handleBtnClick">toggle</button>
    </div>

    <script>
   
  var vm = new Vue({
        el:"#root",
        data:{
            show:false
        },
        methods:{
            handleBtnClick:function(){
             this.show = !this.show   
                }
            }
        })
    
        
  
    </script>   
</body>
</html>

输出:点击--出现--隐藏

                  

封装这个动画功能

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Vue 中的动画封装</title>
    <script src = './vue.js'></script>
    <style>
     .v-enter,.v-leave-to{
      opacity:0;
     }
     .v-enter-active,.v-leave-active{   
      transition: opacity 1s;
     }
   </style>
</head>
<body>
    <div id ='root'>
      
     <fade  :show="show"> //等于父组件的变量  
        <div>   
        hello world
        </div>
     </fade>
      <button @click="handleBtnClick">toggle</button>
    </div>

    <script>
    Vue.component('fade',{
      props:['show'],
      template:`
        <transition>
            <slot v-if="show"> </slot>     //判断外部传来的DOM是否要被显示
        </transition>
        ` 
    })
    
  var vm = new Vue({
        el:"#root",
        data:{
            show:false
        },
        methods:{
            handleBtnClick:function(){
             this.show = !this.show   
                }
            }
        })
    
        
  
    </script>   
</body>
</html>

输出:点击--出现--隐藏

                  

 

封装的好处:不需再设置动画样式,直接输入内容就好

<body>
    <div id ='root'>
      
     <fade  :show="show"> 
        <div>   
        hello world
        </div>
     </fade>

     <fade  :show="show"> 
        <h1>   
        hello world
        </h1>
     </fade>
      <button @click="handleBtnClick">toggle</button>
    </div>

输出:

        

 

可以将 样式 也封装进去,只能使用 JS 动画实现(推荐)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Vue 中的动画封装</title>
    <script src = './vue.js'></script>
    <!-- <style>
     .v-enter,.v-leave-to{
      opacity:0;
     }
     .v-enter-active,.v-leave-active{
      transition: opacity 1s;
     }
       </style> -->
</head>
<body>
    <div id ='root'>
      
     <fade  :show="show"> 
        <div>   
        hello world
        </div>
     </fade>

     <fade  :show="show"> 
        <h1>   
        hello world
        </h1>
     </fade>
      <button @click="handleBtnClick">toggle</button>
    </div>

    <script>
    Vue.component('fade',{
      props:['show'],
      template:`
        <transition @before-enter="handleBeforeEnter"
        @enter="handleEnter"
        >
        <slot v-if="show"> </slot>    
        </transition>
        ` ,
        methods:{
          handleBeforeEnter:function(el){
              el.style.color = 'red'
          },
          handleEnter:function(el,done){
            setTimeout(() =>{
                el.style.color = 'green'
                done()
            },2000)
          }
        }
    })

  var vm = new Vue({
        el:"#root",
        data:{
            show:false
        },
        methods:{
            handleBtnClick:function(){
             this.show = !this.show   
                }
            }
        })
    
        
  
    </script>   
</body>
</html>

输出:点击---显示---2秒后变绿色(将样式都封装到 fade 组件里面,外部只需调用 fade 组件即可)

     

目录
相关文章
|
3天前
|
JavaScript
vue页面加载时同时请求两个接口
vue页面加载时同时请求两个接口
|
2天前
|
移动开发 前端开发
vue3 Element-Plus封装的el-tree-select的使用
vue3 Element-Plus封装的el-tree-select的使用
vue3 Element-Plus封装的el-tree-select的使用
|
2天前
|
JavaScript
vue打印v-model 的值
vue打印v-model 的值
|
3天前
|
JavaScript
Vue实战-组件通信
Vue实战-组件通信
5 0
|
3天前
|
JavaScript
Vue实战-将通用组件注册为全局组件
Vue实战-将通用组件注册为全局组件
5 0
|
3天前
|
JavaScript 前端开发
vue的论坛管理模块-文章评论02
vue的论坛管理模块-文章评论02
|
3天前
|
JavaScript Java
vue的论坛管理模块-文章查看-01
vue的论坛管理模块-文章查看-01
|
前端开发 JavaScript
初识 Vue(24)---(Vue 中同时使用过渡和动画)
Vue 中同时使用过渡和动画 在上篇博客 《Vue 中使用 animate.css 库》基础上开始这篇博客 在上篇博客中,完成了 引入 animate.
1215 0
|
前端开发 内存技术
Vue_同时使用过渡和动画
在上一节我们用animate动画库,在刷新页面时没有动画 如何解决第一次就显示动画内容呢? 在transform 上加上appear 和appear-active-class <transition name='fade' appear enter-active-class='animate.
1531 0
|
3天前
|
JavaScript
VUE里的find与filter使用与区别
VUE里的find与filter使用与区别
13 0