Home  >  Article  >  Web Front-end  >  How to implement staggered transition in vue

How to implement staggered transition in vue

藏色散人
藏色散人Original
2022-12-26 15:38:572671browse

Vue’s method of implementing staggered How to implement staggered transition in vue: 1. Open the corresponding vue file; 2. Use the How to implement staggered transition in vue-group component to batch operate the How to implement staggered transition in vue component on each element in the wrapped list; 3. Give each list item Just add different delays.

How to implement staggered transition in vue

The operating environment of this tutorial: Windows 10 system, Vue version 3, Dell G3 computer.

How to realize the staggered How to implement staggered transition in vue of vue?

Vue’s list staggered How to implement staggered transition in vue

How to implement staggered How to implement staggered transition in vue in vue

## Based on vue, you need to have used and understood vue's

How to implement staggered transition in vue and How to implement staggered transition in vue-group.

How to implement staggered transition in vue

vue's documentation already has a very detailed

analysis of the transtion component.

Here is my understanding:

transtion is an abstract component provided by vue, which will help us operate its wrapped sub-elements at the appropriate time.

The appropriate time is:

  1. beforeEnter The dom is generated from js and the frame just inserted into the page (corresponding to the start of the animation in the picture below)
  2. enter dom The next frame after inserting the page (corresponding to the animation below)
  3. afterEnter dom The next frame after the animation is completed ( The animation corresponding to the picture below ends)
How to implement staggered transition in vue
##How to implement staggered transition in vue-group

ransition-group

Component The transtion component operations will be performed on each element in the packaged list in batches. <pre class="brush:php;toolbar:false">html &lt;div&gt;     &lt;how to implement staggered transition in vue-group&gt;         &lt;item&gt;&lt;/item&gt;     &lt;/how&gt;to implement staggered transition in vue-group&gt; &lt;/div&gt;</pre> <pre class="brush:php;toolbar:false">css.list-enter {    opacity: 0;    transform: translateY(100%); }.list-enter-active {    How to implement staggered transition in vue: .3s; }/* enter-to其实可以不用写, 没有显性写明,就是默认的opacity: 1;transform: none;  */.list-enter-to {    opacity: 1;    transform: translateY(0); }</pre>

How to implement staggered transition in vue-group
Next, add different delays to each list item.

How to implement staggered transition in vue-delay

css.list-enter-active:nth-child(5n+2) {    How to implement staggered transition in vue-delay: .3s;
}.list-enter-active:nth-child(5n+3) {    How to implement staggered transition in vue-delay: .5s;
}.list-enter-active:nth-child(5n+4) {    How to implement staggered transition in vue-delay: .7s;
}.list-enter-active:nth-child(5n+5) {    How to implement staggered transition in vue-delay: .9s;
}复制代码

How to implement staggered transition in vue-delay
Using How to implement staggered transition in vue-delay with css selector can indeed achieve staggered How to implement staggered transition in vue,

But the shortcomings are also obvious. It requires a lot of css to be written and it is not flexible to modify.

Next we use vue’s JavaScript hook to implement it.

How to implement staggered transition in vue

html<div>
    <!-- 这里加上 v-bind:css="false" 让vue跳过对css的检测,让我们更好控制 动画完成的时机 -->
    <how to implement staggered transition in vue-group>

        <item></item>

    </how>to implement staggered transition in vue-group></div>复制代码

When only using JavaScript to How to implement staggered transition in vue, done must be used for callbacks in enter and leave. Otherwise, they are called synchronously and the How to implement staggered transition in vue is completed immediately.

new Vue({    el: "#app",    data: () => ({        num: 0
    }),    methods: {        //让我们在 beforeEnter enter afterEnter 钩子里,把 vue 帮我们做的事,自己做一遍:
        //添加移除 class 类名,监听 How to implement staggered transition in vueend 事件。
        beforeEnter(dom) {
            dom.classList.add('list-enter', 'list-enter-active');
        },        enter(dom,done) {            let delay = dom.dataset.delay;            How to implement staggered transition in vue(function () {
                dom.classList.remove('list-enter');
                dom.classList.add('list-enter-to');                //监听 How to implement staggered transition in vueend 事件
                var How to implement staggered transition in vueend = window.onHow to implement staggered transition in vueend ? "How to implement staggered transition in vueend" : "webkitTransitionEnd";
                dom.addEventListener(How to implement staggered transition in vueend, function onEnd() {
                  dom.removeEventListener(How to implement staggered transition in vueend, onEnd);                  done(); //调用done() 告诉vue动画已完成,以触发 afterEnter 钩子
                });
            }, delay)
        },        afterEnter(dom) {
            dom.classList.remove('list-enter-to', 'list-enter-active');
        }
    }
})复制代码
How to implement staggered transition in vue
Currently, it runs well and achieves the effect of staggered How to implement staggered transition in vue without writing a lot of css.

Looking back, we have done 2 things in total, using

.list-enter .list-enter-to

and How to implement staggered transition in vue to tell the browser The processor How to implement staggered transition in vues the list items from

opacity 0 translateY(100%

) to opacity 1 translateY(0) at different times. Tell the browser the different states of the elements. In addition to the class name,

We can directly operate the dom and write the How to implement staggered transition in vue inline.

In addition to not writing css at all In addition to class names,

can also have more programmatic properties.

How to implement staggered transition in vue

html
<div>
        <how to implement staggered transition in vue-group>

            <item></item>

        </how>to implement staggered transition in vue-group>
    </div>
new Vue({
    el: "#app",
    data: () => ({
        num: 0
    }),
    methods: {        beforeEnter(dom) {
            let { x = 0, y = 0, s = 1, opacity = 0 } = dom.dataset;
            dom.How to implement staggered transition in vue.cssText = `How to implement staggered transition in vue: .3s;opacity: ${opacity};transform: scale(${s}) translateX(${x}) translateY(${y});`;
        },        enter(dom,done) {
            let delay = dom.dataset.delay;            How to implement staggered transition in vue(function () {
                dom.How to implement staggered transition in vue.cssText = `How to implement staggered transition in vue: .3s;opacity: 1;transform: scale(1) translateX(0) translateY(0);`;                //监听 How to implement staggered transition in vueend 事件
                var How to implement staggered transition in vueend = window.onHow to implement staggered transition in vueend ? "How to implement staggered transition in vueend" : "webkitTransitionEnd";
                dom.addEventListener(How to implement staggered transition in vueend, function onEnd() {
                  dom.removeEventListener(How to implement staggered transition in vueend, onEnd);                  done(); //调用done() 告诉vue动画已完成,以触发 afterEnter 钩子
                });
            }, delay)
        },        afterEnter(dom) {
            dom.How to implement staggered transition in vue.cssText = "";
        }
    }
})

How to implement staggered transition in vue
Well, yes, but since it’s all in js, the only thing that can be limited is your imagination.
html
    <item></item>
getRandom() {    var rate = Math.floor(Math.random() * 90 + 10);    return Math.random() > 0.5 ? rate : -1 * rate;
}

How to implement staggered transition in vue Recommended learning: "
vue.js video tutorial
"

The above is the detailed content of How to implement staggered transition in vue. 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