实现点击Animate,上面的方块移动
示例
步骤
1、为Animate按钮添加@click方法animateBlock
<button @click="animateBlock">Animate</button>
2、添加animatedBlock变量控制是否可以移动
data() {
return {
animatedBlock: false,
dialogIsVisible: false
};
},
3、实现animateBlock方法
animateBlock() {
this.animatedBlock = !this.animatedBlock;
},
4、再绑定一个class给方块
<div class="block" :class="{ animate: animatedBlock }"></div>
5、新建class animate
.animate {
transform: translateX(-150px);
}
6、为了实现动画效果,在.block中添加transition
.block {
width: 8rem;
height: 8rem;
background-color: #290033;
margin-bottom: 2rem;
transition: transform 0.3s ease-out;
}
完整代码
BaseModal.vue
<template>
<div class="backdrop" @click="$emit('close')"></div>
<dialog open>
<slot></slot>
</dialog>
</template>
<script>
export default {
emits: ['close'],
};
</script>
<style scoped>
.backdrop {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100vh;
z-index: 10;
background-color: rgba(0, 0, 0, 0.75);
}
dialog {
position: fixed;
top: 30vh;
width: 30rem;
left: calc(50% - 15rem);
margin: 0;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.26);
border-radius: 12px;
padding: 1rem;
background-color: white;
z-index: 100;
border: none;
}
</style>
App.vue
<template>
<div class="container">
<div class="block" :class="{ animate: animatedBlock }"></div>
<button @click="animateBlock">Animate</button>
</div>
<base-modal @close="hideDialog" v-if="dialogIsVisible">
<p>This is a test dialog!</p>
<button @click="hideDialog">Close it!</button>
</base-modal>
<div class="container">
<button @click="showDialog">Show Dialog</button>
</div>
</template>
<script>
export default {
data() {
return {
animatedBlock: false,
dialogIsVisible: false
};
},
methods: {
animateBlock() {
this.animatedBlock = !this.animatedBlock;
},
showDialog() {
this.dialogIsVisible = true;
},
hideDialog() {
this.dialogIsVisible = false;
},
},
};
</script>
<style>
* {
box-sizing: border-box;
}
html {
font-family: sans-serif;
}
body {
margin: 0;
}
button {
font: inherit;
padding: 0.5rem 2rem;
border: 1px solid #810032;
border-radius: 30px;
background-color: #810032;
color: white;
cursor: pointer;
}
button:hover,
button:active {
background-color: #a80b48;
border-color: #a80b48;
}
.block {
width: 8rem;
height: 8rem;
background-color: #290033;
margin-bottom: 2rem;
transition: transform 0.3s ease-out;
}
.container {
max-width: 40rem;
margin: 2rem auto;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
padding: 2rem;
border: 2px solid #ccc;
border-radius: 12px;
}
.animate {
transform: translateX(-150px);
}
</style>
main.js
import { createApp } from 'vue';
import App from './App.vue';
import BaseModal from './components/BaseModal.vue';
const app = createApp(App);
app.component('base-modal', BaseModal);
app.mount('#app');
标签:vue,animatedBlock,color,width,181,background,Transitions,border,Animation
From: https://blog.csdn.net/KevinHuang2088/article/details/142150617