Vue3步骤条(Steps)

article/2025/9/17 23:46:25

Vue2步骤条(Steps)

可自定义设置以下属性:

  • 步骤数组(steps),类型:Array<{title?: string, description?: string}>,默认 []

  • 当前选中的步骤,设置 v-model 后,Steps 变为可点击状态(v-model:current),类型:number,默认 1,从1开始计数

  • 步骤条总宽度(width),类型:string | number,默认 '100%'

  • 描述文本最大宽度(descMaxWidth),类型:number,单位px,默认 120

效果如下图:在线预览

①创建步骤条组件Steps.vue:

<script setup lang="ts">
import { computed } from 'vue'
interface Step {title?: string // 标题description?: string // 描述
}
interface Props {steps: Step[] // 步骤数组current?: number // 当前选中的步骤(v-model),设置 v-model 后,Steps 变为可点击状态。从1开始计数width?: number|string // 步骤条总宽度descMaxWidth?: number // 描述文本最大宽度
}
const props = withDefaults(defineProps<Props>(), {steps: () => [],current: 1,width: '100%',descMaxWidth: 120
})
const totalWidth = computed(() => {if (typeof props.width === 'number') {return props.width + 'px'} else {return props.width}
})
const totalSteps = computed(() => { // 步骤总数return props.steps.length
})
const currentStep = computed(() => {if (props.current < 1) {return 1} else if (props.current > totalSteps.value + 1) {return totalSteps.value + 1} else {return props.current}
})
// 若当前选中步骤超过总步骤数,则默认选择步骤1
const emits = defineEmits(['update:current', 'change'])
function onChange (index: number) { // 点击切换选择步骤if (currentStep.value !== index) {emits('update:current', index)emits('change', index)}
}
</script>
<template><div class="m-steps-area" :style="`width: ${totalWidth};`"><div class="m-steps"><div :class="['m-steps-item',{'finish': currentStep > index + 1,'process': currentStep === index + 1,'wait': currentStep < index + 1}]"v-for="(step, index) in steps" :key="index"><div class="m-info-wrap" @click="onChange(index + 1)"><div class="m-steps-icon"><span class="u-num" v-if="currentStep<=index + 1">{{ index + 1 }}</span><svg class="u-icon" v-else viewBox="64 64 896 896" data-icon="check" aria-hidden="true" focusable="false"><path d="M912 190h-69.9c-9.8 0-19.1 4.5-25.1 12.2L404.7 724.5 207 474a32 32 0 0 0-25.1-12.2H112c-6.7 0-10.4 7.7-6.3 12.9l273.9 347c12.8 16.2 37.4 16.2 50.3 0l488.4-618.9c4.1-5.1.4-12.8-6.3-12.8z"></path></svg></div><div class="m-steps-content"><div class="u-steps-title">{{ step.title }}</div><div class="u-steps-description" v-show="step.description" :style="`max-width: ${descMaxWidth}px;`">{{ step.description }}</div></div></div></div></div></div>
</template>
<style lang="less" scoped>
.m-steps-area {margin: 0px auto;.m-steps {display: flex;.m-steps-item {display: inline-block;overflow: hidden;font-size: 16px;&:not(:last-child) {margin-right: 16px;flex: 1; // 弹性盒模型对象的子元素都有相同的长度,且忽略它们内部的内容.u-steps-title {&:after {background: #e8e8e8;position: absolute;top: 16px;left: 100%;display: block;width: 3000px;height: 1px;content: "";cursor: auto;transition: all .3s;}}}.m-info-wrap {display: inline-block;.m-steps-icon {display: inline-flex;align-items: center;justify-content: center;margin-right: 8px;width: 32px;height: 32px;border-radius: 50%;text-align: center;background: #fff;border: 1px solid rgba(0,0,0,.25);transition: all .3s;.u-num {display: inline-block;font-size: 16px;line-height: 1;color: rgba(0, 0, 0, .25);transition: all .3s;}.u-icon {display: inline-block;fill: @themeColor;width: 16px;height: 16px;}}.m-steps-content {display: inline-block;vertical-align: top;.u-steps-title {position: relative;display: inline-block;padding-right: 16px;color: rgba(0,0,0,.45);line-height: 32px;transition: all .3s;}.u-steps-description {font-size: 14px;color: rgba(0,0,0,.45);line-height: 22px;word-wrap: break-word;transition: all .3s;}}}}.finish {.m-info-wrap {cursor: pointer;.m-steps-icon {background: #fff;border: 1px solid rgba(0,0,0,.25);border-color: @themeColor;}.m-steps-content {.u-steps-title {color: rgba(0,0,0,.85);&:after {background: @themeColor;}}.u-steps-description {color: rgba(0,0,0,.45);}}&:hover {.m-steps-content {.u-steps-title {color: @themeColor;}.u-steps-description {color: @themeColor;}}}}}.process {.m-info-wrap {.m-steps-icon {background: @themeColor;border: 1px solid rgba(0,0,0,.25);border-color: @themeColor;.u-num {color: #fff;}}.m-steps-content {.u-steps-title {font-weight: 500;color: rgba(0,0,0,.85);}.u-steps-description {color: rgba(0,0,0,.85);}}}}.wait {.m-info-wrap {cursor: pointer;&:hover {.m-steps-icon {border-color: @themeColor;.u-num {color: @themeColor;}}.m-steps-content {.u-steps-title {color: @themeColor;}.u-steps-description {color: @themeColor;}}}}}}
}
</style>

②在要使用的页面引入:

<script setup lang="ts">
import Steps from './Steps.vue'
import { ref, watch } from 'vue'
const steps = ref([{title: 'Step 1',description: 'description 1'},{title: 'Step 2',description: 'description 2'},{title: 'Step 3',description: 'description 3'},{title: 'Step 4',description: 'description 4'},{title: 'Step 5',description: 'description 5'}
])
const current = ref(3)
watch(current, (to) => {console.log('p to:', to)
})function onChange (index: number) { // 父组件获取切换后的选中步骤console.log('current:', index)
}
function onPrevious () {if (current.value > 1) {current.value--}
}
function onNext () {if (steps.value.length >= current.value) {current.value++}
}
</script>
<template><div><h2 class="mb10">Steps 步骤条基本使用</h2><Steps:steps="steps":width="'100%'":descMaxWidth="160":current="current"@change="onChange" /><h2 class="mt30 mb10">步骤条设置 v-model: current 后可点击</h2><Steps:steps="steps":width="'100%'":descMaxWidth="160"v-model:current="current"@change="onChange" /><Button @click="onPrevious()" size="large" class="mt30 mr30">Previous</Button><Button @click="onNext()" size="large">Next</Button></div>
</template>
<style lang="less" scoped>
</style>

http://chatgpt.dhexx.cn/article/lVwbxk7r.shtml

相关文章

CSS3动画——steps函数

CSS3动画中应用的定时函数&#xff08;timing function&#xff09;常用的有内置关键字linear, ease, ease-in, ease-out, ease-in-out, step-start, step-end&#xff0c;另外还有两个内置的缓动函数cubic-bezier(), steps() 其中steps()这个函数是将整个动画过程分为指定的步…

css3中animation的steps分步动画

css的animation中&#xff0c;有一种描述动画变化速率的东西&#xff0c;例如常见的linear,ease-in,ease-out等&#xff0c;这些都是连续的变化&#xff0c;还有一种叫做steps的&#xff0c;它用来描述一种不连续的动画&#xff0c;也就是逐帧动画。 基本认识 steps的格式为&…

超几何分布(Hypergeometric distribution)

超几何分布 百度解释 https://baike.baidu.com/item/超几何分布/4782968?fraladdin 通俗解释 超几何分布中的参数是M,N,n&#xff0c;超几何分布记作X~H(N,M,n) 。假如一共有100颗球&#xff0c;20颗为红球&#xff0c;80颗为白球&#xff0c;从中不放回地拿出10个球&#x…

机器学习小组知识点12:超几何分布(Hyper-Geometric Distribution)

超几何分布与二项分布的联系与区别 事实上,超几何分布和二项分布确实有着密切的联系&#xff0c;但也有明显的区别。   课本对于超几何分布的定义是这样的&#xff1a;一般的&#xff0c;若一个随机变量X的分布列为&#xff0c;其中&#xff0c;则称X服从超几何分布&#xf…

常见分布总结-高斯分布、伯努利分布、泊松分布、几何分布、beta分布

概率分布 概率分布是指用于表述随机变量取值的概率规律,包括连续分布和离散分布。 下面作了这些概率分布的一个思维导图。 文章目录 概率分布1、离散概率分布1.1、两点分布2.2、 二项分布1.3、几何分布1.4、超几何分布1.5、泊松分布2、连续概率分布2.1、均匀分布2.2、正太分布…

超几何分布的其他形式及其解释?

超几何分布定义 百度百科&#xff1a;超几何分布是统计学上一种离散概率分布。它描述了从有限N个物件&#xff08;其中包含M个指定种类的物件&#xff09;中抽出n个物件&#xff0c;成功抽出该指定种类的物件的次数&#xff08;不放回&#xff09;。称为超几何分布&#xff0c;…

geometric distribution and exponential distribution(几何分布和指数分布)

几何分布 分布函数均值和方差意义 表示经过k次实验才第一次得到正确的实验结果 比如抛硬币得到正面的需要抛的次数 指数分布 分布函数均值和方差意义 表示经过一段x之后&#xff0c;某件事第一次发生 比如经过x时间之后&#xff0c;公交车来的概率 比如餐厅从开业到第一个客人…

概率论的学习和整理9:超几何分布 (未完成!!!)

目录 1超几何分布 Hypergeometric distribution 1.1 超几何分布的定义 1.2 为什么叫超几何分布 1.3 超几何分布的公式 &#xff08;2种公式&#xff09; 1.3.1 超几何分布的公式1 &#xff08;总体型公式&#xff09; 1.3.2 超几何分布的公式2 &#xff08;拆…

概率论与数理统计基础(二):常用离散分布 二项、泊松、超几何分、几何、 负二项分布

本文列举了常见的离散分布&#xff0c;关于它们的背景、概率分布列、数学期望与方差&#xff0c;以及与之相关的一些重要性质&#xff1b;比如几何分布的无记忆性、 二项分布的泊松近似、超几何分布的二项近似。。。。可作为离散分布的知识速查表。 目录 1. 二项分布b(n,p) …

如何理解几何分布与指数分布的无记忆性?

在经济学上&#xff0c;有一个概念是沉没成本&#xff0c;大概指的是已经付出的、且不可收回的成本。针对这个概念有一个常见的说法&#xff1a; 这句话的意思是&#xff0c;既然沉没成本不可收回&#xff0c;那么在做选择的时候就不应该考虑它。举一个简单的例子&#xff0c;买…

几何分布的期望公式的推导

随机变量服从几何分布 概率分布 期望 现在先求等差比数列和 ②-③, 并运用等比数列求和公式,可得 将④代入①得

统计学:几何分布、二项分布、泊松分布

一、几何分布 假设某种赌博游戏的胜率为0.2&#xff0c;那么意味着你玩第一次就胜出的概率为0.2。 那玩第二次才胜出呢&#xff1f;“玩第二次才胜出”就意味着玩第一次是失败的&#xff0c;而直到第二次才胜出&#xff0c;那么这件事发生的概率就是0.80.20.16。 那么第三次…

统计学 分布篇 - Hypergeometric Distribution(超几何分布)

超几何分布: 是 离散随机分布的一种. 它描述的是 从 n 中 拿 k 个成功的事件的概率( 不放回, 不放回意味着该事件是非独立事件), 其中在 N 中一共有 K 个成功事件. n 为 样本数量, k 为样本中成功的概率 N为 事件的总数量(population), K为 在N中 事件的总数量. note: 超几…

几何分布的期望与方差

几何分布的期望与方差 高中数学教科书新版第三册&#xff08;选修II&#xff09;比原来的修订本新增加随机变量的几何分布&#xff0c;但书中只给出了结论&#xff1a;&#xff08;1&#xff09;&#xff0c;&#xff08;2&#xff09;&#xff0c;而未加以证明。本文给出证明&…

概率统计14——几何分布

我家小朋友年方1岁半&#xff0c;家里每天上午都要出去遛小孩。现在小朋友有两项爱好&#xff0c;在家翻垃圾桶&#xff0c;出门捡烟头。 翻垃圾桶可以有效地限制&#xff0c;捡烟头可是防不胜防。 也许烟头能散发出特殊的能量波动&#xff0c;小区的绿化带和草坪上的大部分烟…

超几何分布定义

设有N件产品&#xff0c;其中有M&#xff08;M≤N&#xff09;件是不合格品&#xff0e;若从中不放回地抽取n&#xff08;n≤N&#xff09;件&#xff0c;设其中含有的不合格品的件数为X&#xff0c;则X的分布律为 称X服从参数为N、M和n的超几何分布&#xff0c;记为X~H&#x…

几何分布和二项分布有什么区别?

● 每周一言 越长大越渺小。 导语 各种常见的分布中&#xff0c;二项分布和几何分布经常同时出现&#xff0c;在前面讲泊松分布的时候也简单提到了二项分布。那么&#xff0c;几何分布是什么分布&#xff1f;和二项分布有什么区别&#xff1f; 几何分布 讲泊松分布的时候提…

几何分布定义

在伯努利试验中&#xff0c;记每次试验中A事件发生的概率P&#xff08;A&#xff09;p&#xff08;0<p<1&#xff09;&#xff0c;设随机变量X表示A事件首次出现时已经试验的次数&#xff0c;则X的取值为1&#xff0c;2&#xff0c;…&#xff0c;n&#xff0c;…&#x…

几何分布GeometricDistribution

几何分布 几何分布用于描述这种分布&#xff1a;独立事件的结果只有2个&#xff1a;”1和0“ 或”成功和失败“等&#xff0c;成功的概率为 p p p, 失败的概率为 q 1 − p q1-p q1−p; 第r次成功的概率为 P ( X r ) p ⋅ q r − 1 P(Xr)p\cdot q^{r-1} P(Xr)p⋅qr−1 即用…