3, component and element switch animation implementation
code
<! DOCTYPEhtml>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="Width = device - width, initial - scale = 1.0">
<title>hello vue</title>
<! Vue library -->
<script src="https://unpkg.com/vue@next"></script>
<! - style -- -- >
<style>
.v-leave-to..v-enter-from{
opacity: 0;
}
.v-leave-active..v-enter-active{
transition: opacity 3s ease-in;
}
.v-leave-from..v-enter-to{
opacity: 1;
}
</style>
</head>
<body>
<div id="root"></div>
</body>
<script></script>
</html>
Copy the code
The results
To get them in and out of order.
mode=”out-in”
<! DOCTYPEhtml>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="Width = device - width, initial - scale = 1.0">
<title>hello vue</title>
<! Vue library -->
<script src="https://unpkg.com/vue@next"></script>
<! - style -- -- >
<style>
.v-leave-to..v-enter-from{
opacity: 0;
}
.v-leave-active..v-enter-active{
transition: opacity 3s ease-in;
}
.v-leave-from..v-enter-to{
opacity: 1;
}
</style>
</head>
<body>
<div id="root"></div>
</body>
<script></script>
</html>
Copy the code
The results
The first time you render the page, you animate it
appear
<! DOCTYPEhtml>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="Width = device - width, initial - scale = 1.0">
<title>hello vue</title>
<! Vue library -->
<script src="https://unpkg.com/vue@next"></script>
<! - style -- -- >
<style>
.v-leave-to..v-enter-from{
opacity: 0;
}
.v-leave-active..v-enter-active{
transition: opacity 3s ease-in;
}
.v-leave-from..v-enter-to{
opacity: 1;
}
</style>
</head>
<body>
<div id="root"></div>
</body>
<script></script>
</html>
Copy the code
Running effect
Multiple single component animation switches
Writing 1. Similar to writing multiple but elements;
Use the :is command, and the previous use of the method is very similar;
<! DOCTYPEhtml>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="Width = device - width, initial - scale = 1.0">
<title>hello vue</title>
<! Vue library -->
<script src="https://unpkg.com/vue@next"></script>
<! - style -- -- >
<style>
.v-leave-to..v-enter-from{
opacity: 0;
}
.v-leave-active..v-enter-active{
transition: opacity 3s ease-in;
}
.v-leave-from..v-enter-to{
opacity: 1;
}
</style>
</head>
<body>
<div id="root"></div>
</body>
<script>
const app = Vue.createApp({
data(){return{currentItem: 'hello'}},
methods: {shift(){
if(this.currentItem === 'hello') {this.currentItem = 'bye'
}else{
this.currentItem = 'hello'}}},template: `
'
});
app.component("hello", {
template: `
hello world!
`
});
app.component("bye", {
template: `
bye world!
`
});
const vm = app.mount('#root');
</script>
</html>
Copy the code