vue简单实现tab切换

tab切换效果如下:

注:(图片用的public静态资源管理)
在这里插入图片描述

  1. 上方的标题和下方的图片用的是两个块,是兄弟节点,所以需要点击tab标题和下方的图片一一对应
  2. 利用vue提供的v-for指令遍历得到索引和值

html结构(两块结构):

<template>
   <div id="app">
      <!--  导航  -->
      <ul class="tab-tilte">
         <li v-for="(item,index) in title"
             :key="index"
             @click="num=index"
             :class="{active:num == index}"
         >
            {{item}}
         </li>
      </ul>
      <!--  内容  -->
      <ul class="tab-content">
         <li v-for="(item,index) in content"
             :key="index"
             v-show="num==index"
         >
            <img :src="item" alt="">
         </li>
      </ul>
   </div>
</template>

简单写一下css样式:

<style lang="scss" scoped>
   #app {
      ul li {
         list-style: none;
      }
      .tab-tilte {
         height: 50px;
         line-height: 50px;
         display: flex;
         justify-content: space-around;
         li {
            width: 20%;
            text-align: center;
            background-color: #eee;
            cursor: pointer; /* 鼠标变小手 */
         }
      }
      /* 动态绑定class 点击标题改变样式 */
      .tab-tilte .active {
         background-color: #ff1100;
         color: #fff;
      }
      img {
         width: 100%;
         height: 100%;
      }
   }
</style>

vue实现代码:

<script>
   export default {
      data() {
         return {
            title: ['标题一', '标题二', '标题三', '标题四', '标题五'],
            content: [
               "http://localhost:8080/images/car1.jpg",
               "http://localhost:8080/images/car2.jpg",
               "http://localhost:8080/images/car3.jpg",
               "http://localhost:8080/images/car4.jpg",
               "http://localhost:8080/images/car5.jpg",
            ],
            num: 0,
         }
      }
   }
</script>
  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值