当前位置:首页 > VUE

vue实现步骤条效果

2026-02-22 09:07:48VUE

使用Vue实现步骤条效果

基础步骤条实现

通过v-for动态渲染步骤项,结合CSS样式控制进度条和步骤状态:

<template>
  <div class="steps-container">
    <div class="steps">
      <div 
        v-for="(step, index) in steps" 
        :key="index"
        :class="['step', { 'active': currentStep >= index }]"
      >
        <div class="step-circle">{{ index + 1 }}</div>
        <div class="step-label">{{ step.label }}</div>
      </div>
      <div class="progress-bar" :style="progressStyle"></div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentStep: 1,
      steps: [
        { label: '第一步' },
        { label: '第二步' },
        { label: '第三步' }
      ]
    }
  },
  computed: {
    progressStyle() {
      return {
        width: `${(this.currentStep / (this.steps.length - 1)) * 100}%`
      }
    }
  }
}
</script>

<style scoped>
.steps-container {
  width: 100%;
  padding: 20px;
}

.steps {
  display: flex;
  justify-content: space-between;
  position: relative;
}

.step {
  display: flex;
  flex-direction: column;
  align-items: center;
  z-index: 1;
}

.step-circle {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: #ccc;
  display: flex;
  justify-content: center;
  align-items: center;
  color: white;
}

.step.active .step-circle {
  background: #4CAF50;
}

.step-label {
  margin-top: 8px;
}

.progress-bar {
  position: absolute;
  height: 4px;
  background: #4CAF50;
  top: 15px;
  left: 0;
  transition: width 0.3s;
}
</style>

垂直步骤条实现

修改CSS布局实现垂直方向展示:

<template>
  <div class="vertical-steps">
    <div 
      v-for="(step, index) in steps" 
      :key="index"
      :class="['step', { 'active': currentStep >= index }]"
    >
      <div class="step-circle">{{ index + 1 }}</div>
      <div class="step-content">
        <div class="step-title">{{ step.title }}</div>
        <div class="step-desc">{{ step.desc }}</div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.vertical-steps {
  display: flex;
  flex-direction: column;
  padding: 20px;
}

.step {
  display: flex;
  position: relative;
  padding-bottom: 20px;
}

.step-circle {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #e0e0e0;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-right: 16px;
  flex-shrink: 0;
}

.step.active .step-circle {
  background: #2196F3;
}

.step-content {
  padding-bottom: 16px;
}

.step:not(:last-child)::after {
  content: '';
  position: absolute;
  left: 12px;
  top: 24px;
  height: calc(100% - 24px);
  width: 2px;
  background: #e0e0e0;
}

.step.active:not(:last-child)::after {
  background: #2196F3;
}
</style>

使用第三方库

推荐使用vue-step-wizard等现成组件库快速实现:

npm install vue-step-wizard
<template>
  <div>
    <step-wizard :active="currentStep">
      <step title="个人信息">
        <!-- 第一步内容 -->
      </step>
      <step title="支付信息">
        <!-- 第二步内容 -->
      </step>
      <step title="确认订单">
        <!-- 第三步内容 -->
      </step>
    </step-wizard>
    <button @click="currentStep--" :disabled="currentStep <= 0">上一步</button>
    <button @click="currentStep++" :disabled="currentStep >= 2">下一步</button>
  </div>
</template>

<script>
import { StepWizard, Step } from 'vue-step-wizard'

export default {
  components: { StepWizard, Step },
  data() {
    return {
      currentStep: 0
    }
  }
}
</script>

动态步骤控制

实现步骤的导航控制和验证逻辑:

methods: {
  nextStep() {
    if (this.validateCurrentStep()) {
      this.currentStep++
    }
  },
  prevStep() {
    this.currentStep--
  },
  validateCurrentStep() {
    switch(this.currentStep) {
      case 0: return this.validatePersonalInfo()
      case 1: return this.validatePayment()
      default: return true
    }
  }
}

响应式设计

添加媒体查询确保在小屏幕上正常显示:

@media (max-width: 768px) {
  .steps {
    flex-direction: column;
    align-items: flex-start;
  }
  .progress-bar {
    width: 4px !important;
    height: 100%;
    left: 15px;
    top: 0;
  }
  .step {
    margin-bottom: 20px;
    flex-direction: row;
  }
}

vue实现步骤条效果

标签: 步骤效果
分享给朋友:

相关文章

vue实现效果

vue实现效果

以下是Vue实现常见效果的几种典型方法,分为不同场景说明: 数据绑定与响应式更新 使用v-model实现表单双向绑定: <template> <input v-model…

h5实现左右滚动效果

h5实现左右滚动效果

使用CSS实现横向滚动 在HTML5中实现左右滚动效果,可以通过CSS的overflow-x属性配合white-space或flex布局实现。以下是一个基础示例: <div class="sc…

vue实现基金效果

vue实现基金效果

Vue 实现基金效果 在 Vue 中实现基金效果,通常指的是模拟基金的增长、波动或可视化展示。以下是几种常见的实现方式: 数据绑定与动态更新 通过 Vue 的数据绑定特性,可以动态展示基金净值的变…

vue实现点选效果

vue实现点选效果

实现点选效果的基本思路 在Vue中实现点选效果通常涉及监听用户的点击事件,并通过动态绑定类名或样式来改变元素的外观。核心是利用Vue的响应式数据和事件绑定机制。 使用v-bind和v-on实现 通过…

vue实现折叠效果

vue实现折叠效果

使用 v-show 或 v-if 实现基础折叠 通过 Vue 的指令 v-show 或 v-if 控制元素的显示与隐藏。v-show 通过 CSS 的 display 属性切换,v-if 直接操作 D…

vue实现注册步骤

vue实现注册步骤

Vue实现用户注册功能 注册功能需要结合前端表单和后端接口实现,以下为Vue.js的实现方案: 前端表单结构 <template> <form @submit.prevent…