Animation in Vue
1. Use Vue to achieve basic CSS transitions and animations
Transitions: For example, a div’s background color changes from red to green.
Animation: for example, a div moving from left to right is called animation;
animation
<! 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>
@keyframes leftToRight {
0% {
transform: translateX(-100px);
}
50% {
transform: translateX(-50px);
}
100% {
transform: translateX(0px); }}.animation{
animation: leftToRight 3s;
}
</style>
</head>
<body>
<div id="root"></div>
</body>
<script>
const app = Vue.createApp({
data(){
return{
animate: {
animation: false}}},methods: {open(){
this.animate.animation = !this.animate.animation; }},template: `
hello world! < / div > < button @ click = "open" > start animation < / button > < / div > `
});
const vm = app.mount('#root');
</script>
</html>
Copy the code
The results
The transition
<! 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>
.transition{
transition: 3s background-color ease;
}
.blue{
background: blue;
}
.red{
background: red;
}
</style>
</head>
<body>
<div id="root"></div>
</body>
<script>
const app = Vue.createApp({
data(){
return{
animate: {
transition: true.blue: true.red: false}}},methods: {shift(){
this.animate.red = !this.animate.red; }},template: `
hello world! < / div > < button @ click = "shift" > switch < / button > < / div > `
});
const vm = app.mount('#root');
</script>
</html>
Copy the code
The results
The transition is achieved by binding styles
<! 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>
.transition{
transition: 3s background-color ease;
}
</style>
</head>
<body>
<div id="root"></div>
</body>
<script>
const app = Vue.createApp({
data(){
return{
styleObject: {
background: 'red'}}},methods: {shift(){
if(this.styleObject.background === 'red') {this.styleObject.background = 'blue';
}else{
this.styleObject.background = 'red'; }}},template: `
hello world! < / div > < button @ click = "shift" > switch < / button > < / div > `
});
const vm = app.mount('#root');
</script>
</html>
Copy the code