Vue2步骤条(Steps)

简介: 这是一个基于 Vue3 的步骤条(Steps)组件,支持高度自定义。主要属性包括步骤标题数组(stepsLabel)、步骤描述数组(stepsDesc)、步骤总数(totalSteps,默认为3)、当前选中的步骤(currentStep,默认为1)、步骤条总宽度(totalWidth,默认为900px)和描述文本最大宽度(descMaxWidth,默认为140px)。组件通过不同的样式展示已完成、进行中和未开始的状态,并支持点击切换步骤。可在需要的页面中引入并传入相关初始数据。
  • 可自定义设置以下属性:

  • 步骤标题数组(stepsLabel)

  • 步骤描述数组(stepsDesc)
  • 步骤总数(totalSteps),默认3
  • 当前选中的步骤(currentStep),默认1
  • 步骤条总宽度(totalWidth),默认900px
  • 描述文本最大宽度(descMaxWidth),默认140px
  • 效果图如下:

​

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

<template>
  <div class="m-steps-area" :style="`width: ${totalWidth}px;`">
    <div class="m-steps">
      <div :class="['m-steps-item',
          {
            'finished': current > n,
            'process': current === n && n !== totalSteps,
            'last-process': current === totalSteps && n === totalSteps,
            'middle-wait': current < n && n !== totalSteps,
            'last-wait': current < n && n === totalSteps
          }
        ]"
        v-for="n in totalSteps"
        :key="n">
        <div class="m-info-wrap" @click="onChange(n)">
          <div class="m-steps-icon">
            <span class="u-icon" v-if="current<=n">{
  { n }}</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">{
  { stepsLabel[n-1] }}</div>
            <div class="u-steps-description" v-show="stepsDesc[n-1]" :style="`max-width: ${descMaxWidth}px;`">{
  { stepsDesc[n-1] }}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: 'Steps',
  props: {
    stepsLabel: { // 步骤标题数组
      type: Array,
      default: () => {
        return []
      }
    },
    stepsDesc: { // 步骤描述数组
      type: Array,
      default: () => {
        return []
      }
    },
    totalSteps: { // 步骤总数
      type: Number,
      default: 3
    },
    currentStep: { // 当前选中的步骤
      type: Number,
      default: 1
    },
    totalWidth: { // 步骤条总宽度
      type: Number,
      default: 900
    },
    descMaxWidth: { // 描述文本最大宽度
      type: Number,
      default: 140
    }
  },
  data () {
    return {
      // 若当前选中步骤超过总步骤数,则默认选择步骤1
      current: this.currentStep > this.totalSteps ? 1 : this.currentStep
    }
  },
  methods: {
    onChange (index) { // 点击切换选择步骤
      // e.stopPropagation()
      console.log('index:', index)
      if (this.current !== index) {
        this.current = index
        this.$emit('change', index)
      }
    }
  }
}
</script>
<style lang="less" scoped>
@themeColor: #1890FF;
.m-steps-area {
  margin: 0px auto;
  .m-steps {
    padding: 30px 0;
    display: flex;
    .m-steps-item {
      display: inline-block;
      overflow: hidden;
      font-size: 16px;
      line-height: 32px;
      &:not(:last-child) {
        margin-right: 16px;
        flex: 1; // 弹性盒模型对象的子元素都有相同的长度,且忽略它们内部的内容
      }
      .m-info-wrap {
        display: inline-block;
        cursor: pointer;
        .m-steps-icon {
          display: inline-block;
          margin-right: 8px;
          width: 32px;
          height: 32px;
          border-radius: 50%;
          text-align: center;
        }
        .m-steps-content {
          display: inline-block;
          vertical-align: top;
          .u-steps-title {
            position: relative;
            display: inline-block;
            padding-right: 16px;
          }
          .u-steps-description {
            font-size: 14px;
            word-wrap: break-word;
          }
        }
      }
    }
    .u-tail {
      background: #e8e8e8;
      position: absolute;
      top: 16px;
      left: 100%;
      display: block;
      width: 3000px;
      height: 1px;
      content: "";
    }
    .finished {
      &:hover {
        .m-steps-content {
          .u-steps-title {
            color: @themeColor;
          }
          .u-steps-description {
            color: @themeColor;
          }
        }
      }
      .m-steps-icon {
        background: #fff;
        border: 1px solid rgba(0,0,0,.25);
        border-color: @themeColor;
        .u-icon {
          fill: @themeColor;
          width: 16px;
          height: 16px;
          vertical-align: top;
          margin-top: 8px;
        }
      }
      .m-steps-content {
        color: rgba(0,0,0,.65);
        .u-steps-title {
          color: rgba(0,0,0,.65);
          &:after {
            .u-tail();
            background: @themeColor;
          }
        }
        .u-steps-description {
          color: rgba(0,0,0,.45);
        }
      }
    }
    .process {
      .m-steps-icon {
        background: @themeColor;
        border: 1px solid rgba(0,0,0,.25);
        border-color: @themeColor;
        .u-icon {
          color: #fff;
        }
      }
      .m-steps-content {
        color: rgba(0,0,0,.65);
        .u-steps-title {
          font-weight: 600;
          color: rgba(0,0,0,.85);
          &:after {
            .u-tail();
          }
        }
        .u-steps-description {
          color: rgba(0,0,0,.65);
        }
      }
    }
    .last-process {
      .m-steps-icon {
        background: @themeColor;
        border: 1px solid rgba(0,0,0,.25);
        border-color: @themeColor;
        .u-icon {
          color: #fff;
        }
      }
      .m-steps-content {
        color: rgba(0,0,0,.65);
        .u-steps-title {
          font-weight: 600;
          color: rgba(0,0,0,.85);
        }
        .u-steps-description {
          color: rgba(0,0,0,.65);
        }
      }
    }
    .middle-wait {
      &:hover {
        .m-steps-icon {
          border: 1px solid @themeColor;
          .u-icon {
            color: @themeColor;
          }
        }
        .m-steps-content {
          .u-steps-title {
            color: @themeColor;
          }
          .u-steps-description {
            color: @themeColor;
          }
        }
      }
      .m-steps-icon {
        background: #fff;
        border: 1px solid rgba(0,0,0,.25);
        .u-icon {
          color: rgba(0,0,0,.25);
        }
      }
      .m-steps-content {
        color: rgba(0,0,0,.65);
        .u-steps-title {
          color: rgba(0,0,0,.45);
          &:after {
            .u-tail();
          }
        }
        .u-steps-description {
          color: rgba(0,0,0,.45);
        }
      }
    }
    .last-wait {
      margin-right: 0;
      &:hover {
        .m-steps-icon {
          border: 1px solid @themeColor;
          .u-icon {
            color: @themeColor;
          }
        }
        .m-steps-content {
          .u-steps-title {
            color: @themeColor;
          }
          .u-steps-description {
            color: @themeColor;
          }
        }
      }
      .m-steps-icon {
        background: #fff;
        border: 1px solid rgba(0,0,0,.25);
        .u-icon {
          color: rgba(0,0,0,.25);
        }
      }
      .m-steps-content {
        color: rgba(0,0,0,.65);
        .u-steps-title {
          color: rgba(0,0,0,.45);
        }
        .u-steps-description {
          color: rgba(0,0,0,.45);
        }
      }
    }
  }
}
</style>

②在要使用的页面引入Steps组件,并传入相关初始数据:

<Steps
      :currentStep="3"
      :totalSteps="5"
      :stepsLabel="stepsLabel"
      :stepsDesc="stepsDesc"
      :totalWidth="900"
      :descMaxWidth="160"
      @change="onChange" />
import Steps from '@/components/Steps'
components: {
    Steps
},
data () {
    return {
      stepsLabel: ['Step 1', 'Step 2', 'Step 3', 'Step 4', 'Step 5'],
      stepsDesc: ['description 1', 'description 2', 'description 3', 'description 4', 'description 5']
    }
}
methods: {
    onChange (index) { // 父组件获取切换后的选中步骤
      console.log('current:', index)
    }
}
相关文章
|
移动开发 JavaScript 小程序
uView Steps 步骤条
uView Steps 步骤条
640 0
|
Java Windows
documents4j 文档转换
documents4j 文档转换
|
存储 JSON JavaScript
VUE2(七)VUE配置.env文件使用
公司的项目是基于vue2搭建的。 之前一直是采用一个.env文件来存储变量,然后在打包的时候修改.env文件中对应的变量的值。 但是这样做现阶段有两个小问题:
1354 0
|
JavaScript
基于Vue2超简单实现DIY定制的步骤条
这篇文章介绍了如何在Vue 2中实现一个自定义设计的步骤条组件,包括子组件的设计和在父页面中的使用示例。
664 1
基于Vue2超简单实现DIY定制的步骤条
基于微信小程序的线上博物馆系统
线上博物馆系统利用互联网与数字技术,实现文化遗产的数字化保护与传播,打破时空限制,推动文化传承与教育创新。结合Java、Vue及Uniapp等技术,构建跨平台、高互动的在线展览平台,提升公众文化体验。
|
JavaScript 前端开发 开发者
Vue 自定义进度条组件封装及使用方法详解
这是一篇关于自定义进度条组件的使用指南和开发文档。文章详细介绍了如何在Vue项目中引入、注册并使用该组件,包括基础与高级示例。组件支持分段配置(如颜色、文本)、动画效果及超出进度提示等功能。同时提供了完整的代码实现,支持全局注册,并提出了优化建议,如主题支持、响应式设计等,帮助开发者更灵活地集成和定制进度条组件。资源链接已提供,适合前端开发者参考学习。
980 17
Vue3步骤条(Steps)
这是一个基于 Vue2 的步骤条(Steps)组件,支持多种自定义属性,如步骤数组、宽度、大小、垂直显示、标签位置等。通过 `v-model` 可实现步骤的动态切换和点击交互。提供了丰富的样式调整选项,适用于各种场景下的多步骤流程引导。组件内详细展示了如何创建和使用步骤条,并提供了多个示例代码片段。
2360 4
Vue3步骤条(Steps)
|
JavaScript
Vue2进度条(Progress)
这篇文章介绍了如何在Vue 3框架中创建一个可自定义设置的进度条组件,包括进度条的宽度、当前进度、线条宽度和是否显示进度信息等属性。
864 2
Vue2进度条(Progress)
Element table组件封装 核心:父子组件传值、传方法
本文介绍了如何基于Element UI的table组件进行二次封装,创建一个具有父子组件传值和传方法功能的自定义表格组件,并提供了封装后的组件如何引入、注册和使用的方法。
581 0
Element table组件封装 核心:父子组件传值、传方法
|
小程序 PHP
全新UI自助图文打印系统小程序源码 PHP后端 附教程
全新UI自助图文打印系统小程序源码 PHP后端 附教程
1106 2