鱼C论坛

 找回密码
 立即注册
查看: 2147|回复: 1

为啥实现不了两个盒子之间的隐藏,类似于top栏切换效果

[复制链接]
发表于 2022-6-12 22:52:13 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0">

  7.     <title>Document</title>
  8.     <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>

  9.     <style>
  10.         ul{
  11.             width:400px;
  12.             height:100px;
  13.             background-color:red;
  14.             display:flex;
  15.         }
  16.         ul li{
  17.             width:150px;
  18.             height:100px;
  19.             margin-left:20px;
  20.             background-color:green;
  21.             text-align:center;
  22.             line-height:100px;
  23.         }
  24.         .box1{
  25.             width:400px;
  26.             height:400px;
  27.             background-color:blue;
  28.             display:none;

  29.         }
  30.         .box2{
  31.             width:400px;
  32.             height:400px;
  33.             background-color:gray;
  34.             display:none;
  35.         }
  36.     </style>
  37. </head>
  38. <body>
  39.     <div id="root">
  40.         <ul>
  41.             <li @click="change1()">1</li>
  42.             <li @click="change2()">2</li>
  43.         </ul>
  44.         <div class="box1" v-show="a">1</div>
  45.         <div class="box2" v-show="b">2</div>
  46.     </div>
  47.     <script>
  48.     const vm=    new Vue({
  49.             el:'#root',
  50.             data:{
  51.                 a:'false',
  52.                 b:'false',
  53.             },
  54.           methods:{
  55.             change1(){
  56.                 this.a='true',
  57.                 this.b='false',
  58.                 alert('11');
  59.               console.log(this.a)

  60.             },
  61.             change2(){
  62.               this.b='true',
  63.               this.a='false',
  64.               alert('22');
  65.               console.log(this.b)

  66.             },

  67.           }
  68.         })
  69.     </script>
  70. </body>
  71. </html>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-6-30 17:27:10 | 显示全部楼层
修改后的代码:
  1. <!DOCTYPE html>
  2. <html lang="en">

  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0">

  7.     <title>Document</title>
  8.     <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>

  9.     <style>
  10.         ul {
  11.             width: 400px;
  12.             height: 100px;
  13.             background-color: red;
  14.             display: flex;
  15.         }

  16.         ul li {
  17.             width: 150px;
  18.             height: 100px;
  19.             margin-left: 20px;
  20.             background-color: green;
  21.             text-align: center;
  22.             line-height: 100px;
  23.         }

  24.         .box1 {
  25.             width: 400px;
  26.             height: 400px;
  27.             background-color: blue;
  28.             /* display:none; 不需要,是通过v-show决定是否要显示的*/

  29.         }

  30.         .box2 {
  31.             width: 400px;
  32.             height: 400px;
  33.             background-color: gray;
  34.             /* display:none; */
  35.         }
  36.     </style>
  37. </head>

  38. <body>
  39.     <div id="root">
  40.         <ul>
  41.             <li @click="change1()">1</li>
  42.             <li @click="change2()">2</li>
  43.         </ul>
  44.         <div class="box1" v-show="a">1</div>
  45.         <div class="box2" v-show="b">2</div>
  46.     </div>
  47.     <script>
  48.         const vm = new Vue({
  49.             el: '#root',
  50.             data: {
  51.                 // a:'false',
  52.                 // b:'false',
  53.                 // 去掉true和false外面的引号
  54.                 a: false,
  55.                 b: false,
  56.             },
  57.             methods: {
  58.                 change1() {
  59.                     // this.a='true',
  60.                     // this.b='false',
  61.                     // 去掉引号,逗号改成分号或者不要符号
  62.                     this.a = true;
  63.                     this.b = false;
  64.                     alert('11');
  65.                     console.log(this.a)

  66.                 },
  67.                 change2() {
  68.                     //   this.b='true',
  69.                     //   this.a='false',
  70.                     this.b = true;
  71.                     this.a = false;
  72.                     alert('22');
  73.                     console.log(this.b)

  74.                 },

  75.             }
  76.         })
  77.     </script>
  78. </body>

  79. </html>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-5 02:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表