vue 实现tab菜单切换

1、目标:

实现切换tab菜单,激活状态,按钮高亮,显示对应的菜单内容

2、实现

javascript 复制代码
<template>
  <div class="tan_menu">
    <ul class="container">
      <li
        class="item"
        v-for="item in tab_menu"
        :key="item.type"
        :class="{ active: current_menu === item.type }"
        @click="selectMenu(item.type)"
      >
        {{ item.label }}
      </li>
      <!-- <li class="item" :class="{ actice: current_menu === 'login' }">登录</li>
      <li class="item" :class="{ actice: current_menu === 'register' }">
        注册
      </li> -->
    </ul>
    <template v-if="current_menu === 'login'">
      <div class="login">这是--登录--内容</div>
    </template>
    <template v-if="current_menu === 'register'">
      <div class="register">这是--注册--内容</div>
    </template>
  </div>
</template>

<script>
export default {
  name: "TabMenu",
  data() {
    return {
      tab_menu: [
        { type: "login", label: "登录" },
        { type: "register", label: "注册" },
      ],
      current_menu: "login",
    };
  },
  methods: {
    selectMenu(type) {
      this.current_menu = type;
    },
  },
};
</script>

<style lang="less" scoped>
// 清除内外边距
* {
  margin: 0;
  padding: 0;
}
.tan_menu {
  // margin: 20px 20px 0;
  .container {
    list-style: none;
    margin: 100px auto;
    width: 200px;
    height: 40px;
    line-height: 40px;
    text-align: center;
    display: flex;
    justify-content: space-around;
    background-color: skyblue;
    .item {
      width: 100px;
      cursor: pointer;
      &.active {
        background-color: pink;
        color: #fff;
      }
    }
  }
  .login,
  .register {
    width: 300px;
    height: 100px;
    line-height: 100px;
    text-align: center;
    border: 2px solid pink;
    margin: 20px auto;
  }
}
</style>

效果:

相关推荐
2401_8784545335 分钟前
html和css的复习(1)
前端·css·html
@PHARAOH1 小时前
WHAT - git worktree 概念
前端·git
IT_陈寒2 小时前
我竟然被JavaScript的隐式类型转换坑了三天!
前端·人工智能·后端
我亚索贼六丶2 小时前
二十六. AI基础概念之如何更好的使用AI
前端
小码哥_常2 小时前
安卓启动页Logo适配秘籍:告别“奇形怪状”的展示
前端
我亚索贼六丶2 小时前
二十五.Electron 初体验与进阶
前端
当时只道寻常2 小时前
像使用 Redis 一样操作 LocalStorage
前端·前端工程化
RONIN2 小时前
UI组件库elementplus
前端
汤姆Tom2 小时前
从 0 到 1 开发项目?你是否也是这样开始?先有再优化一步一步带你了解架构设计
前端·后端·架构
review445432 小时前
基于 Cursor 实现智能代码审查skill
前端