基于Vue实现一个"蛇形"步骤条

 更新时间:2024年11月29日 10:11:33   作者:coderzsp  
在现代Web应用中,步骤条作为一种常见的UI组件,广泛应用于表单提交、任务进度以及多步骤操作等场景,下面我们来看看如何利用Vue实现一个蛇形步骤条吧

前言

在现代Web应用中,步骤条(Step Progression)作为一种常见的UI组件,广泛应用于表单提交、任务进度以及多步骤操作等场景。为了提升用户体验,设计师往往会采用更加直观和有趣的方式展示步骤进度。今天,我们将通过 Vue 实现一个“蛇形”步骤图组件,这种独特的设计方式不仅能清晰地表达步骤的完成状态,还能给用户带来更加流畅和动态的交互体验。

前几天在做一个需求时就需要写一个步骤图组件,大致原型如下:

看到这个,内心无一点波澜,直接去项目配套 Ant Desin Vue 组件库中找对应组件拿来改改就 ojbk 了。

But,后端同事告诉我业务场景后发现还有操作空间,因为 UI 组件的 Step 步骤条不太符合当前业务,里面所有步骤都是连续的,但是当前业务场景是可以跳过其中一些步骤项,于是打算自己写一个,最后根据后端返回的数据来决定步骤项是否执行了。

customStep(V1)

customStep.vue 完整代码:

<template>
    <div class="custom-step">
        <div v-for="(item, index) in stepList" :key="index" class="step-item">
            <div class="item-content">
                <div class="step-title" @click="handleStepClick(index)">
                    <div class="step-num" :class="{ 'step-num-finished': item.status === 'finished' }">{{ index + 1 }}
                    </div>
                    <div class="setp-txt" :class="{ 'step-txt-finished': item.status === 'finished' }">{{ item.title }}
                    </div>
                </div>
                <div class="split-line" v-if="!item.isLast" :class="{ 'split-line-finished': isFinished(index) }"></div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, computed } from 'vue'

const props = defineProps({
    stepList: {
        type: Array,
        default: () => []
    }
})

// 步骤列表
// const stepList = ref([
//     { title: '确定意向', status: 'finished', isLast: false },
//     { title: '对接洽谈', status: 'finished', isLast: false },
//     { title: '项目报价', status: 'unfinished', isLast: false },
//     { title: '投标对比', status: 'unfinished', isLast: false },
//     { title: '合同拟定', status: 'unfinished', isLast: false },
//     { title: '转化完成', status: 'finished', isLast: false },
//     { title: '转化完成1', status: 'finished', isLast: false },
//     { title: '转化完成2', status: 'finished', isLast: true },
// ])

const isFinished = computed(() => index => {
    const prevStep = props.stepList[index];
    const nextStep = props.stepList[index + 1];
    return prevStep.status === 'finished' && nextStep.status === 'finished';
})

const emit = defineEmits(['stepClick'])
const handleStepClick = index => {
    emit('stepClick', index)
}
</script>

<style lang="less" scoped>
.custom-step {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    row-gap: 20px;
    width: 100%;
    padding: 0 30px;

    .step-item {
        width: calc(100% / 6);

        .item-content {
            box-sizing: border-box;
            display: flex;
            align-items: center;
        }

        .step-title {
            width: 80px;
            text-align: center;
            font-weight: 600;
            color: rgb(153, 153, 166);
            cursor: pointer;

            .step-num {
                box-sizing: content-box;
                width: 35px;
                margin: 0 auto;
                line-height: 35px;
                font-size: 16px;
                border: 3px solid #e3e8ec;
                border-radius: 100%;
            }

            .setp-txt {
                margin-top: 10px;
            }

            .step-num-finished {
                color: rgb(26, 188, 156);
                border: 3px solid rgb(26, 188, 156);
            }

            .step-txt-finished {
                color: rgb(26, 188, 156);
            }
        }

        .split-line {
            width: calc(100% - 80px);
            height: 3px;
            margin-top: -25px;
            background-color: #e3e8ec;
            border-radius: 5px;
        }

        .split-line-finished {
            background-color: rgb(26, 188, 156);
        }
    }
}
</style>

最后效果如下:

页面初始渲染步骤,然后可以点击某个步骤项,进行业务操作后刷新页面重新渲染步骤条,这样可以根据业务需求跳过某些步骤项。

But,还有事,我这固定了一行显示6个步骤项,超过6个会换行,但是显示就有一点瑕疵,因为正常元素布局都是从左到右,所以超过6个步骤项就会这样显示:

1 -> 2 -> 3 -> 4 -> 5 -> 6 ->

7 -> 8 -> ...

产品给我说这样看着不太连贯,想实现一个类似 "S" 形的,看着会连贯一些,后面找了个例子改了改,算是符合当前需求了。

customStep(V2)

customStep_.vue 完整代码:

<template>
    <div class="container">
        <div v-for="(item, index) in stepList" class="grid-item" :key="index">
            <div class="step" :class="{ 'step-finished': item.status === 'finished' }" @click="handleStepClick(index)">
                {{ item.title }}</div>
        </div>
    </div>
</template>

<script setup>
import { ref } from 'vue';

const props = defineProps({
    stepList: {
        type: Array,
        default: () => []
    }
})

// 步骤列表
// const stepList = ref([
//     { title: '确定意向', status: 'finished' },
//     { title: '对接洽谈', status: 'finished' },
//     { title: '项目报价', status: 'unfinished' },
//     { title: '投标对比', status: 'unfinished' },
//     { title: '合同拟定', status: 'unfinished' },
//     { title: '转化完成', status: 'finished' },
//     { title: '制定方案', status: 'unfinished' },
//     { title: '合同签订', status: 'finished' },
//     { title: '合同跟踪', status: 'finished' },
//     { title: '合同回款', status: 'unfinished' },
//     { title: '项目交付', status: 'unfinished' },
//     { title: '项目验收', status: 'unfinished' },
//     { title: '项目结束', status: 'unfinished' },
// ])

const emit = defineEmits(['stepClick'])
const handleStepClick = index => {
    emit('stepClick', index)
}

</script>

<style lang="less" scoped>
@colNum: 6; // 单行排列的步骤项个数(2、3、4、5、6、...) 
@colEven: @colNum * 2; // 两行元素数
@lineWidth: 35px; // 步骤间连线长度
@rowDistance: 50px; // 行间距
@colDistance: @lineWidth; // 列间距
@arrowSize: 6px; // 箭头大小
@stepColor: #9e9e9e; // 步骤颜色

.container {
    width: 100%;
    display: grid;
    padding: 30px 0;
    grid-template-columns: repeat(@colNum, 1fr);
    gap: @rowDistance @colDistance;
}

.grid-item {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;

    &::before {
        position: absolute;
        content: '';
        right: -@lineWidth;
        width: @lineWidth;
        height: 0;
        border-top: 1px dashed @stepColor;
    }

    &::after {
        content: '';
        position: absolute;
        right: (-@colDistance / 2);
        transform: translateX(50%);
        border-top: (@arrowSize / 1.4) solid transparent;
        border-left: @arrowSize solid @stepColor;
        border-bottom: (@arrowSize / 1.4) solid transparent;
    }

    // 给每行最后一个步骤(除最后一行)添加向下的连接箭头
    &:nth-child(@{colNum}n) {

        &:not(:last-child) {
            .step {
                &::before {
                    content: '';
                    position: absolute;
                    left: 50%;
                    bottom: -(@rowDistance / 2);
                    height: @lineWidth;
                    border-left: 1px dashed @stepColor;
                    transform: translate(-50%, 50%);
                }

                &::after {
                    content: '';
                    position: absolute;
                    left: 50%;
                    bottom: -(@rowDistance / 2);
                    border-top: @arrowSize solid @stepColor;
                    border-left: (@arrowSize / 1.4) solid transparent;
                    border-right: (@arrowSize / 1.4) solid transparent;
                    transform: translate(-50%, 50%);
                }
            }
        }
    }

    each(range(@colEven), {
        &:nth-child(@{colEven}n+@{value}) {
            @isEvenLine: boolean(@value > @colNum);
            @modNum: mod(@value, @colEven); // 余数 1、2、3、4、5、0

            /** 偶数行旋转箭头,步骤倒序排列(使用transform交换位置) */
            & when (@isEvenLine) {
                @transN: (@colNum + 1 + @colEven - @value - @value);
                transform: translateX(calc(@transN * 100% + @transN * @colDistance));

                &::after {
                    transform: translateX(50%) rotate(180deg) !important; // 旋转箭头
                }
            }

            // 最右排(n & n + 1 位)隐藏多余的箭头(如果container设置了overflow:hidden 则不用处理)
            & when (@modNum=@colNum), (@modNum=@colNum+1) {
                &::before, &::after {
                    display: none;
                }
            }

            // 最后一个步骤在奇数行 需要隐藏连线箭头
            & when not (@isEvenLine) {
                &:last-child {
                    &::before, &::after {
                        display: none;
                    }
                }
            }
        }
    })
}

.step {
    position: relative;
    width: 100px;
    line-height: 40px;
    font-size: 16px;
    text-align: center;
    border-radius: 5px;
    color: #9e9e9e;
    border: 2px solid #9e9e9e;
}

.step-finished {
    background-color: #4caf50;
    color: #fff;
    border: 2px solid #4caf50;
}
</style>

两种对比效果:

到此这篇关于基于Vue实现一个"蛇形"步骤条的文章就介绍到这了,更多相关Vue步骤条内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 详解Vue组件之间的数据通信实例

    详解Vue组件之间的数据通信实例

    本篇文章主要介绍了详解Vue组件之间的数据通信实例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-06-06
  • vue路由嵌套的SPA实现步骤

    vue路由嵌套的SPA实现步骤

    这篇文章主要为大家详细介绍了vue路由嵌套的SPA实现步骤,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-11-11
  • vue-cli常用设置总结

    vue-cli常用设置总结

    本文给大家总结了vue-cli常用设置,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2018-02-02
  • vue结合axios实现restful风格的四种请求方式

    vue结合axios实现restful风格的四种请求方式

    这篇文章主要介绍了vue结合axios实现restful风格的四种请求方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-06-06
  • VUE3数据的侦听超详细讲解

    VUE3数据的侦听超详细讲解

    在Vue3中watch特性进行了一些改变和优化,与computed不同,watch通常用于监听数据的变化,并执行一些副作用,这篇文章主要给大家介绍了关于VUE3数据侦听的相关资料,需要的朋友可以参考下
    2023-12-12
  • Vue中使用的EventBus有生命周期

    Vue中使用的EventBus有生命周期

    这篇文章主要介绍了Vue中使用的EventBus有生命周期的相关知识,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-07-07
  • 浅谈Vue3的几个优势

    浅谈Vue3的几个优势

    这篇文章主要给大家分享的是Vue3的几个优势,Vue3仍然在源码、性能和语法 API 三个大的方面进行了优化,下面我们一起进入文章看看具体详情吧
    2021-10-10
  • vue3如何使用ref获取元素

    vue3如何使用ref获取元素

    这篇文章主要介绍了vue3如何使用ref获取元素,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-05-05
  • 实用的 vue tags 创建缓存导航的过程实现

    实用的 vue tags 创建缓存导航的过程实现

    这篇文章主要介绍了实用的 vue tags 创建缓存导航的过程实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12
  • 详解vue 自定义组件使用v-model 及探究其中原理

    详解vue 自定义组件使用v-model 及探究其中原理

    这篇文章主要介绍了详解vue 自定义组件使用v-model 及探究其中原理,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-10-10

最新评论