用css样式写两个盒子,分别用v-if v-show还有事件指令做出显示隐藏效果
该回答内容部分引用GPT,GPT_Pro更好的解决问题
使用v-if v-show还有事件指令做出显示隐藏效果,首先要在css样式中写两个盒子,分别是显示盒子和隐藏盒子。
在HTML中,使用v-if来实现显示和隐藏的功能,将显示盒子和隐藏盒子的v-if放在不同的位置,然后使用一个变量show来控制,当show的值为true时,显示盒子就会显示出来,而隐藏盒子就会隐藏;当show的值为false时,显示盒子就会隐藏,而隐藏盒子就会显示出来。
<div id="app">
<div v-if="show">显示盒子</div>
<div v-else>隐藏盒子</div>
</div>
另外一种方法是使用v-show来实现显示隐藏的效果,只需要在HTML中将show变量放在v-show的位置即可。当show的值为true时,显示盒子就会显示出来,而隐藏盒子就会隐藏;当show的值为false时,显示盒子就会隐藏,而隐藏盒子就会显示出来。
<div id="app">
<div v-show="show">显示盒子</div>
<div v-show="!show">隐藏盒子</div>
</div>
最后一种方法是使用事件指令来实现显示隐藏效果。在HTML中使用@click事件来控制变量show的值,当show的值为true时,显示盒子就会显示出来,而隐藏盒子就会隐藏;当show的值为false时,显示盒子就会隐藏,而隐藏盒子就会显示出来。
<div id="app">
<div @click="show=true">显示盒子</div>
<div @click="show=false">隐藏盒子</div>
</div>
如果回答有帮助,望采纳。
以下答案引用自GPT-3大模型,请合理使用:
示例
<template>
<div>
<div class="box-1" v-show="showBox1">Box 1</div>
<div class="box-2" v-if="showBox2">Box 2</div>
<input type="button" @click="handleClick" value="切换"/>
</div>
</template>
<script>
export default {
data() {
return {
showBox1: true,
showBox2: false
}
},
methods: {
handleClick(){
this.showBox1 = !this.showBox1;
this.showBox2 = !this.showBox2;
}
}
}
</script>
<style>
.box-1{
width: 200px;
height: 200px;
background: #ccc;
}
.box-2{
width: 200px;
height: 200px;
background: #333;
}
</style>
如果我的回答解决了您的问题,请采纳我的回答