当前位置:首页 > VUE

vue实现步骤条组件

2026-02-22 04:20:56VUE

Vue 步骤条组件实现

基础实现思路

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

vue实现步骤条组件

<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>

可点击交互版本

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

vue实现步骤条组件

<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);
}

动态内容插槽

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

<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 下拉刷新组件实现 核心思路 通过监听 touch 事件计算滑动距离,结合 CSS 过渡动画实现下拉效果,触发刷新回调。 基础实现步骤 监听 touch 事件 在组件 mounted 阶段绑定…

vue实现下拉框组件

vue实现下拉框组件

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

vue组件实现

vue组件实现

Vue 组件实现 Vue 组件是 Vue.js 的核心特性之一,允许开发者将 UI 拆分为独立、可复用的模块。以下是实现 Vue 组件的几种常见方式: 单文件组件(SFC) 单文件组件是 Vue 中…

如何设计react组件

如何设计react组件

设计 React 组件的核心原则 React 组件的设计需要遵循高内聚、低耦合的原则,确保组件功能独立且易于维护。组件的设计可以分为展示组件和容器组件两类,展示组件负责 UI 渲染,容器组件负责逻辑处…

vue实现递归组件

vue实现递归组件

递归组件的实现方法 在Vue中实现递归组件主要依靠组件调用自身的能力。以下是几种常见的实现方式: 使用组件name属性 通过组件的name属性实现递归调用是最简单的方式: <template…

vue实现search组件

vue实现search组件

Vue 实现搜索组件 基础实现 创建一个基础的搜索组件需要包含输入框和搜索逻辑。以下是一个简单的实现示例: <template> <div class="search-cont…