vue动态新增修改删除class切换面板

前言

  • 界面是这样的
    在这里插入图片描述

  • 我想实现的效果,如果切换之后下面有一条横线。

代码实现

  • HTML代码
 <div :class="tabMenuItemClass(0)" @click="changePanel(0)">面板一</div>
 <div :class="tabMenuItemClass(1)" @click="changePanel(1)">面板二</div>
 <div :class="tabMenuItemClass(2)" @click="changePanel(2)">面板三</div>
...省略CSS...
  • js代码
 ...省略...
   data() {
    return {
    // 先声明选择哪个面板,默认0就第一个
      activePanel: 0,
    };
  },
  ... 省略...

  methods: {
  //样式
    tabMenuItemClass(index) {
      const normal = "tab_item_text";
      const active = "tab_item_text tab_item_text_active";
      // 如果相等
      if (this.activePanel === index) {
      	// 则使用选中的样式
        return active;
      }
      return normal;
    },
    // 切换面板
    changePanel(index) {
      this.activePanel = index;
    },
  },
  
  • css
  .tab_item_text {
 	position: relative;
     color: #707070;
     font-size: 18px;
     margin-right: 30px;
     padding-bottom: 15.5px;

     -webkit-box-align: center;
     align-items: center;
     display: inline-flex;
     font-weight: 600;
     transition: border .2s linear;
     width: max-content;
     }
 .tab_item_text_active {
          border-bottom: 2px solid;
          text-decoration: none;
           color: #191919;
 }
  • 效果
    在这里插入图片描述

posted on   愤怒的苹果ext  阅读(331)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示