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>

效果:

相关推荐
DT——1 小时前
Vite项目中eslint的简单配置
前端·javascript·代码规范
学习ing小白4 小时前
JavaWeb - 5 - 前端工程化
前端·elementui·vue
一只小阿乐4 小时前
前端web端项目运行的时候没有ip访问地址
vue.js·vue·vue3·web端
计算机学姐4 小时前
基于python+django+vue的旅游网站系统
开发语言·vue.js·python·mysql·django·旅游·web3.py
真的很上进4 小时前
【Git必看系列】—— Git巨好用的神器之git stash篇
java·前端·javascript·数据结构·git·react.js
胖虎哥er4 小时前
Html&Css 基础总结(基础好了才是最能打的)三
前端·css·html
qq_278063714 小时前
css scrollbar-width: none 隐藏默认滚动条
开发语言·前端·javascript
.ccl4 小时前
web开发 之 HTML、CSS、JavaScript、以及JavaScript的高级框架Vue(学习版2)
前端·javascript·vue.js
2301_765347545 小时前
Vue3 Day7-全局组件、指令以及pinia
前端·javascript·vue.js