uniapp v-tabs修改了几项功能,根据自己需求自己改
根据自己的需求都可以改
这里写自定义目录标题
- 1.数组中的名字过长,导致滑动异常
- 2.change 事件拿不到当前点击的数据,通过index在原数组中查找得到所需要的id 各种字段麻烦
- 3.添加指定下标下新加红点显示样式

1.数组中的名字过长,导致滑动异常
2.change 事件拿不到当前点击的数据,通过index在原数组中查找得到所需要的id 各种字段麻烦
直接帮点前点击的传上来 change方法中 直接获取 item ,index
<v-tabs v-model="current" v-model:tipsindex='tipsindex' ellipsisNums='3' v-model:tipsnums='tipsnums' isEllipsis :tabs="tabs" @change="changeTab">
const changeTab =(item,index)=>{console.log(item)console.log(index)
}
3.添加指定下标下新加红点显示样式
一般情况下只能会在某一项上添加红点,所以只用传显示的下标以及数量,每一项都显示的话这种场景直接在作者的源码里v-tabs.vue
里面 slot
这里直接添加一个布局,显示每一列的数量就行,
直觉贴代码
props.js 里面新加
// 名字过长是否缩写isEllipsis: {type: Boolean,default: false},// 超过数量ellipsisNums: {type: [Number, String],default: 5},// 红点显示的下标tipsindex: {type: [Number, String],default: 0},// 红点显示的数量tipsnums: {type: [Number, String],default: 0},// 红点背景颜色tipbg: {type:String,default: 'red'},// 红点颜色tipcolor: {type:String,default: '#ff'}
v-tabs.vue 直接复制
<template><view class="v-tabs"><scroll-view:id="getDomId":scroll-x="scroll":scroll-left="scroll ? scrollLeft : 0":scroll-with-animation="scroll":style="{ position: fixed ? 'fixed' : 'relative', zIndex, width: '100%' }"><viewclass="v-tabs__container":style="{display: scroll ? 'inline-flex' : 'flex',whiteSpace: scroll ? 'nowrap' : 'normal',background: bgColor,height,padding}"><view:class="['v-tabs__container-item', { disabled: !!v.disabled }, { active: current == i }]"v-for="(v, i) in tabs":key="i":style="{color: current == i ? activeColor : color,fontSize: current == i ? fontSize : fontSize,fontWeight: bold && current == i ? 'bold' : '',justifyContent: !scroll ? 'center' : '',flex: scroll ? '' : 1,padding: paddingItem}"@click="change(v,i)"><slot :row="v" :index="i"><view class="name">{{ field ? limitText( v[field]) : limitText(v) }}</view><view class="tip" v-if="tipsindex===i">{{tipsnums}}</view></slot></view><template v-if="!!tabs.length"><viewv-if="!pills":class="['v-tabs__container-line', { animation: lineAnimation }]":style="{background: lineColor,width: lineWidth + 'px',height: lineHeight,borderRadius: lineRadius,transform: `translate3d(${lineLeft}px, 0, 0)`}" /><viewv-else:class="['v-tabs__container-pills', { animation: lineAnimation }]":style="{background: pillsColor,borderRadius: pillsBorderRadius,width: currentWidth + 'px',transform: `translate3d(${pillsLeft}px, 0, 0)`,height}" /></template></view></scroll-view><!-- fixed 的站位高度 --><view class="v-tabs__placeholder" :style="{ height: fixed ? height : '0', padding }"></view></view>
</template><script>
import { startMicroTask, throttle } from './utils'
import props from './props'
/*** v-tabs* @property {Number} value 选中的下标* @property {Array} tabs tabs 列表* @property {String} bgColor = '#fff' 背景颜色* @property {String} color = '#333' 默认颜色* @property {String} activeColor = '#2979ff' 选中文字颜色* @property {String} fontSize = '28rpx' 默认文字大小* @property {String} activeFontSize = '28rpx' 选中文字大小* @property {Boolean} bold = [true | false] 选中文字是否加粗* @property {Boolean} scroll = [true | false] 是否滚动* @property {String} height = '60rpx' tab 的高度* @property {String} lineHeight = '10rpx' 下划线的高度* @property {String} lineColor = '#2979ff' 下划线的颜色* @property {Number} lineScale = 0.5 下划线的宽度缩放比例* @property {String} lineRadius = '10rpx' 下划线圆角* @property {Boolean} pills = [true | false] 是否胶囊样式* @property {String} pillsColor = '#2979ff' 胶囊背景色* @property {String} pillsBorderRadius = '10rpx' 胶囊圆角大小* @property {String} field 如果是对象,显示的键名* @property {Boolean} fixed = [true | false] 是否固定* @property {String} paddingItem = '0 22rpx' 选项的边距* @property {Boolean} lineAnimation = [true | false] 下划线是否有动画* @property {Number} zIndex = 1993 默认层级* @property {Boolean} isEllipsis = [true | false] 名字过长是否简写...* @property {Boolean} ellipsisNums = 名字超过几个* @property {Boolean} tipsindex = 红点要显示的下标* @property {Boolean} tipsnums = 红点要显示的数量 * @event {Function(current)} change 改变标签触发*/
export default {name: 'VTabs',props,// #ifdef VUE3emits: ['update:modelValue', 'change'],// #endifdata() {return {lineWidth: 30,currentWidth: 0, // 当前选项的宽度lineLeft: 0, // 滑块距离左侧的位置pillsLeft: 0, // 胶囊距离左侧的位置scrollLeft: 0, // 距离左边的位置container: { width: 0, height: 0, left: 0, right: 0 }, // 容器的宽高,左右距离current: 0, // 当前选中项scrollWidth: 0 // 可以滚动的宽度}},computed: {getDomId() {const len = 16const $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678' /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/const maxPos = $chars.lengthlet pwd = ''for (let i = 0; i < len; i++) {pwd += $chars.charAt(Math.floor(Math.random() * maxPos))}return `xfjpeter_${pwd}`}},watch: {// #ifdef VUE3modelValue: {// #endif// #ifdef VUE2value: {// #endifimmediate: true,handler(newVal) {this.current = newVal > -1 && newVal < this.tabs.length ? newVal : 0this.$nextTick(this.update)}}},methods: {limitText(val) {val += ''if (!val) returnif (!this.isEllipsis) return valreturn val.length > this.ellipsisNums ? `${val.slice(0, this.ellipsisNums)}...` : val},// 切换事件change: throttle(function(item,index) {const isDisabled = !!this.tabs[index].disabledif (this.current !== index && !isDisabled) {this.current = index// #ifdef VUE3this.$emit('update:modelValue', index)// #endif// #ifdef VUE2this.$emit('input', item,index)// #endifthis.$emit('change', item,index)}}, 300),createQueryHandler() {let query// #ifndef MP-ALIPAYquery = uni.createSelectorQuery().in(this)// #endif// #ifdef MP-ALIPAYquery = uni.createSelectorQuery()// #endifreturn query},update() {const _this = thisstartMicroTask(() => {// 没有列表的时候,不执行if (!this.tabs.length) return_this.createQueryHandler().select(`#${this.getDomId}`).boundingClientRect(data => {const { width, height, left, right } = data || {}// 获取容器的相关属性this.container = { width, height, left, right: right - width }_this.calcScrollWidth()_this.setScrollLeft()_this.setLine()}).exec()})},// 计算可以滚动的宽度calcScrollWidth(callback) {const view = this.createQueryHandler().select(`#${this.getDomId}`)view.fields({ scrollOffset: true })view.scrollOffset(res => {if (typeof callback === 'function') {callback(res)} else {// 获取滚动条的宽度this.scrollWidth = res.scrollWidth}}).exec()},// 设置滚动条滚动的进度setScrollLeft() {this.calcScrollWidth(res => {// 动态读取 scrollLeftlet scrollLeft = res.scrollLeftthis.createQueryHandler().select(`#${this.getDomId} .v-tabs__container-item.active`).boundingClientRect(data => {if (!data) return// 除开当前选项外容器的一半宽度let curHalfWidth = (this.container.width - data.width) / 2let scrollDiff = this.scrollWidth - this.container.width// 在原有滚动条的基础上 + (当前元素距离左侧的距离 - 计算的一半宽度) - 容器的外边距之类的scrollLeft += data.left - curHalfWidth - this.container.left// 已经滚动在左侧了if (scrollLeft < 0) scrollLeft = 0// 已经超出右侧了else if (scrollLeft > scrollDiff) scrollLeft = scrollDiffthis.scrollLeft = scrollLeft}).exec()})},setLine() {this.calcScrollWidth(res => {const scrollLeft = res.scrollLeftthis.createQueryHandler().select(`#${this.getDomId} .v-tabs__container-item.active`).boundingClientRect(data => {if (!data) returnif (this.pills) {this.currentWidth = data.widththis.pillsLeft = scrollLeft + data.left - this.container.left} else {this.lineWidth = data.width * this.lineScalethis.lineLeft = scrollLeft + data.left + (data.width - data.width * this.lineScale) / 2 - this.container.left}}).exec()})}}
}
</script><style lang="scss" scoped>
.v-tabs__container-item{position: relative !important;.tip{width: 14px;height: 14px;background: red;position: absolute;top: 0px;right: 0px;text-align: center;line-height: 14px;border-radius: 6px;color: #fff;}
}
.v-tabs {width: 100%;box-sizing: border-box;overflow: hidden;/* #ifdef H5 */::-webkit-scrollbar {display: none;}/* #endif */&__container {min-width: 100%;position: relative;display: inline-flex;align-items: center;white-space: nowrap;overflow: hidden;&-item {flex-shrink: 0;display: flex;align-items: center;height: 100%;position: relative;z-index: 10;transition: all 0.3s;white-space: nowrap;&.disabled {opacity: 0.5;color: #999;}}&-line {position: absolute;left: 0;bottom: 0;}&-pills {position: absolute;z-index: 9;}&-line,&-pills {&.animation {transition: all 0.3s linear;}}}
}</style>
组件中使用 index.vue
<template><view><!-- <son4></son4>--><view class="main"><view class="left"><v-tabs v-model="current" v-model:tipsindex='tipsindex' ellipsisNums='3' v-model:tipsnums='tipsnums' isEllipsis :tabs="tabs" @change="changeTab"></v-tabs></view><view class="right">站位</view></view></view>
</template><script setup>
import { ref,provide} from 'vue'
// import son4 from "../../components/son4.vue";
// let toChildValue = ref('传递给所有子集的数据')
// provide( 'toChildValue', toChildValue)let current = ref(0)
let tipsindex = ref(2)
let tipsnums = ref(2)
let tabs = ref([])// 模拟请求数据
setTimeout(()=>{tabs.value = ['军事', '国内', '新闻新闻新闻新闻新闻新闻新闻新闻', '军事', '国内', '新闻', '军事', '国内', '新闻']
},1500)
const changeTab =(item,index)=>{console.log(item)console.log(index)
}
// 模拟修改红点数量
setTimeout(()=>{tipsnums.value = 6
},6000)</script><style>
.main{width: 100%;height: 40px;background: pink;display: flex;
}
.left{flex: 1;width: 0;height: 40px;
}
.right{width: 100px;height: 40px;background: springgreen;
}
</style>
相关文章:

uniapp v-tabs修改了几项功能,根据自己需求自己改
根据自己的需求都可以改 这里写自定义目录标题 1.数组中的名字过长,导致滑动异常2.change 事件拿不到当前点击的数据,通过index在原数组中查找得到所需要的id 各种字段麻烦3.添加指定下标下新加红点显示样式 1.数组中的名字过长,导致滑动异常…...
用vscode,进行vue开发
使用Visual Studio Code(VSCode)进行Vue.js开发是一个很好的选择,因为VSCode提供了强大的编辑功能以及丰富的插件生态。以下是使用VSCode进行Vue开发的基本步骤: 1. 安装Node.js和npm 首先,确保你的计算机上安装了No…...
Kafka 磁道寻址过程详解
前言 Apache Kafka 是一款高吞吐、分布式的消息流平台,广泛应用于实时数据处理和事件驱动系统。在 Kafka 中,消息是存储在磁盘上的,这种高效的数据读写性能得益于 Kafka 独特的磁盘存储架构和寻址机制。本文将从 Kafka 的存储结构、磁道寻址…...

基于Spring Boot的社区药房系统
一、系统背景与目的 随着医疗改革的深入和社区医疗服务的不断完善,社区药房在居民健康保障中扮演着越来越重要的角色。然而,传统的药房管理方式存在着库存管理混乱、药品销售不透明、客户信息管理不规范等问题。为了解决这些问题,基于Spring…...

005 QT常用控件Qwidget_上
文章目录 前言控件概述QWidgetenable属性geometry属性windowTitle属性windowlcon属性 小结 前言 本文将会向你介绍常用的Qwidget属性 控件概述 Widget 是 Qt 中的核心概念. 英文原义是 “⼩部件”, 我们此处把它翻译为 “控件” . 控件是构成⼀个图形化界面的基本要素. QWi…...

机器学习之交叉熵
交叉熵(Cross-Entropy)是机器学习中用于衡量预测分布与真实分布之间差异的一种损失函数,特别是在分类任务中非常常见。它源于信息论,反映了两个概率分布之间的距离。 交叉熵的数学定义 对于分类任务,假设我们有&#…...

数据结构 ——前缀树查词典的实现
数据结构 ——前缀树查词典的实现 一、前缀树的概念 前缀树是一种多叉树结构,主要用于存储字符串。每个节点代表一个字符,路径从根节点到叶节点表示一个完整的字符串。前缀树的关键特征是 共享前缀,也就是说,如果两个字符串有相…...
MySQL 主从复制与高可用架构
一、MySQL 主从复制概述 (一)定义与作用 MySQL 主从复制是一种允许在多个 MySQL 数据库服务器之间进行数据同步的技术。简单来说,就是可以把数据从一个 MySQL 服务器(主服务器、主节点)复制到一个或多个从节点&#…...
【Golang】如何读取并解析SQL文件
一、背景 在数据库开发与维护过程中,我们经常需要执行大量的SQL语句。有时,这些SQL语句会被保存在一个文件中,以便于批量执行。为了方便地在Go语言中处理这些SQL文件,我们可以编写一个函数来读取并解析SQL文件中的语句。 二、实…...

git branch -r(--remotes )显示你本地仓库知道的所有 远程分支 的列表
好的,git branch -r 这个命令用于列出远程分支。让我详细解释一下: 命令: git branch -rdgqdgqdeMac-mini ProductAuthentication % git branch -rorigin/main作用: 这个命令会显示你本地仓库知道的所有 远程分支 的列表。它不…...
Typescript安装
建议全局安装npm i -g typescript安装好之后,就可以直接使用 tsc 来编译 ts 文件了可通过 tsc 回车查看 tsc 的各项配置信息,通过 tsc --version 查看版本号。编译我们现在可以创建一个 ts 文件,并将他编译成 js 文件,比如下面简单…...

使用C#在目录层次结构中搜索文件以查找目标字符串
例程以递归方式搜索目录层次结构中的文件以查找目标字符串。它可以搜索几乎任何类型的文件,即使它不包含 Windows 理解的文本。例如,它可以搜索 DLL 和可执行文件以查看它们是否恰好包含字符串。 下面的代码中显示的ListFiles 方法完成了大部分工作。 …...

基于Redis实现令牌桶算法
基于Redis实现令牌桶算法 令牌桶算法算法流程图优点缺点 实现其它限流算法 令牌桶算法 令牌桶是一种用于分组交换和电信网络的算法。它可用于检查数据包形式的数据传输是否符合定义的带宽和突发性限制(流量不均匀或变化的衡量标准)。它还可以用作调度算…...

[Java] 使用 VSCode 来开发 Java
目录 前言Java 环境怎么看自己是否已经配置完成?安装 JDK安装 Maven 环境修改 Maven 依赖源 完善 VS Code配置插件配置 Maven配置 Maven Settings配置 Maven 可执行文件地址 前言 由于使用 VSCode 编码已经成为习惯,并且它确实相对其他的 IDE 较为轻量化…...

奇怪的知识又增加了,ESP32下的Lisp编程:ULisp--Lisp for microcontrollers
ESP32下有MicroPython,那么我就在想,有Lisp语言支持吗?答案是果然有!有ULisp,专门为MCU设计的Lisp! 网址:uLisp - Lisp for microcontrollers 介绍:用于微控制器的 Lisp 适用于 Ar…...

STM32标准库学习之寄存器方法点亮LED灯
STM32C8T6最小系统开发板,点亮PC13引脚的LED灯 1.使能PC13引脚的定时器 PC13引脚为GPIOC组的第13个端口,GPIO的时钟使能定时器为RCC_APB2ENR,这是可以从手册中得出的,如下图所示 从下图可以得出,若要使能GPIOC端口&a…...

Jenkins:持续集成与持续部署的利器
🐇明明跟你说过:个人主页 🏅个人专栏:《未来已来:云原生之旅》🏅 🔖行路有良友,便是天堂🔖 目录 一、引言 1、什么是Jenkins 2、Jenkins的起源 二、Jenkins的核心…...

概率论得学习和整理30: 用EXCEL 描述泊松分布 poisson distribution
目录 1 泊松分布的基本内容 1.1 泊松分布的关键点 1.1.1 属于离散分布 1.1.2 泊松分布的特点:每个子区间内概率相等 , λ就是平均概率 1.2 核心参数 1.3 pmf公式 1.4 期望和方差 2 例1:用EXCEL计算泊松分布的概率 3 比较λ不同值时…...

汽车SoC芯片及其安全岛设计与未来发展趋势(学习笔记)
SoC系列已发布多篇文章,之前应该发布到4.3章节,后续还有包含常见汽车SoC,SoC评价指标,产业链及发展趋势等,均见已发布完整版本付费资源,链接如下: 汽车SoC芯片及其安全岛设计与未来发展趋势&am…...

【排序算法】——选择排序
前言 排序(Sorting) 是计算机程序设计中的一种重要操作,它的功能是将一个数据元素(或记录)的任意序列,重新排列成一个关键字有序的序列。所谓排序,就是使一串记录,按照其中的某个或某些关键字的大小&#x…...

springboot 百货中心供应链管理系统小程序
一、前言 随着我国经济迅速发展,人们对手机的需求越来越大,各种手机软件也都在被广泛应用,但是对于手机进行数据信息管理,对于手机的各种软件也是备受用户的喜爱,百货中心供应链管理系统被用户普遍使用,为方…...
前端倒计时误差!
提示:记录工作中遇到的需求及解决办法 文章目录 前言一、误差从何而来?二、五大解决方案1. 动态校准法(基础版)2. Web Worker 计时3. 服务器时间同步4. Performance API 高精度计时5. 页面可见性API优化三、生产环境最佳实践四、终极解决方案架构前言 前几天听说公司某个项…...
镜像里切换为普通用户
如果你登录远程虚拟机默认就是 root 用户,但你不希望用 root 权限运行 ns-3(这是对的,ns3 工具会拒绝 root),你可以按以下方法创建一个 非 root 用户账号 并切换到它运行 ns-3。 一次性解决方案:创建非 roo…...
【C语言练习】080. 使用C语言实现简单的数据库操作
080. 使用C语言实现简单的数据库操作 080. 使用C语言实现简单的数据库操作使用原生APIODBC接口第三方库ORM框架文件模拟1. 安装SQLite2. 示例代码:使用SQLite创建数据库、表和插入数据3. 编译和运行4. 示例运行输出:5. 注意事项6. 总结080. 使用C语言实现简单的数据库操作 在…...

《基于Apache Flink的流处理》笔记
思维导图 1-3 章 4-7章 8-11 章 参考资料 源码: https://github.com/streaming-with-flink 博客 https://flink.apache.org/bloghttps://www.ververica.com/blog 聚会及会议 https://flink-forward.orghttps://www.meetup.com/topics/apache-flink https://n…...

Python Ovito统计金刚石结构数量
大家好,我是小马老师。 本文介绍python ovito方法统计金刚石结构的方法。 Ovito Identify diamond structure命令可以识别和统计金刚石结构,但是无法直接输出结构的变化情况。 本文使用python调用ovito包的方法,可以持续统计各步的金刚石结构,具体代码如下: from ovito…...
MySQL 8.0 事务全面讲解
以下是一个结合两次回答的 MySQL 8.0 事务全面讲解,涵盖了事务的核心概念、操作示例、失败回滚、隔离级别、事务性 DDL 和 XA 事务等内容,并修正了查看隔离级别的命令。 MySQL 8.0 事务全面讲解 一、事务的核心概念(ACID) 事务是…...
LRU 缓存机制详解与实现(Java版) + 力扣解决
📌 LRU 缓存机制详解与实现(Java版) 一、📖 问题背景 在日常开发中,我们经常会使用 缓存(Cache) 来提升性能。但由于内存有限,缓存不可能无限增长,于是需要策略决定&am…...
Caliper 配置文件解析:fisco-bcos.json
config.yaml 文件 config.yaml 是 Caliper 的主配置文件,通常包含以下内容: test:name: fisco-bcos-test # 测试名称description: Performance test of FISCO-BCOS # 测试描述workers:type: local # 工作进程类型number: 5 # 工作进程数量monitor:type: - docker- pro…...

零知开源——STM32F103RBT6驱动 ICM20948 九轴传感器及 vofa + 上位机可视化教程
STM32F1 本教程使用零知标准板(STM32F103RBT6)通过I2C驱动ICM20948九轴传感器,实现姿态解算,并通过串口将数据实时发送至VOFA上位机进行3D可视化。代码基于开源库修改优化,适合嵌入式及物联网开发者。在基础驱动上新增…...