Skip to content

Tabs 选项卡

一个普通的标签选项卡组件,支持激活项自动滚动到中间位置。

基础使用

  • 通过 list 属性设置选项卡数据。
  • 通过 keyName 属性设置数据中显示文本的字段名。
  • 通过 current 属性设置激活项下标。
vue
<template>
    <hi-tabs :list="tabs" keyName="name" :current="current" @change="onChange"></hi-tabs>
</template>

<script setup>
    import { ref } from 'vue';

    // 选项卡数据
    // 所有属性都为可选,为空则不显示或不设置
    /// icon: 图标。
    /// name: 显示文本
    /// disabled: 是否禁用。
    /// styles: 选项样式
    /// activeStyles: 选项激活态样式
    /// hover: 选项点击态类名
    const tabs = ref([
        { icon: "", name: "热门", disabled: false, styles: "", activeStyles: "", hover: "hi-hover" },
        { icon: "", name: "数码", disabled: false, styles: "", activeStyles: "", hover: "hi-hover" },
        { icon: "", name: "美妆", disabled: false, styles: "", activeStyles: "", hover: "hi-hover" },
        { icon: "", name: "礼品卡", disabled: false, styles: "", activeStyles: "", hover: "hi-hover" },
        { icon: "", name: "百货", disabled: false, styles: "", activeStyles: "", hover: "hi-hover" },
        { icon: "", name: "家电", disabled: false, styles: "", activeStyles: "", hover: "hi-hover" },
        { icon: "", name: "手表", disabled: false, styles: "", activeStyles: "", hover: "hi-hover" },
        { icon: "", name: "配饰", disabled: false, styles: "", activeStyles: "", hover: "hi-hover" },
        { icon: "", name: "潮玩", disabled: false, styles: "", activeStyles: "", hover: "hi-hover" },
        { icon: "", name: "食品", disabled: false, styles: "", activeStyles: "", hover: "hi-hover" },
    ]);

    // 激活项下标
    const current = ref(0);

    // 切换事件
    function onChange(index) {
        current.value = index;
    }
</script>

右侧固定菜单

此组件可以通过 right 属性设置右侧固定菜单数据,数据结构同 list

vue
<template>
    <hi-tabs :list="tabs" keyName="name" :current="current" @change="onChange" :right="menus" @rightClick="onRightClick"></hi-tabs>
</template>

<script setup>
    import { ref } from 'vue';

    // 选项卡数据
    /// icon: 图标,为空则不展示。
    /// name: 显示文本
    /// disabled: 是否禁用
    const tabs = ref([
        { icon: "", name: "热门", disabled: false },
        { icon: "", name: "数码", disabled: false },
        { icon: "", name: "美妆", disabled: false },
        { icon: "", name: "礼品卡", disabled: false },
        { icon: "", name: "百货", disabled: false },
        { icon: "", name: "家电", disabled: false },
        { icon: "", name: "手表", disabled: false },
        { icon: "", name: "配饰", disabled: false },
        { icon: "", name: "潮玩", disabled: false },
        { icon: "", name: "食品", disabled: false },
    ]);

    // 激活项下标
    const current = ref(0);

    // 右侧固定菜单
    const menus = ref([
        { icon: "filter", name: "筛选" },
    ])

    // 切换事件
    function onChange(index) {
        current.value = index;
    }

    // 右侧菜单点击事件
    function onRightClick(item, index) {
        console.log(item, index);
    }
</script>

自动滚动

通过 autoScroll 属性设置是否自动滚动。默认开启。

vue
<hi-tabs :list="tabs" :current="current" :autoScroll="false"></hi-tabs>

等分布局

通过 equal 属性设置是否等分选项卡进行布局,此模式只适用于横向布局,并且开启等分后会禁止滚动。
等分布局通常来说就是选项的宽度相同,选项的内容居中展示。

vue
<hi-tabs :list="tabs" :current="current" equal></hi-tabs>

两端对齐

通过 justify 属性设置是否两端对齐选项卡进行布局,此模式只适用于横向布局,并且开启两端对齐后会禁止滚动。
两端对齐布局通常来说就是选项靠两侧进行对齐,选项之间的间隔相同。

vue
<hi-tabs :list="tabs" :current="current" justify></hi-tabs>

纵向布局

此组件支持纵向布局,通过 column 属性设置为纵向布局。

vue
<hi-tabs :list="tabs" :current="current" column></hi-tabs>

Props

属性名说明类型默认值可选值版本
hover指定选项按下去的样式类Stringhi-hover--
list选项卡数据Array<Object>[]--
current激活项下标Number0--
keyName显示文字的字段名Stringname--
right右侧固定菜单数据Array<Object>[]--
equal是否等分布局Booleanfalsetrue-
justify是否两端对齐布局Booleanfalsetrue-
scrollWithAnimation是否开启滚动动画Booleanfalsetrue-
bg组件背景String---
radius组件圆角String---
width组件宽度String---
height组件高度String---
color组件文字颜色String---
activeColor激活项文字颜色String---
fontSize组件文字大小String---
activeFontSize激活项文字大小String---
fontWeight组件文字粗细String---
activeFontWeight激活项文字粗细String---
gap选项间距String---
itemPadding选项内边距String---
itemBg选项背景String---
activeItemBg激活项背景String---
barWidth指示器的宽度String---
barHeight指示器的高度String---
barColor指示器的颜色String---
barRadius指示器的圆角大小String---
barLeft指示器的 leftString---
barBottom指示器的 bottomString---
itemStyle选项样式[String, Object, Array]---
activeItemStyle激活项样式[String, Object, Array]---
iconColor图标颜色String---
activeIconColor激活项图标颜色String---
iconSize图标大小String---
activeIconSize激活项图标大小String---

Event

事件名说明回调参数版本
itemClick选项点击事件item: 选项数据。index: 选项索引。-
rightClick右键固定菜单点击事件item: 菜单项数据。index: 菜单项索引。-
change选项切换事件item: 选项数据。index: 选项索引。-

Slots

名称说明参数版本
item选项插槽-item: 选项数据。index: 选项索引。
rightItem右侧选项插槽-item: 选项数据。index: 选项索引。

样式变量

组件提供了以下样式变量,可以在使用时设置这些变量来修改组件的样式。

变量名默认值版本
--hi-tabs-backgroundnone-
--hi-tabs-border-radius0-
--hi-tabs-displayflex-
--hi-tabs-flex-directionrow / column-
--hi-tabs-align-itemsstretch-
--hi-tabs-gap0-
--hi-tabs-width100% / 8em-
--hi-tabs-height3em / 100%-
--hi-tabs-text-aligncenter-
--hi-tabs-colorinherit-
--hi-tabs-active-colorvar(--hi-theme-primary)-
--hi-tabs-font-sizeinherit-
--hi-tabs-active-font-size--
--hi-tabs-font-weightinherit-
--hi-tabs-active-font-weight--
--hi-tabs-left-flex1-
--hi-tabs-left-overflowhidden-
--hi-tabs-left-positionrelative-
--hi-tabs-items-displayflex-
--hi-tabs-items-gap15px-
--hi-tabs-items-flex-directionrow / column-
--hi-tabs-items-justify-contentflex-start / space-between-
--hi-tabs-item-gap2px-
--hi-tabs-item-positionrelative-
--hi-tabs-item-displayflex-
--hi-tabs-item-flex-directionrow-
--hi-tabs-item-align-itemscenter-
--hi-tabs-item-justify-contentcenter-
--hi-tabs-item-transition100ms-
--hi-tabs-item-flex-shrink0-
--hi-tabs-item-backgroundnone-
--hi-tabs-item-active-backgroundcurrentColor / var(--hi-tabs-item-background, none)-
--hi-tabs-item-padding0 5px / 10px-
--hi-tabs-item-flexnone / 1-
--hi-tabs-item-widthauto-
--hi-tabs-item-icon-colorinherit-
--hi-tabs-item-active-icon-color--
--hi-tabs-item-icon-font-size1.25em-
--hi-tabs-item-active-icon-font-size--
--hi-tabs-item-background-opacity0.2-
--hi-tabs-bar-width1em / 3px-
--hi-tabs-bar-height3px / 1em-
--hi-tabs-bar-backgroundcurrentColor-
--hi-tabs-bar-opacity0 / 1-
--hi-tabs-bar-border-radius10px-
--hi-tabs-bar-left50% / 1px-
--hi-tabs-bar-bottom1px / 50%-
--hi-tabs-bar-transformtranslateX(-50%) scale(0) / translateX(-50%) scale(1) / translateY(50%) scale(1)-
--hi-tabs-right-flex-shrink0-
--hi-tabs-right-displayflex-
--hi-tabs-right-align-itemscenter-
--hi-tabs-right-font-sizeinherit-
--hi-tabs-right-font-weightinherit-
--hi-tabs-right-colorinherit-
--hi-tabs-right-gap10px-

基于 MIT 许可发布