Home >Web Front-end >Vue.js >How to implement staggered transition in vue
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.
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
## Based on vue, you need to have used and understood vue'sHow to implement staggered transition in vue and
How to implement staggered transition in vue-group.
analysis of the
transtion component.
transtion is an abstract component provided by vue, which will help us operate its wrapped sub-elements at the appropriate time.
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)
dom The next frame after inserting the page (corresponding to the animation below)
dom The next frame after the animation is completed ( The animation corresponding to the picture below ends)
Component The transtion
component operations will be performed on each element in the packaged list in batches. <pre class="brush:php;toolbar:false">html
<div>
<how to implement staggered transition in vue-group>
<item></item>
</how>to implement staggered transition in vue-group>
</div></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-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; }复制代码
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'); } } })复制代码
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
) 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 = ""; } } })
html <item></item>
getRandom() { var rate = Math.floor(Math.random() * 90 + 10); return Math.random() > 0.5 ? rate : -1 * rate; }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!