当前位置: 首页 > news >正文

一键复制:基于vue实现的tab切换效果

需求:顶部栏有切换功能,内容区域随顶部切换而变化

目录

    • 实现效果
    • 实现代码
    • 使用示例
    • 在线预览

实现效果

如下
在这里插入图片描述

实现代码

组件代码 MoTab.vue

<template><div class="mo-tab"><divv-for="item in options"class="mo-tab__item":class="{'mo-tab__item--active': item.value == currentValue,}"@click="handleChange(item.value)">{{ item.label }}</div></div>
</template><script>
// created at 2024-05-08
export default {name: "MoTab",props: {// 选项// eg: [{label: '显示值', value: '真实值'}]options: {type: Array,default: () => {return [];},},// 选定值value: {type: String,default: "",},},computed: {currentValue: {get() {return this.value;},set(val) {this.$emit("input", val);},},},methods: {handleChange(value) {this.currentValue = value;this.$emit("on-change", value);},},
};
</script><style lang="less">
.mo-tab {display: flex;margin-left: 20px;
}.mo-tab__item {position: relative;padding: 0 10px;line-height: 30px;font-size: 13px;color: #666;cursor: pointer;
}.mo-tab__item:hover,
.mo-tab__item--active {color: #111;
}.mo-tab__item--active::before {content: "";position: absolute;height: 2px;width: 16px;left: 50%;transform: translateX(-50%);bottom: 0;background-color: #6833cf;
}</style>

使用示例

<template><div class="app"><MoTab:options="options"v-model="value"@on-change="handleValueChange"></MoTab><div class="content"><p>选定真实值: {{ value }}</p><p>选定显示值: {{ currentLabel }}</p></div></div>
</template><script>
// created at 2023-03-31
import MoTab from "./components/MoTab/MoTab.vue";export default {name: "App",props: {},components: {MoTab,},data() {return {// 选定值value: "1",// 选项options: [{value: "1",label: "蔬菜",},{value: "2",label: "肉类",},{value: "3",label: "水果",},],};},computed: {currentLabel() {return this.options.find((item) => item.value === this.value)?.label;},},methods: {handleValueChange(value) {console.log("value change: ", value);},},created() {},
};
</script><style lang="less">
.app {width: 200px;margin: 0 auto;border: 1px solid #ccc;padding: 20px;
}.content {margin-top: 20px;border-top: 1px solid #ccc;
}
</style>

在线预览

https://mouday.github.io/vue-demo/packages/vue-mo-tab/dist/index.html

http://www.lryc.cn/news/344280.html

相关文章:

  • 新手做抖音小店,卖什么最容易出单?抖音必爆类目来了!
  • 男人圣经 10
  • 如何让路由器分配固定网段(网络号)ip
  • Q1保健品线上市场分析(三):牛初乳市场扩张,同比去年增长54%
  • 使用docker-compose编排Lnmp(dockerfile) 完成Wordpress
  • 母婴店运用商城小程序店铺的效果是什么
  • 大数据技术概述_2.大数据面临的5个方面的挑战
  • 《动手学深度学习(Pytorch版)》Task03:线性神经网络——4.29打卡
  • 机器学习(二) ----------K近邻算法(KNN)+特征预处理+交叉验证网格搜索
  • This error originates from a subprocess, and is likely not a problem with pip.
  • Python中关于子类约束的开发规范
  • Isaac Sim 4 键盘控制小车前进方向(学习笔记5.8.2)
  • ​「Python绘图」绘制太极图
  • 解决html2canvas生成图片慢的问题
  • 模型智能体开发之metagpt-多智能体实践
  • Java | Leetcode Java题解之第67题二进制求和
  • 考过PMP之后,为什么建议学CSPM?
  • 智能合约是什么?搭建与解析
  • windows下安装最新的nginx
  • 【深耕 Python】Data Science with Python 数据科学(19)书402页练习题:模型准确率对比研究、KMeans算法的一点探讨
  • 汽车品牌区域营销方案
  • matlab 中在3维坐标系中绘制一个点的X,Y,Z坐标,除了mesh还有什么函数?使用格式与mesh都有什么区别?
  • 如何在六个月内学会任何一门外语(ted转述)
  • 前端 Android App 上架详细流程 (Android App)
  • MySQL学习笔记11——数据备份 范式 ER模型
  • 软件测试基础理论复习
  • 【UnityRPG游戏制作】Unity_RPG项目_玩家逻辑相关
  • QT_BEGIN_NAMESPACE
  • Swift 集合类型
  • string容器