更新记录
1.0.5(2025-12-16) 下载此版本
支持配置默认选中索引
1.0.4(2025-12-10) 下载此版本
增加多选类型
1.0.3(2025-12-09) 下载此版本
增加单选类型
查看更多平台兼容性
uni-app(4.66)
| Vue2 | Vue3 | Chrome | Safari | app-vue | app-nvue | Android | iOS | 鸿蒙 |
|---|---|---|---|---|---|---|---|---|
| - | - | - | - | - | - | √ | √ | - |
| 微信小程序 | 支付宝小程序 | 抖音小程序 | 百度小程序 | 快手小程序 | 京东小程序 | 鸿蒙元服务 | QQ小程序 | 飞书小程序 | 快应用-华为 | 快应用-联盟 |
|---|---|---|---|---|---|---|---|---|---|---|
| √ | √ | √ | √ | √ | √ | - | √ | √ | √ | √ |
liaction-tabs - UniApp 标签页组件
📱 组件简介
liaction-tabs 是一款专为 UniApp 开发的高度可定制化标签页组件,提供了丰富的样式配置和交互效果,支持滚动和非滚动两种模式,以及完善的徽章系统。适用于各类需要标签切换功能的移动端页面。
✨ 特性亮点
- 三种组件类型:支持标签页(tabs)、单选按钮(radio)和多选按钮(checkbox)三种模式
- 双模式布局:支持滚动模式和非滚动模式,适应不同场景需求
- 高度可定制:提供丰富的样式参数,支持自定义颜色、尺寸、间距等
- 完善的徽章系统:支持圆点、数字、文本多种徽章类型,并可自定义位置
- 灵活的指示器:支持内容宽度和标签项宽度两种指示器样式
- 平滑动画效果:标签切换时提供平滑的滚动动画
- 响应式设计:适配不同尺寸的移动设备
- 自定义插槽:支持自定义标签内容展示
- 灵活的默认配置:支持配置默认选中索引,-1表示不选中任何项
🎛️ Props 参数
基础配置
| 参数名 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| type | String | 'tabs' | 组件类型,可选值:'tabs'(标签页)、'radio'(单选按钮)、'checkbox'(多选按钮) |
| tabs | Array | [] | 标签数据数组,必填项 |
| activeIndex | [Number, String, Array] | 0 | 激活标签的索引或唯一标识,支持数字索引、字符串标识或数组(多选模式) |
| defaultIndex | Number | -1 | 默认索引配置,-1表示不选中任何项,仅在单选模式下有效 |
| showBadge | Boolean | false | 是否显示徽章 |
| customClass | String | '' | 自定义类名 |
| customStyle | Object | {} | 自定义样式 |
标签样式
| 参数名 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| tabActiveColor | String | '#007bff' | 激活标签的颜色 |
| tabDefaultColor | String | '#333' | 默认标签颜色 |
| tabMaxWidth | String | '120px' | 标签最大宽度 |
| tabPadding | String | '8px' | 标签内边距 |
| tabFontSize | String | '16px' | 标签字体大小 |
| tabHeight | String | '44px' | 标签高度 |
| tabFlex | Boolean | false | 是否弹性布局标签 |
| tabWrap | Boolean | false | 标签是否换行显示 |
| tabTextAlign | String | 'center' | 标签文本对齐方式 |
| tabStyle | Object | {} | 自定义标签样式 |
| tabBorderRadius | String | '4px' | 标签圆角,radio模式下有效 |
| tabMargin | String | '0 8px' | 标签外边距,radio模式下有效 |
指示器配置
| 参数名 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| indicatorHeight | String | '3px' | 指示器高度 |
| indicatorMarginTop | String | '6px' | 指示器上边距 |
| indicatorShow | Boolean | true | 是否显示指示器 |
| indicatorWidth | String | 'item' | 指示器宽度模式,可选 'item'、'content' |
滚动配置
| 参数名 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| scrollEnabled | Boolean | true | 是否启用滚动 |
| scrollAnimation | Boolean | true | 是否启用滚动动画 |
非滚动模式布局
| 参数名 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| layoutJustify | String | 'center' | 非滚动模式下的对齐方式 |
| layoutAlign | String | 'center' | 非滚动模式下的垂直对齐方式 |
徽章配置
| 参数名 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| badgeDefaultType | String | 'circle' | 默认徽章类型,可选 'circle'、'dot' |
| badgeDefaultColor | String | '#ff3b30' | 默认徽章颜色 |
| badgeDefaultTextColor | String | '#ffffff' | 默认徽章文本颜色 |
| badgeDefaultPosition | String | 'top-right' | 默认徽章位置,可选 'top-right'、'after' |
| badgeBaseSize | String | '14px' | 徽章基础尺寸 |
| badgeDotRatio | Number | 0.5 | 圆点徽章比例 |
| badgeLongTextRatio | Number | 1.2 | 长文本徽章比例 |
| badgeFontSizeRatio | Number | 0.8 | 徽章字体缩放比例 |
| badgeMargin | String | '4px' | 徽章外边距 |
| badgeMaxLength | Number | 2 | 徽章最大字符长度 |
| badgeOverflowChar | String | '+' | 徽章溢出字符 |
| badgePadding | String | '1px' | 徽章内边距 |
📢 事件
| 事件名 | 说明 | 参数 |
|---|---|---|
| change | 标签切换时触发 | 对象,包含: • item: 点击的标签数据 • index: 当前选中标签的索引 • key: 仅当activeIndex为字符串类型时返回,当前选中标签的唯一标识 • selectedIndexes: 仅当type为checkbox时返回,所有选中标签的索引数组 • selectedKeys: 仅当type为checkbox且activeIndex为字符串类型时返回,所有选中标签的唯一标识数组 |
| click | 点击标签时触发 | item: 标签数据, index: 标签索引 |
📚 使用示例
基础用法
<liaction-tabs
:tabs="tabs"
/>
<script>
export default {
data() {
return {
tabs: [
{ name: '首页' },
{ name: '发现' },
{ name: '消息' },
{ name: '我的' }
],
};
},
methods: {
}
};
</script>
带badge的标签
<liaction-tabs
:tabs="tabs"
:showBadge="true"
/>
<script>
export default {
data() {
return {
tabs: [
{ name: '首页' },
{ name: '消息', badge: 9, badgeType: 'circle' },
{ name: '通知', badge: '99+', badgeType: 'circle' },
{ name: '活动', badge: true, badgeType: 'dot' }
]
};
}
};
</script>
自定义样式
<liaction-tabs
:tabs="tabs"
tabActiveColor="#ff6700"
tabDefaultColor="#999"
indicatorHeight="4px"
indicatorMarginTop="8px"
:tabStyle="{
fontSize: '15px',
fontWeight: '500'
}"
/>
非滚动模式
<liaction-tabs
:tabs="tabs"
:scrollEnabled="false"
:tabFlex="true"
tabTextAlign="center"
/>
自定义标签内容
<liaction-tabs :tabs="tabs">
<template #tab="{ item, index }">
<view class="custom-tab-item">
<image :src="item.icon" mode="aspectFit" class="tab-icon"></image>
<text class="tab-text">{{ item.name }}</text>
</view>
</template>
</liaction-tabs>
<script>
export default {
data() {
return {
tabs: [
{ name: '首页', icon: '/static/icons/1.png' },
{ name: '分类', icon: '/static/icons/2.png' },
{ name: '购物车', icon: '/static/icons/3.png' },
{ name: '我的', icon: '/static/icons/4.png' }
]
};
}
};
</script>
<style scoped>
.custom-tab-item {
display: flex;
flex-direction: column;
align-items: center;
padding: 4px 0;
}
.tab-icon {
width: 22px;
height: 22px;
margin-bottom: 2px;
}
</style>
长文本徽章配置
<liaction-tabs
:tabs="tabs"
:showBadge="true"
:badgeLongTextRatio="1.5"
/>
<script>
export default {
data() {
return {
tabs: [
{ name: '标签一' },
{ name: '标签二', badge: '新活动', badgeType: 'circle' },
{ name: '标签三', badge: 999, badgeType: 'circle' }
]
};
}
};
</script>
单选按钮模式
<liaction-tabs
type="radio"
:tabs="tabs"
tabActiveColor="red"
tabDefaultColor="#f5f5f5"
tabBorderRadius="20px"
tabMargin="0 10px"
/>
<script>
export default {
data() {
return {
tabs: [
{ name: '选项一' },
{ name: '选项二' },
{ name: '选项三' }
]
};
}
};
</script>
多选按钮模式
<liaction-tabs
type="checkbox"
:tabs="tabs"
v-model:activeIndex="activeTabs"
tabActiveColor="#007bff"
tabDefaultColor="#f0f0f0"
tabBorderRadius="16px"
tabMargin="0 8px"
@change="handleChange"
/>
<script>
export default {
data() {
return {
tabs: [
{ name: '选项一', id: 'opt1' },
{ name: '选项二', id: 'opt2' },
{ name: '选项三', id: 'opt3' },
{ name: '选项四', id: 'opt4' }
],
activeTabs: [0, 2] // 初始选中第一个和第三个选项
};
},
methods: {
handleChange(e) {
console.log('选中的标签索引:', e.selectedIndexes);
console.log('选中的标签数据:', e.item);
}
}
};
</script>
默认索引配置 (defaultIndex)
1. 默认不选中任何项
<liaction-tabs
:tabs="tabs"
:defaultIndex="-1"
@change="handleChange"
/>
<script>
export default {
data() {
return {
tabs: [
{ name: '首页' },
{ name: '分类' },
{ name: '消息' },
{ name: '购物车' }
]
};
},
methods: {
handleChange(e) {
console.log('当前选中:', e.item ? e.item.name : '无');
}
}
};
</script>
2. 默认选中指定项
<liaction-tabs
:tabs="tabs"
:defaultIndex="2" // 默认选中第三个标签(索引从0开始)
@change="handleChange"
/>
3. 动态配置defaultIndex
<template>
<view>
<slider
:value="dynamicDefaultIndex"
min="-1"
max="3"
step="1"
show-value
@change="updateDefaultIndex"
/>
<liaction-tabs
:tabs="tabs"
:defaultIndex="dynamicDefaultIndex"
v-model:activeIndex="activeIndex"
@change="handleChange"
/>
<view class="result">
当前选中: {{ activeIndex === -1 ? '无' : tabs[activeIndex]?.name }}
</view>
</view>
</template>
<script>
export default {
data() {
return {
tabs: [
{ name: '首页' },
{ name: '分类' },
{ name: '消息' },
{ name: '购物车' }
],
dynamicDefaultIndex: 0,
activeIndex: 0
};
},
methods: {
updateDefaultIndex(e) {
this.dynamicDefaultIndex = e.detail.value;
// 重置activeIndex触发defaultIndex生效
this.activeIndex = -2;
this.$nextTick(() => {
this.activeIndex = this.dynamicDefaultIndex;
});
},
handleChange(e) {
console.log('当前选中:', e.item ? e.item.name : '无');
}
}
};
</script>
4. 无效defaultIndex自动回退
<liaction-tabs
:tabs="tabs"
:defaultIndex="10" // 无效索引,自动回退到不选中状态
@change="handleChange"
/>
📝 License
MIT
🤝 贡献指南
欢迎提交 Issues 来改进这个组件。

收藏人数:
下载插件并导入HBuilderX
赞赏(0)
下载 66
赞赏 0
下载 12607050
赞赏 1830
赞赏
京公网安备:11010802035340号