search
HomeWeb Front-endJS TutorialDetailed explanation of examples of animation transition effects for Vue elements

This article mainly introduces the vue element to achieve animation Transition effect. The editor thinks it is quite good. Now I will share it with you and give it a reference. Let’s follow the editor and take a look.

1 In vue, use the <transition></transition> tag to contain a single sub-element using v-show or v-if Before switching the display and hiding, it will first determine whether there is a corresponding class style that can match the sub-element:

<script src="/public/javascripts/vuejs"></script>
<style>
  red {background-color: red; width: 100px; height: 100px;}
  redv-leave { margin-top: 50px; }
  redv-leave-active { transition: all 3s;}
  redv-leave-to { margin-top: 100px; opacity: 0;}
  redv-enter { margin-top: 50px; }
  redv-enter-active { transition: all 3s;}
  redv-enter-to { margin-top: 10px; opacity: 0;}
</style>
<body>
<p id="app">
  <transition>
    <p class="red" v-show="show"></p>
  </transition>
  <button v-on:click="change">button</button>
</p>
<script>
new Vue({
  el: &#39;#app&#39;,
  data: {
    show: true
  },
  methods: {
    change: function(){
      thisshow = !thisshow;
    }
  }
});
</script>
</script>
</body>

  1. v-leave The current element is ready to be changed from displayed to hidden. It is added to the element before the animation starts. It will be deleted immediately once the animation starts;

  2. v- leave-active: During the animation transition process, the element always has this style. It will be automatically deleted until the end of the animation. It is used to set the transition effect;

  3. v-leave-to During the animation transition process , the element always has this style, and will be automatically deleted until the end of the animation, which is used to set the final effect of the animation;

In the example, when the button is clicked, p will not display immediately: none , instead, v-leave is set first, v-leave is deleted the next moment, and v-leave-active v-leave-to is added at the same time. When the transition time in v-leave-active is completed, v-leave-active is deleted v-leave-to, and add display: none.

  1. v-enter The current element is ready to change from hidden to displayed. It is added to the element before the animation starts. It will be deleted immediately once the animation starts;

  2. v-enter-active During the animation transition process, the element always has this style. It will be automatically deleted until the end of the animation. It is used to set the transition effect;

  3. v-enter-to During the animation transition process, the element always has this style, and is automatically deleted until the end of the animation. It is used to set the final effect of the animation;

In the example, when the button is clicked, p immediately clears the display : none, then set v-enter, delete v-enter the next moment, and add v-enter-active v-enter-to. When the transition time in v-enter-active is completed, delete v-enter-active v-enter-to.

2 Custom animation class name:

<script src="/public/javascripts/vuejs"></script>
<style>
  red {background-color: red; width: 100px; height: 100px;}
  redslide-leave { margin-top: 50px; }
  redslide-leave-active { transition: all 3s;}
  redslide-leave-to { margin-top: 100px; opacity: 0;}
  redslide-enter { margin-top: 50px; }
  redslide-enter-active { transition: all 3s;}
  redslide-enter-to { margin-top: 10px; opacity: 0;}
</style>
<body>
<p id="app">
  <transition name="slide">
    <p class="red" v-show="show"></p>
  </transition>
  <button v-on:click="change">button</button>
</p>
<script>
new Vue({
  el: &#39;#app&#39;,
  data: {
    show: true
  },
  methods: {
    change: function(){
      thisshow = !thisshow;
    }
  }
});
</script>

This effect is exactly the same as the previous example, transition elements can use name attribute to specify the class name prefix used to replace the v- field, for example, name="slide" in the example makes the original v-enter became slide-enter.

3 When transition and animation are used at the same time

<script src="/public/javascripts/vuejs"></script>
<style>
@keyframes aslide {
  0% {
    margin-left: 10px;
  }
  100% {
    margin-left: 100px;
  }
}
  red {background-color: red; width: 100px; height: 100px;}
  blue {background-color: blue; width: 100px; height: 100px;}
  v-leave { margin-top: 50px; }
  v-leave-active { transition: all 3s; animation: aslide 5s;}
  v-leave-to { margin-top: 100px;}
</style>
<body>
<p id="app">
  <transition type="transition" >
    <p class="red" v-show="show"></p>
  </transition>
  <br>
  <transition type="animation" >
    <p class="blue" v-show="show"></p>
  </transition>
  <button v-on:click="change">button</button>
</p>
<script>
new Vue({
  el: &#39;#app&#39;,
  data: {
    show: true
  },
  methods: {
    change: function(){
      thisshow = !thisshow;
    }
  }
});
</script>

In the case, the animation specifies both transition and animation animation, and the transition element The type attribute can specify which animation time is used as the end time of the element. If the animation monitoring method is not specified, the longest time will prevail.

4 javascript monitoring animation

<script src="/public/javascripts/vuejs"></script>
<style>
  red {background-color: red; width: 100px; height: 100px;}
  v-leave { margin-top: 50px; }
  v-leave-active { transition: all 3s;}
  v-leave-to { margin-top: 100px;}
</style>
<body>
<p id="app">
  <transition
    v-on:before-enter="beforeEnter"
    v-on:enter="enter"
    v-on:after-enter="afterEnter"
    v-on:enter-cancelled="enterCancelled"
    v-on:before-leave="beforeLeave"
    v-on:leave="leave"
    v-on:after-leave="afterLeave"
    v-on:leave-cancelled="leaveCancelled"
    >
    <p class="red" v-show="show"></p>
  </transition>
  <button v-on:click="change">button</button>
</p>
<script>
new Vue({
  el: &#39;#app&#39;,
  data: {
    show: true
  },
  methods: {
    change: function() {
      thisshow = !thisshow; 
      consolelog(&#39;-----------click---------&#39;);
    },
    beforeEnter: function (el) {
      consolelog(&#39;beforeEnter:&#39;);
    },
    enter: function (el, done) {
      consolelog(&#39;enter:&#39;);
      // done()
    },
    afterEnter: function (el) {
      consolelog(&#39;afterEnter:&#39;);
    },
    enterCancelled: function (el) {
      consolelog(&#39;enterCancelled:&#39;);
    },
    beforeLeave: function (el) {
      consolelog(&#39;beforeLeave:&#39;);
    },
    leave: function (el, done) {
      consolelog(&#39;leave:&#39;);
      done()
    },
    afterLeave: function (el) {
      consolelog(&#39;afterLeave:&#39;);
    },
    leaveCancelled: function (el) {
      consolelog(&#39;leaveCancelled:&#39;);
    }
  }
});
</script>

  1. Once the js event is used, the original css animation transition effect will Invalid, the official recommendation is to set v-bind:css="false" on

    to avoid the need for Vue's internal mechanism to monitor css animation event callbacks , improve performance.
  2. Enter and leave events need to manually call the done method, otherwise the event will never call the subsequent after event. If the after event is not called but other events start, it will be regarded as animation. Was canceled.

5 Animation during page initialization:

<script src="/public/javascripts/vuejs"></script>
<style>
@keyframes aslide {
  0% {
    margin-left: 10px;
  }
  100% {
    margin-left: 100px;
  }
}
  red {background-color: red; width: 100px; height: 100px;}
  apper { margin-top: 50px; }
  apper-active { margin-top: 100px; animation: aslide 4s; transition: all 3s;}
</style>
<body>
<p id="app">
  <transition
    appear 
    appear-class="apper" 
    appear-active-class="apper-active" 
    v-on:before-appear="customBeforeAppearHook"
    v-on:appear="customAppearHook"
    v-on:after-appear="customAfterAppearHook" >
    <p class="red" ></p>
  </transition>
  <button v-on:click="change">button</button>
</p>
<script>
new Vue({
  el: &#39;#app&#39;,
  data: {
    show: true
  },
  methods: {
    change: function() {
      thisshow = !thisshow; 
      consolelog(&#39;-----------click---------&#39;);
    },
    customBeforeAppearHook: function (el) {
      consolelog(&#39;customBeforeAppearHook:&#39;);
    },
    customAppearHook: function (el) {
      consolelog(&#39;customAppearHook:&#39;);
      // done()
    },
    customAfterAppearHook: function (el) {
      consolelog(&#39;customAfterAppearHook:&#39;);
    }
  }
});
</script>

  1. The appear attribute indicates that the initialization animation is turned on, the appear-class attribute specifies the style before initialization, and the appear-active-class attribute specifies the style of the initialization animation process;

  2. transition animation cannot be started in the initialization animation. Effective, while animation animation can;

  3. before-appear appear after-appear is an event callback, it is quite clear to see the example.

6 Key of animation element:

<script src="/public/javascripts/vuejs"></script>
<style>
  v-enter-active { transition: all 15s;}
  v-enter-to { margin-top: 100px;}
  v-leave-active { transition: all 15s;}
  v-leave-to { margin-top: 10px;}
</style>
<body>
<p id="app">
  <p class="show1">
    <transition>
      <button v-if="show1" @click="show1 = false">on</button>
      <button v-else @click="show1 = true">off</button>
    </transition>
  </p>
  <p class="show2">
    <transition>
      <button v-if="show2" key="on" @click="show2 = false">on</button>
      <button v-else key="off" @click="show2 = true">off</button>
    </transition>
  </p>
</p>
<script>
var app = new Vue({
  el: &#39;#app&#39;,
  data: {
    show1: true,
    show2: true
  }
});
</script>

#show1 Why is there no animation effect? Because vue will recognize the two buttons in the switch as the same element, but only modify the different contents in the buttons, so the DOM element switching does not actually occur on the page;

If you want vue To clearly identify that these are two different button elements, specify different key attribute values ​​for each element.

7 Animation mode for element switching:

<script src="/public/javascripts/vuejs"></script>
<style>
  v-enter { margin-left: 100px;}
  v-enter-active { transition: all 5s;}
  v-enter-to { margin-left: 10px;}
  v-leave { margin-left: 10px;}
  v-leave-active { transition: all 5s;}
  v-leave-to { margin-left: 100px;}
</style>
<body>
<p id="app">
  <p class="default">
    <transition>
      <button v-if="show" key="on" @click="show = false">on</button>
      <button v-else key="off" @click="show = true">off</button>
    </transition>
  </p>
  <p class="inout">
    <transition mode="in-out">
      <button v-if="show" key="on" @click="show = false">on</button>
      <button v-else key="off" @click="show = true">off</button>
    </transition>
  </p>
  <p class="outin">
    <transition mode="out-in">
      <button v-if="show" key="on" @click="show = false">on</button>
      <button v-else key="off" @click="show = true">off</button>
    </transition>
  </p>
</p>
<script>
var app = new Vue({
  el: &#39;#app&#39;,
  data: {
    show: true
  }
});
</script>


  1. transition 默认是同时执行2个元素的切换动画的,案例中红色的 off 按钮其实是会同时向左移动的,只是因为布局上没有脱离布局流,被 on 按钮顶住,无法移动;

  2. mode="in-out" 可以使切换元素先执行将要显示元素的动画,再执行将要隐藏元素的动画;

  3. mode="out-in" 可以使切换元素先执行将要隐藏元素的动画,再执行将要显示元素的动画;

8 多元素动画:

<script src="/public/javascripts/vuejs"></script>
<style>
  v-enter { margin-left: 100px;}
  v-enter-active { transition: all 2s;}
  v-enter-to { margin-left: 10px;}
</style>
<body>
<p id="app">
  <transition-group>
    <li v-for="item in items" :key="item">{{item}}</li>
  </transition-group>
  <transition-group tag="ul">
    <li v-for="item in items" :key="item">{{item}}</li>
  </transition-group>
  <button @click="itemspush(itemslength)">add</button>
</p>
<script>
var app = new Vue({
  el: &#39;#app&#39;,
  data: {
    items: [0,1]
  }
});
</script>


  1. transition 里面只能放置单个元素或使用 v-if v-show 切换的单个元素,要想使用多个元素的动画,必须使用 transition-group;

  2. transition-group 默认会在 DOM 里渲染成 span 标签,可使用 tag="ul" 指定渲染成其他标签;

  3. transition-group 必须为每一个子元素指定 key;

8 多元素的位移动画:

<script src="/public/javascripts/vuejs"></script>
<style>
  v-move { transition: all 1s; }
</style>
<body>
<p id="app">
  <transition-group tag="ul" >
    <li v-for="item in items" :key="item">{{item}}</li>
  </transition-group>
  <button @click="itemsreverse()">reverse</button>
</p>
<script>
var app = new Vue({
  el: &#39;#app&#39;,
  data: {
    items: [0,1,2,3]
  }
});
</script>


  1. transition-group 允许在每个元素移动时,添加 v-move 的样式,移动完成后自动清除该样式;

  2. transition 的属性, transition-group 都有,包括 name enter leave;

The above is the detailed content of Detailed explanation of examples of animation transition effects for Vue elements. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
用户遭遇罕见故障 三星 Watch 智能手表突现白屏问题用户遭遇罕见故障 三星 Watch 智能手表突现白屏问题Apr 03, 2024 am 08:13 AM

你可能遇到过智能手机屏幕出现绿色线条的问题,即使没见过,也一定在网络上看到过相关图片。那么,智能手表屏幕变白的情况你遇见过吗?4月2日,CNMO从外媒了解到,一名Reddit用户在社交平台上分享了一张图片,展示了三星Watch系列智能手表屏幕变白的情况。该用户写道:"我离开时正在充电,回来时就这样了,我尝试重启,但重启过程中屏幕还是这样。"三星Watch智能手表屏幕变白这位Reddit用户并未指明这款智能手表的具体型号。不过,从图片上看,应该是三星Watch5。此前,另一位Reddit用户也报告

九州风神阿萨辛 4S 散热器评测 风冷“刺客大师”范儿九州风神阿萨辛 4S 散热器评测 风冷“刺客大师”范儿Mar 28, 2024 am 11:11 AM

说起阿萨辛ASSASSIN,相信玩家们一定会想到《刺客信条》中的各位刺客大师,不仅身手了得,而且"躬身于黑暗、服务于光明"的信条,与国内知名机箱/电源/散热器品牌九州风神(DeepCool)旗下的阿萨辛ASSASSIN系列旗舰级风冷散热器不谋而合。最近,该系列的最新产品阿萨辛ASSASSIN4S重磅上线,"西装刺客,再进阶"为高级玩家带来全新的风冷散热体验。外观一览细节满满阿萨辛4S散热器采用双塔构造+单风扇内嵌设计,外面包覆立方体造型的整流罩,整体感极强,并提供白、黑两种配色可选,满足不同色系

春日里的精致光影艺术,哈趣 H2 性价比之选春日里的精致光影艺术,哈趣 H2 性价比之选Apr 17, 2024 pm 05:07 PM

随着春天的到来,万物复苏,一切都充满了生机与活力。在这个美好的季节里,如何为家居生活增添一抹别样的色彩?哈趣H2投影仪,以其精致的设计和超高的性价比,成为了这个春天里不可或缺的一道亮丽风景。这款H2投影仪小巧玲珑却不失时尚。无论是放在客厅的电视柜上,还是卧室的床头柜旁,都能成为一道亮丽的风景线。它的机身采用了奶白色的磨砂质地,这种设计不仅让投影仪的外观更显高级,同时也增加了触感的舒适度。米色仿皮纹材质,更是为整体外观增添了一抹温馨与雅致。这种色彩与材质的搭配,既符合现代家居的审美趋势,又能够融入

航嘉 MX750P 全模组电源评测:750W 的白金实力浓缩航嘉 MX750P 全模组电源评测:750W 的白金实力浓缩Mar 28, 2024 pm 03:20 PM

ITX平台以小巧的身形吸引了不少追求极致和独特美感的玩家,随着制程的提升和技术的进步,英特尔第14代酷睿和RTX40系显卡都可以在ITX平台中发挥实力,游戏玩家也对SFX电源有了更高的要求。游戏爱好者航嘉推出新的MX系列电源,在满足高性能需求的ITX平台中,MX750P全模组电源的定额功率高达750W,同时通过了80PLUS白金级认证。以下我们就带来这款电源的评测。航嘉MX750P全模组电源采用了简约时尚的设计理念,共有黑白两款供玩家选择,均采用磨砂表面处理,搭配银灰色和红色的字体有很好的质感,

七彩虹隐星 P15 24 评测:颜值性能兼具的硬核全能游戏本七彩虹隐星 P15 24 评测:颜值性能兼具的硬核全能游戏本Mar 06, 2024 pm 04:40 PM

在当下科技飞速发展的时代,笔记本电脑已经成为人们日常生活和工作中不可或缺的重要工具。对于那些对性能有高要求的玩家而言,拥有配置强大、性能出色的笔记本电脑才能满足其硬核需求。七彩虹隐星P15笔记本电脑凭借其卓越性能和令人惊艳的设计,成为了未来的引领者,堪称硬核笔记本的典范。七彩虹隐星P1524配备了13代英特尔酷睿i7处理器和RTX4060LaptopGPU,外观采用更时尚的宇宙飞船设计风格,同时在细节表现上也有出色表现。让我们先来了解一下这款笔记本的特点。至高搭载英特尔酷睿i7-13620H处理

轻松拿捏 4K 高清图像理解!这个多模态大模型自动分析网页海报内容,打工人简直不要太方便轻松拿捏 4K 高清图像理解!这个多模态大模型自动分析网页海报内容,打工人简直不要太方便Apr 23, 2024 am 08:04 AM

一个可以自动分析PDF、网页、海报、Excel图表内容的大模型,对于打工人来说简直不要太方便。上海AILab,香港中文大学等研究机构提出的InternLM-XComposer2-4KHD(简写为IXC2-4KHD)模型让这成为了现实。相比于其他多模态大模型不超过1500x1500的分辨率限制,该工作将多模态大模型的最大输入图像提升到超过4K(3840x1600)分辨率,并支持任意长宽比和336像素~4K动态分辨率变化。发布三天,该模型就登顶HuggingFace视觉问答模型热度榜单第一。轻松拿捏

真正的一镜走天下 尼克尔 Z 28-400mm f/4-8 VR 镜头上手体验真正的一镜走天下 尼克尔 Z 28-400mm f/4-8 VR 镜头上手体验Mar 28, 2024 pm 02:54 PM

很多摄影爱好者喜欢使用镜头,他们的拍摄需求非常多变,因此在镜头的选择上更倾向于一支比较全能的产品,也就是我们俗称的"一镜走天下"镜头。刚好,现在尼康推出了一支新的产品,尼克尔Z28-400mmf/4-8VR镜头,一支真正的"一镜走天下"镜头。镜头从28mm广角端一直覆盖到400mm长焦端,配备其Z卡口相机,可以轻松拍摄十分丰富的摄影主题,并带来一场丰富的视角变化。今天,我们就通过近期的使用体验,跟大家一起聊聊这支尼克尔Z28-400mmf/4-8VR镜头。尼克尔Z28-400mmf/4-8VR是

上代机皇能否再战?三星 Galaxy S23 Ultra 实际使用体验分享上代机皇能否再战?三星 Galaxy S23 Ultra 实际使用体验分享Mar 12, 2024 pm 01:58 PM

在智能手机市场,三星的Galaxy系列一直以其卓越的性能和创新的设计备受瞩目。而GalaxyS23Ultra作为上代机皇,自发布以来便受到了广大消费者的喜爱。随着时间的推移,新机型层出不穷,那么,这款昔日的机皇如今还能否再战呢?接下来,我将分享自己在使用三星GalaxyS23Ultra过程中的实际体验,带大家一同探讨这个问题。首先,从外观设计上来看,GalaxyS23Ultra依然保持着三星一贯的精致与高端。其独特的微曲屏设计不仅提升了手机的整体美感,更为用户带来了更加沉浸的视觉体验。在日常使用

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),