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 步骤条
473 0
|
JavaScript
Vue2使用全局函数或变量的两种常用方式
这篇文章介绍了在Vue 2项目中实现全局函数或变量的两种常用方式:一种是通过挂载到Vue的`prototype`,另一种是使用Vue的全局混入(Vue.mixin)。
1450 0
Vue2使用全局函数或变量的两种常用方式
|
JavaScript
基于Vue2超简单实现DIY定制的步骤条
这篇文章介绍了如何在Vue 2中实现一个自定义设计的步骤条组件,包括子组件的设计和在父页面中的使用示例。
470 1
基于Vue2超简单实现DIY定制的步骤条
Vue3 父组件调用子组件方法($refs 在setup()、<script setup> 中使用)
Vue3 父组件调用子组件方法($refs 在setup()、<script setup> 中使用)
2646 0
|
小程序 前端开发 API
【微信小程序】TypeError: Cannot read property ‘get‘ of undefined & Error: MiniProgramError
【微信小程序】TypeError: Cannot read property ‘get‘ of undefined & Error: MiniProgramError
|
前端开发 JavaScript 应用服务中间件
前端跨域问题解决Access to XMLHttpRequest at xxx from has been blocked by CORS policy
跨域问题是前端开发中常见且棘手的问题,但通过理解CORS的工作原理并应用合适的解决方案,如服务器设置CORS头、使用JSONP、代理服务器、Nginx配置和浏览器插件,可以有效地解决这些问题。选择合适的方法可以确保应用的安全性和稳定性,并提升用户体验。
8017 90
Vue2时间轴(横向)
该横向时间轴组件 `HorizonTimeLine` 可自定义时间轴数据 (`timelineData`) 和初始选中年份 (`activeYear`)。通过点击不同的年份,可以改变当前激活的年份,并有相应的视觉反馈。该组件支持均匀自适应排列,且易于在其他页面中引入和使用。
1333 0
Vue2时间轴(横向)
Vue3步骤条(Steps)
这是一个基于 Vue2 的步骤条(Steps)组件,支持多种自定义属性,如步骤数组、宽度、大小、垂直显示、标签位置等。通过 `v-model` 可实现步骤的动态切换和点击交互。提供了丰富的样式调整选项,适用于各种场景下的多步骤流程引导。组件内详细展示了如何创建和使用步骤条,并提供了多个示例代码片段。
1931 4
Vue3步骤条(Steps)
|
监控 网络协议 网络安全
|
JavaScript
Vue2进度条(Progress)
这篇文章介绍了如何在Vue 3框架中创建一个可自定义设置的进度条组件,包括进度条的宽度、当前进度、线条宽度和是否显示进度信息等属性。
680 2
Vue2进度条(Progress)

热门文章

最新文章