<Transition>
Misol
v-if yordamida olib tashlanayotgan <p> elementini animatsiya qilish uchun o‘rnatilgan <Transition> komponentidan foydalanish:
<Transition>
<p v-if="exists">Hello World!</p>
</Transition>
Misolni ishga tushirish »
Quyida ko‘proq misollarni ko‘ring.
Ta’rif va qo‘llanilishi
O‘rnatilgan <Transition> komponenti elementlar v-if , v-show yoki dinamik komponentlar yordamida qo‘shilayotganda yoki olib tashlanayotganda ularni animatsiya qilish uchun ishlatiladi.
Elementlar qanday animatsiya qilinishi haqidagi qoidalar avtomatik yaratiladigan classlar yoki JavaScript o‘tish (transition) hook’lari ichida yoziladi. Quyidagi jadvallarga qarang.
<Transition> komponentining root darajasida faqat bitta element bo‘lishi mumkin.
Props
| Prop | Tavsif | |
|---|---|---|
| none | Standart. | Misolni ishga tushirish » |
| appear | Agar true qiymati berilsa, element birinchi marta DOMga o‘rnatilayotganda (mount qilinayotganda) ham animatsiya qilinadi. Standart qiymat — false. | Misolni ishga tushirish » |
| mode | mode="out-in" dastlabki element keyingi element kirishidan oldin chiqib ketishini ta’minlaydi. mode="in-out" yangi element eski element chiqib ketishidan oldin kirishini ta’minlaydi. Standart holatda eski element yangi element kirishi bilan bir vaqtda chiqib ketadi. | Misolni ishga tushirish » |
| name | O‘tish (transition) nomini belgilang. Agar bir nechta o‘tish bo‘lsa, ularni bir-biridan farqlash uchun ularga noyob nomlar berishimiz kerak. name="swirl" CSS o‘tish classlari standart v- prefiksi o‘rniga swirl- bilan boshlanishini ta’minlaydi. | Misolni ishga tushirish » |
| css | Boolean. :css="false" Vue kompilyatoriga ushbu o‘tish (transition) uchun hech qanday o‘tish classlari ishlatilmasligini, faqat JavaScript hook’lari ishlatilishini bildiradi. Bu prop o‘rnatilganda enter va leave hook’lari ichida done() callback funksiyasidan foydalanish shart. | Misolni ishga tushirish » |
| type | O‘tish tugashi uchun "animation" yoki "transition" tugashini kutish kerakligini belgilang. Agar ham CSS animatsiyasi, ham CSS transition o‘rnatilgan bo‘lsa va bu type prop’i berilmagan bo‘lsa, Vue ularning eng uzoq davomiyligini aniqlaydi va uni o‘tish vaqti sifatida ishlatadi. | |
| duration | "enter" va "leave" uchun o‘tish vaqtining davomiyligini belgilang. Standart holatda o‘tish CSS animatsiyasi yoki CSS transition tugashi bilan tugaydi. Aniq vaqtlarni :duration="{enter:2000, leave:1000 }" ko‘rinishida yoki duration="1000" ko‘rinishida belgilash mumkin. | |
|
enterFromClass enterActiveClass enterToClass appearFromClass appearActiveClass appearToClass leaveFromClass leaveActiveClass leaveToClass |
Use these props to rename transition classes.
Bu prop’lardan birini enter-active-class="entering" ko‘rinishida ishlatish ushbu o‘tish classiga CSS-da standart .v-enter-active o‘rniga .entering nomi bilan murojaat qilish mumkinligini anglatadi. HTMLda atributlar qanday yozilishiga mos bo‘lishi uchun shablondagi (template) prop’lar uchun kebab-case’dan foydalanish qabul qilingan. |
Misolni ishga tushirish » |
CSS o‘tish classlari
<Transition> komponentidan foydalanganda avtomatik ravishda olti xil CSS classiga ega bo‘lamiz; ulardan elementlar qo‘shilayotganda yoki olib tashlanayotganda ularni animatsiya qilish uchun foydalanishimiz mumkin.
Ushbu classlar elementlar qo‘shilayotgan (enter) yoki olib tashlanayotgan (leave) turli bosqichlarda faol bo‘ladi:
| O‘tish classi | Tavsif | |
|---|---|---|
| v-enter-from | Kirish bosqichi boshlanganda elementning boshlang‘ich uslubi | Misolni ishga tushirish » |
| v-enter-active | Kirish bosqichi davomida elementning uslubi | Misolni ishga tushirish » |
| v-enter-to | Kirish bosqichining aynan oxiridagi element uslubi | Misolni ishga tushirish » |
| v-leave-from | Chiqish bosqichi boshlanganda elementning dastlabki uslubi | Misolni ishga tushirish » |
| v-leave-active | Chiqish bosqichi davomida elementning uslubi | Misolni ishga tushirish » |
| v-leave-to | Chiqish bosqichining aynan oxiridagi element uslubi | Misolni ishga tushirish » |
JavaScript o‘tish hook’lari
Yuqoridagi o‘tish classlari JavaScript kodini ishga tushirish uchun ulanishimiz (hook qilishimiz) mumkin bo‘lgan hodisalarga mos keladi.
| JavaScript hodisasi | Tavsif | |
|---|---|---|
| before-enter | Kirish (enter) bosqichining aynan boshida chaqiriladi | |
| enter | Kirish (enter) bosqichida "before-enter" hook’idan keyin chaqiriladi | Misolni ishga tushirish » |
| after-enter | Kirish o‘tishining aynan oxirida chaqiriladi | Misolni ishga tushirish » |
| enter-cancelled | Kirish o‘tishi bekor qilinsa chaqiriladi | Misolni ishga tushirish » |
| before-leave | Chiqish (leave) bosqichining aynan boshida chaqiriladi | Misolni ishga tushirish » |
| leave | Chiqish (leave) bosqichida "before-leave" hook’idan keyin chaqiriladi | Misolni ishga tushirish » |
| after-leave | Chiqish o‘tishining aynan oxirida chaqiriladi | |
| leave-cancelled | Bu faqat v-show ishlatilganda va chiqish bosqichi bekor qilinganda chaqiriladi |
Ko‘proq misollar
1-misol
<p> elementi almashtirib turilganda (toggle) sirpanib kiradi va chiqib ketadi.
<template>
<h1>Add/Remove <p> Tag</h1>
<button @click="this.exists = !this.exists">{{btnText}}</button><br>
<Transition>
<p v-if="exists">Hello World!</p>
</Transition>
</template>
<script>
export default {
data() {
return {
exists: false
}
},
computed: {
btnText() {
if(this.exists) {
return 'Remove';
}
else {
return 'Add';
}
}
}
}
</script>
<style>
.v-enter-from {
opacity: 0;
translate: -100px 0;
}
.v-enter-to {
opacity: 1;
translate: 0 0;
}
.v-leave-from {
opacity: 1;
translate: 0 0;
}
.v-leave-to {
opacity: 0;
translate: 100px 0;
}
p {
background-color: lightgreen;
display: inline-block;
padding: 10px;
transition: all 0.5s;
}
</style>
Misolni ishga tushirish »
2-misol
<p> elementi “enter” va “leave” paytida alohida fon ranglariga ega
<template>
<h1>Add/Remove <p> Tag</h1>
<button @click="this.exists = !this.exists">{{btnText}}</button><br>
<Transition>
<p v-if="exists">Hello World!</p>
</Transition>
</template>
<script>
export default {
data() {
return {
exists: false
}
},
computed: {
btnText() {
if(this.exists) {
return 'Remove';
}
else {
return 'Add';
}
}
}
}
</script>
<style>
.v-enter-active {
background-color: lightgreen;
animation: added 1s;
}
.v-leave-active {
background-color: lightcoral;
animation: added 1s reverse;
}
@keyframes added {
from {
opacity: 0;
translate: -100px 0;
}
to {
opacity: 1;
translate: 0 0;
}
}
p {
display: inline-block;
padding: 10px;
border: dashed black 1px;
}
</style>
Misolni ishga tushirish »
3-misol
<p> elementlari <Transition> komponentlarini bir-biridan farqlash uchun name prop’idan foydalanib, turlicha animatsiya qilinadi.
<template>
<h1>Add/Remove <p> Tag</h1>
<p>The second transition in this example has the name prop "swirl", so that we can keep the transitions apart with different class names.</p>
<hr>
<button @click="this.p1Exists = !this.p1Exists">{{btn1Text}}</button><br>
<Transition>
<p v-if="p1Exists" id="p1">Hello World!</p>
</Transition>
<hr>
<button @click="this.p2Exists = !this.p2Exists">{{btn2Text}}</button><br>
<Transition name="swirl">
<p v-if="p2Exists" id="p2">Hello World!</p>
</Transition>
</template>
<script>
export default {
data() {
return {
p1Exists: false,
p2Exists: false
}
},
computed: {
btn1Text() {
if(this.p1Exists) {
return 'Remove';
}
else {
return 'Add';
}
},
btn2Text() {
if(this.p2Exists) {
return 'Remove';
}
else {
return 'Add';
}
}
}
}
</script>
<style>
.v-enter-active {
background-color: lightgreen;
animation: added 1s;
}
.v-leave-active {
background-color: lightcoral;
animation: added 1s reverse;
}
@keyframes added {
from {
opacity: 0;
translate: -100px 0;
}
to {
opacity: 1;
translate: 0 0;
}
}
.swirl-enter-active {
animation: swirlAdded 1s;
}
.swirl-leave-active {
animation: swirlAdded 1s reverse;
}
@keyframes swirlAdded {
from {
opacity: 0;
rotate: 0;
scale: 0.1;
}
to {
opacity: 1;
rotate: 360deg;
scale: 1;
}
}
#p1, #p2 {
display: inline-block;
padding: 10px;
border: dashed black 1px;
}
#p2 {
background-color: lightcoral;
}
</style>
Misolni ishga tushirish »
4-misol
after-enter hodisasi <div> elementining ko‘rsatilishini ishga tushiradi.
<template>
<h1>JavaScript Transition Hooks</h1>
<p>This code hooks into "after-enter" so that after the initial animation is done, a method runs that displays a red div.</p>
<button @click="pVisible=true">Create p-tag!</button><br>
<Transition @after-enter="onAfterEnter">
<p v-show="pVisible" id="p1">Hello World!</p>
</Transition>
<br>
<div v-show="divVisible">This appears after the "enter-active" phase of the transition.</div>
</template>
<script>
export default {
data() {
return {
pVisible: false,
divVisible: false
}
},
methods: {
onAfterEnter() {
this.divVisible = true;
}
}
}
</script>
<style>
.v-enter-active {
animation: swirlAdded 1s;
}
@keyframes swirlAdded {
from {
opacity: 0;
rotate: 0;
scale: 0.1;
}
to {
opacity: 1;
rotate: 360deg;
scale: 1;
}
}
#p1, div {
display: inline-block;
padding: 10px;
border: dashed black 1px;
}
#p1 {
background-color: lightgreen;
}
div {
background-color: lightcoral;
}
</style>
Misolni ishga tushirish »
5-misol
Almashtirish (toggle) tugmasi enter-cancelled hodisasini ishga tushiradi.
<template>
<h1>The 'enter-cancelled' Event</h1>
<p>Click the toggle button again before the enter animation is finished to trigger the 'enter-cancelled' event.</p>
<button @click="pVisible=!pVisible">Toggle</button><br>
<Transition @enter-cancelled="onEnterCancelled">
<p v-if="pVisible" id="p1">Hello World!</p>
</Transition>
<br>
<div v-if="divVisible">You interrupted the "enter-active" transition.</div>
</template>
<script>
export default {
data() {
return {
pVisible: false,
divVisible: false
}
},
methods: {
onEnterCancelled() {
this.divVisible = true;
}
}
}
</script>
<style>
.v-enter-active {
animation: swirlAdded 2s;
}
@keyframes swirlAdded {
from {
opacity: 0;
rotate: 0;
scale: 0.1;
}
to {
opacity: 1;
rotate: 720deg;
scale: 1;
}
}
#p1, div {
display: inline-block;
padding: 10px;
border: dashed black 1px;
}
#p1 {
background-color: lightgreen;
}
div {
background-color: lightcoral;
}
</style>
Misolni ishga tushirish »
6-misol
appear prop’i sahifa yuklangan zahoti <p> elementi animatsiyasini boshlaydi.
<template>
<h1>The 'appear' Prop</h1>
<p>The 'appear' prop starts the animation when the p tag below is rendered for the first time as the page opens. Without the 'appear' prop, this example would have had no animation.</p>
<Transition appear>
<p id="p1">Hello World!</p>
</Transition>
</template>
<style>
.v-enter-active {
animation: swirlAdded 1s;
}
@keyframes swirlAdded {
from {
opacity: 0;
rotate: 0;
scale: 0.1;
}
to {
opacity: 1;
rotate: 360deg;
scale: 1;
}
}
#p1 {
display: inline-block;
padding: 10px;
border: dashed black 1px;
background-color: lightgreen;
}
</style>
Misolni ishga tushirish »
7-misol
Rasmlarni “enter” va “leave” animatsiyalari bilan varaqlash. Eski rasm olib tashlanishidan oldin yangi rasm qo‘shiladi.
<template>
<h1>Transition Between Elements</h1>
<p>Click the button to get a new image.</p>
<p>The new image is added before the previous is removed. We will fix this in the next example with mode="out-in".</p>
<button @click="newImg">Next image</button><br>
<Transition>
<img src="/img_pizza.svg" v-if="imgActive === 'pizza'">
<img src="/img_apple.svg" v-else-if="imgActive === 'apple'">
<img src="/img_cake.svg" v-else-if="imgActive === 'cake'">
<img src="/img_fish.svg" v-else-if="imgActive === 'fish'">
<img src="/img_rice.svg" v-else-if="imgActive === 'rice'">
</Transition>
</template>
<script>
export default {
data() {
return {
imgActive: 'pizza',
imgs: ['pizza', 'apple', 'cake', 'fish', 'rice'],
indexNbr: 0
}
},
methods: {
newImg() {
this.indexNbr++;
if(this.indexNbr >= this.imgs.length) {
this.indexNbr = 0;
}
this.imgActive = this.imgs[this.indexNbr];
}
}
}
</script>
<style>
.v-enter-active {
animation: swirlAdded 1s;
}
.v-leave-active {
animation: swirlAdded 1s reverse;
}
@keyframes swirlAdded {
from {
opacity: 0;
rotate: 0;
scale: 0.1;
}
to {
opacity: 1;
rotate: 360deg;
scale: 1;
}
}
img {
width: 100px;
margin: 20px;
}
img:hover {
cursor: pointer;
}
</style>
Misolni ishga tushirish »
8-misol
Rasmlarni “enter” va “leave” animatsiyalari bilan varaqlash. mode="out-in" eski rasm olib tashlanmaguncha yangi rasmlar qo‘shilishiga yo‘l qo‘ymaydi.
<template>
<h1>mode="out-in"</h1>
<p>Click the button to get a new image.</p>
<p>With mode="out-in", the next image is not added until the current image is removed. Another difference from the previous example, is that here we use computed prop instead of a method.</p>
<button @click="indexNbr++">Next image</button><br>
<Transition mode="out-in">
<img src="/img_pizza.svg" v-if="imgActive === 'pizza'">
<img src="/img_apple.svg" v-else-if="imgActive === 'apple'">
<img src="/img_cake.svg" v-else-if="imgActive === 'cake'">
<img src="/img_fish.svg" v-else-if="imgActive === 'fish'">
<img src="/img_rice.svg" v-else-if="imgActive === 'rice'">
</Transition>
</template>
<script>
export default {
data() {
return {
imgs: ['pizza', 'apple', 'cake', 'fish', 'rice'],
indexNbr: 0
}
},
computed: {
imgActive() {
if(this.indexNbr >= this.imgs.length) {
this.indexNbr = 0;
}
return this.imgs[this.indexNbr];
}
}
}
</script>
<style>
.v-enter-active {
animation: swirlAdded 0.7s;
}
.v-leave-active {
animation: swirlAdded 0.7s reverse;
}
@keyframes swirlAdded {
from {
opacity: 0;
rotate: 0;
scale: 0.1;
}
to {
opacity: 1;
rotate: 360deg;
scale: 1;
}
}
img {
width: 100px;
margin: 20px;
}
img:hover {
cursor: pointer;
}
</style>
Misolni ishga tushirish »
9-misol
Komponentlar o‘rtasida almashish animatsiya bilan amalga oshiriladi.
<template>
<h1>Transition with Dynamic Components</h1>
<p>The Transition component wraps around the dynamic component so that the switching can be animated.</p>
<button @click="toggleValue = !toggleValue">Switch component</button>
<Transition mode="out-in">
<component :is="activeComp"></component>
</Transition>
</template>
<script>
export default {
data () {
return {
toggleValue: true
}
},
computed: {
activeComp() {
if(this.toggleValue) {
return 'comp-one'
}
else {
return 'comp-two'
}
}
}
}
</script>
<style>
.v-enter-active {
animation: slideIn 0.5s;
}
@keyframes slideIn {
from {
translate: -200px 0;
opacity: 0;
}
to {
translate: 0 0;
opacity: 1;
}
}
.v-leave-active {
animation: slideOut 0.5s;
}
@keyframes slideOut {
from {
translate: 0 0;
opacity: 1;
}
to {
translate: 200px 0;
opacity: 0;
}
}
#app {
width: 350px;
margin: 10px;
}
#app > div {
border: solid black 2px;
padding: 10px;
margin-top: 10px;
}
</style>
Misolni ishga tushirish »
10-misol
Komponentlar o‘rtasida almashish animatsiya bilan amalga oshiriladi.
<template>
<h1>The :css="false" Prop</h1>
<p>With the 'css' prop set to 'false', we tell the compiler that JavaScript hooks are used instead of CSS transition classes.</p>
<p>When we use :css="false", we must call done() inside the 'enter' and the 'leave' hooks, to tell the browser when those transitions are finished.</p>
<button @click="pVisible=!pVisible">Toggle</button>
<div>
<Transition
:css="false"
@enter="onEnter"
@after-enter="onAfterEnter"
@before-leave="onBeforeLeave"
@leave="onLeave"
>
<p
v-if="pVisible"
id="p1">
Hello World!
</p>
</Transition>
</div>
</template>
<script>
export default {
data() {
return {
pVisible: false
}
},
methods: {
onEnter(el,done) {
let pos = 0;
window.requestAnimationFrame(frame);
function frame() {
if (pos > 150) {
done();
} else {
pos++;
el.style.left = pos + "px";
window.requestAnimationFrame(frame);
}
}
},
onAfterEnter(el) {
el.style.backgroundColor = "yellow";
},
onBeforeLeave(el) {
el.style.backgroundColor = "lightgreen";
},
onLeave(el,done) {
let pos = 150;
window.requestAnimationFrame(frame);
function frame() {
if (pos < 0) {
done();
}
else {
pos--;
el.style.left = pos + "px";
window.requestAnimationFrame(frame);
}
}
}
}
}
</script>
<style>
#p1 {
position: absolute;
padding: 10px;
border: dashed black 1px;
background-color: lightgreen;
}
#app > div {
position: relative;
background-color: coral;
width: 300px;
height: 300px;
border: dashed black 1px;
margin-top: 20px;
}
</style>
Misolni ishga tushirish »
11-misol
"v-enter-active" CSS classini "entering" deb qayta nomlash uchun enterActiveClass prop’idan foydalanish.
<template>
<h1>The 'enterActiveClass' Prop</h1>
<button @click="this.exists = !this.exists">{{btnText}}</button><br>
<Transition enter-active-class="entering">
<p v-if="exists">Hello World!</p>
</Transition>
</template>
<script>
export default {
data() {
return {
exists: false
}
},
computed: {
btnText() {
if(this.exists) {
return 'Remove';
}
else {
return 'Add';
}
}
}
}
</script>
<style>
.entering {
background-color: lightgreen;
animation: added 1s;
}
.v-leave-active {
background-color: lightcoral;
animation: added 1s reverse;
}
@keyframes added {
from {
opacity: 0;
translate: -100px 0;
}
to {
opacity: 1;
translate: 0 0;
}
}
p {
display: inline-block;
padding: 10px;
border: dashed black 1px;
}
</style>
Misolni ishga tushirish »
Tegishli sahifalar
Vue darsligi: Vue animatsiyalari
Vue darsligi: v-for bilan Vue animatsiyalari
Vue ma’lumotnomasi: Vue komponenti elementi
W3Schools Pathfinder
Yutuqlaringizni kuzating – bu bepul!
