当前位置:首页 > VUE

vue实现步骤条组件

2026-02-22 04:20:56VUE

Vue 步骤条组件实现

基础实现思路

通过动态渲染步骤项和状态控制实现步骤条。核心是利用v-for循环和条件判断样式。

<template>
  <div class="steps">
    <div 
      v-for="(step, index) in steps" 
      :key="index" 
      class="step-item"
      :class="{ 'active': currentStep >= index, 'completed': currentStep > index }"
    >
      <div class="step-index">{{ index + 1 }}</div>
      <div class="step-title">{{ step.title }}</div>
      <div class="step-connector" v-if="index < steps.length - 1"></div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    steps: {
      type: Array,
      required: true
    },
    currentStep: {
      type: Number,
      default: 0
    }
  }
}
</script>

<style scoped>
.steps {
  display: flex;
  justify-content: space-between;
}

.step-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  position: relative;
}

.step-index {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: #ccc;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 8px;
}

.step-item.active .step-index {
  background: #409eff;
  color: white;
}

.step-item.completed .step-index {
  background: #67c23a;
  color: white;
}

.step-connector {
  position: absolute;
  top: 15px;
  left: calc(50% + 15px);
  width: 100%;
  height: 2px;
  background: #ccc;
}

.step-item.completed + .step-item .step-connector {
  background: #67c23a;
}
</style>

可点击交互版本

增加点击事件允许用户跳转到特定步骤。

<template>
  <div class="steps">
    <div 
      v-for="(step, index) in steps" 
      :key="index" 
      class="step-item"
      :class="{ 
        'active': currentStep >= index, 
        'completed': currentStep > index,
        'clickable': clickable
      }"
      @click="handleStepClick(index)"
    >
      <!-- 同上 -->
    </div>
  </div>
</template>

<script>
export default {
  props: {
    clickable: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    handleStepClick(index) {
      if (this.clickable) {
        this.$emit('step-click', index);
      }
    }
  }
}
</script>

<style scoped>
.step-item.clickable {
  cursor: pointer;
}
</style>

垂直布局实现

通过修改CSS实现垂直步骤条。

.steps.vertical {
  flex-direction: column;
  height: 100%;
}

.steps.vertical .step-item {
  flex-direction: row;
  align-items: flex-start;
  margin-bottom: 20px;
}

.steps.vertical .step-connector {
  position: absolute;
  left: 15px;
  top: 30px;
  width: 2px;
  height: calc(100% - 30px);
}

动态内容插槽

使用插槽允许自定义步骤内容。

vue实现步骤条组件

<template>
  <div class="steps">
    <div 
      v-for="(step, index) in steps" 
      :key="index" 
      class="step-item"
      :class="{ 'active': currentStep >= index }"
    >
      <slot name="step" :step="step" :index="index">
        <!-- 默认内容 -->
        <div class="step-index">{{ index + 1 }}</div>
        <div class="step-title">{{ step.title }}</div>
      </slot>
    </div>
  </div>
</template>

使用示例

<template>
  <Steps 
    :steps="[
      { title: '第一步' },
      { title: '第二步' },
      { title: '第三步' }
    ]" 
    :currentStep="currentStep"
    @step-click="handleStepClick"
  />
</template>

<script>
import Steps from './Steps.vue';

export default {
  components: { Steps },
  data() {
    return {
      currentStep: 0
    }
  },
  methods: {
    handleStepClick(index) {
      this.currentStep = index;
    }
  }
}
</script>

关键实现要点

  • 使用v-for动态渲染步骤项
  • 通过:class绑定控制步骤状态样式
  • 使用绝对定位实现连接线
  • 通过插槽支持内容自定义
  • 提供水平和垂直两种布局方式
  • 支持点击交互功能

标签: 组件步骤
分享给朋友:

相关文章

vue实现倒计时组件

vue实现倒计时组件

Vue 倒计时组件实现 核心思路 通过 setInterval 或 setTimeout 实现时间递减,结合 Vue 的响应式数据更新 UI。需注意组件销毁时清除定时器。 基础实现方案 模板部分…

vue实现下拉框组件

vue实现下拉框组件

Vue 下拉框组件实现 基础实现 创建基础下拉框组件,使用v-model实现双向数据绑定: <template> <div class="dropdown"> &…

vue实现拖拽实现组件嵌套

vue实现拖拽实现组件嵌套

实现拖拽组件嵌套的核心思路 Vue中实现拖拽组件嵌套需要结合拖拽库(如vuedraggable)和组件递归渲染。核心在于动态管理嵌套数据结构,并通过事件传递实现层级更新。 安装必要依赖 推荐使用vu…

组件elementui

组件elementui

ElementUI 简介 ElementUI 是一套基于 Vue.js 的桌面端组件库,由饿了么前端团队开发并开源。它提供了丰富的 UI 组件(如按钮、表单、表格等),适合快速构建企业级中后台产品。…

div css制作步骤

div css制作步骤

创建HTML结构 使用<div>标签划分页面区块,每个<div>需通过id或class属性标识。例如构建基础布局框架: <div id="header">页眉内容…

vue实现父子组件

vue实现父子组件

Vue 父子组件通信实现方式 1. 父组件向子组件传递数据(Props) 父组件通过 props 向子组件传递数据,子组件通过 props 接收数据。 父组件模板: <template>…