vue中声明式开发选项卡

选项卡案例

  • 第一:css样式
* {
      margin: 0;
      padding: 0;
    }

    .tab ul {
      overflow: hidden;
      padding: 0;
      margin: 0;
    }

    .tab ul li {
      box-sizing: border-box;
      padding: 0;
      float: left;
      width: 100px;
      height: 45px;
      line-height: 45px;
      list-style: none;
      text-align: center;
      border-top: 1px solid blue;
      border-right: 1px solid blue;
      cursor: default
    }

    .tab ul li:first-child {
      border-left: 1px solid blue;
    }

    .tab ul li.active {
      background-color: orange;
    }

    .tab div {
      width: 500px;
      height: 300px;
      display: none;
      text-align: center;
      font-size: 30px;
      line-height: 300px;
      border: 1px solid blue;
      border-top: 0px;
    }

    .tab div.current {
      display: block;
    }
  • 第二:结构html
<div id="app">
    <div class="tab">
      <ul>
        <li class="active">apple</li>
        <li>orange</li>
        <li>lemon</li>
      </ul>
      <div class="current">
        <img src="img/apple.png">
      </div>
      <div>
        <img src="img/orange.png">
      </div>
      <div>
        <img src="img/lemon.png">
      </div>
    </div>
  </div>
  • 第三:js逻辑
var vm = new Vue({
      el: '#app',
      data: {
      },
    methods: {
     }
)}
下面是解答结果 html结构和js代码

1、html结构

<div id="app">
    <div class="tab">
      <!--  tab栏  -->
      <ul>
        <!-- 动态绑定class   有 active   类名高亮  无 active   不高亮-->
        <li @click="change(index)" :class='currentIndex==index?"active":""' v-for="(item, index) in list"
          :key="item.id">
          {{item.title}}</li>
      </ul>
      <!--  对应显示的图片 -->
      <!-- 动态绑定class   有 current  类名显示  无 current  隐藏-->
      <div :class="currentIndex==index?'current':''" v-for="(item, index) in list" :key="item.id"><img :src="item.path">
      </div>
    </div>
  </div>

2、js逻辑

let vm = new Vue({
      el: '#app',
      data: {
        currentIndex: 0,// 选项卡当前的索引  默认为 0  
        list: [{
          id: 1,
          title: 'apple',
          path: 'img/apple.png'
        },
        {
          id: 2,
          title: 'orange',
          path: 'img/orange.png'
        },
        {
          id: 3,
          title: 'lemon',
          path: 'img/lemon.png'
        }]
      },
      methods: {
        // 通过传入过来的索引来让当前的  currentIndex  和点击的index 值 相等 
        //  从而实现 控制类名    
        change(index) {
          this.currentIndex = index
        }
      }
    })

选项卡视图
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值