微信小程序自定义步骤条效果

微信小程序自定义一个步骤条组件,自定义文字在下面,已完成和未完成和当前进度都不一样的样式,可点击上一步和下一步切换流程状态,效果如下。

这是视频效果:

前端实现步骤条效果

下面我们一步步实现编码,自定义的步骤条组件写在components文件夹下的custom-steps文件夹中。custom-steps文件夹中有custom-steps.json、custom-steps.wxml、custom-steps.wxss、custom-steps.js四个文件。

首先编写custom-steps.json文件。

javascript 复制代码
{
  "component": true,
  "usingComponents": {}
}

其次,编写custom-steps.wxml文件。

wxml文件代码结构也很简单,首先for循环steps状态, 三个状态的icon放在image中,用条件判断渲染不同状态对应显示的icon,用不同class控制状态样式;<view class="step-line" wx:if="{{index !== steps.length - 1}}"></view>这个代码是控制步骤间的横向样式,不是最后一步就显示步骤中间的那条横线;以及控制上一步、下一步的按钮。

html 复制代码
<view class="steps-container">
  <view class="steps">
    <view wx:for="{{steps}}" wx:key="index" class="step {{item.status}}" bindtap="handleClickStep" data-id="{{index}}">
      <view class="step-icon">
        <image class="icon-active" wx:if="{{item.status === 'active'}}" src="{{item.activeIcon}}" />
        <image class="icon-completed" wx:elif="{{item.status === 'completed'}}" src="{{item.completedIcon}}" />
        <image class="icon-inactive" wx:else src="{{item.inactiveIcon}}" />
        <view class="step-line" wx:if="{{index !== steps.length - 1}}"></view>
      </view>
      <view class="step-title">{{item.title}}</view>
    </view>
  </view>
</view>
<view class="btn-group">
  <button wx:if="{{index !== 0}}" bindtap="prevStep">上一步</button>
  <button wx:if="{{index !== steps.length - 1}}" bindtap="nextStep">下一步</button>
</view>

其次,编写custom-steps.wxss文件。可根据自己的需求对样式进行调整。

css 复制代码
.steps-container {
  display: flex;
  padding: 0 55px;
  justify-content: space-between;
  width: 100%;
}
.steps {
  display: flex;
  justify-content: space-between;
  width: 640px;
}

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

.step-icon {
  width: 44px;
  height: 44px;
  position: relative;
  margin: 0 24px 15px;
}

.step-title {
  color: #FFEBAA;
  text-align: center;
  font-size: 28px;
  font-weight: 500;
  line-height: 44px;
}

.step-title image {
  width: 44px;
  height: 44px;
}

.step-line {
  width: 120px;
  height: 4px;
  background-color: #ccc;
  position: absolute;
  left: 72px;
  top: 24px;
}

接着,编写custom-steps.js文件。定义了上一步、下一步、点击当前步骤等相关操作逻辑。

javascript 复制代码
Component({
  properties: {
    steps: {
      type: Array,
      value: [],
    },
    index: {
      type: Number,
      value: 0,
    },
  },
  data: {},
  pageLifetimes: {
    show() {},
    hide() {},
  },
  methods: {
    // 上一步
    prevStep() {
      this.setData({
        index: this.data.index - 1,
      });
    },
    // 下一步
    nextStep() {
      this.setData({
        index: this.data.index + 1,
      });
    },
    handleClickStep(e) {
      // 点击将当前步骤设置为激活状态,当前步骤之前为已完成状态,之后为未激活状态
      this.setData({
        steps: this.data.steps.map((item, i) => {
          const { id } = e.currentTarget.dataset;
          if (i === id) {
            item.status = 'active';
          } else if (i < id) {
            item.status = 'completed';
          } else {
            item.status = 'inactive';
          }
          return item;
        }),
      });
    },
  },
  lifetimes: {
    attached() {},
    detached() {},
  },
});

组件编写完了,接下来就在页面引入了。在pages文件下新建页面文件夹 home,home里面有index.json, index.wxss, index.js, index.wxml文件。

首先,index.json文件通过 usingComponents 引用自定义组件custom-steps

javascript 复制代码
{
  "usingComponents": {
    "custom-steps": "./components/custom-steps/custom-steps"
  }
}

接着,在index.wxml文件中引入custom-steps

html 复制代码
 <view class="steps-wrapper">
    <custom-steps steps="{{steps}}"></custom-steps>
 </view>

然后,在index.js里配置steps数据。其中title为显示的文字,status为状态,inactiveIcon、 activeIcon、completedIcon为三个状态对应的icon,根据自己需求替换这些内容就好了。

javascript 复制代码
Page({
  data: {
    steps: [
      {
        title: '选择模板',
        status: 'completed',
        inactiveIcon: '',
        activeIcon: '/assets/active1.png',
        completedIcon: '/assets/success.png',
      },
      {
        title: '上传正面照',
        status: 'active',
        inactiveIcon: '/assets/inactive2.png',
        activeIcon: '/assets/active2.png',
        completedIcon: '/assets/success.png',
      },
      {
        title: 'AI生成贺词',
        status: 'inactive',
        inactiveIcon: '/assets/inactive3.png',
        activeIcon: '/assets/active3.png',
        completedIcon: '/assets/success.png',
      },
    ],
  },
});

这样就实现步骤条效果啦 O(∩_∩)O。

相关推荐
说私域16 小时前
定制开发AI智能名片S2B2C预约服务小程序的定制开发与优势分析
大数据·人工智能·小程序
云起SAAS1 天前
名字姓名起名打分评分抖音快手微信小程序看广告流量主开源
微信小程序·小程序·ai编程·看广告变现轻·名字姓名起名打分评分
说私域1 天前
从裂变能力竞争到技术水平竞争:开源AI智能名片链动2+1模式S2B2C商城小程序对微商企业竞争格局的重塑
人工智能·小程序·开源
开发加微信:hedian1161 天前
短剧小程序开发全攻略:技术选型与实现思路
微信·小程序·架构·aigc·交互
2501_915918411 天前
移动端 HTTPS 抓包实战,多工具组合分析与高效排查指南
数据库·网络协议·ios·小程序·https·uni-app·iphone
星光一影1 天前
陪诊陪检系统源码,陪诊小程序,陪诊APP,陪诊服务,家政上门系统,居家护理陪护源码
mysql·小程序·uni-app·php
阿奇__2 天前
uniapp h5 app 小程序获取当前定位
小程序·uni-app
2501_915106322 天前
iOS性能调优的系统化实践,从架构分层到多工具协同的全流程优化指南(开发者深度版)
android·ios·小程序·架构·uni-app·iphone·webview
李纲明2 天前
Wordpress如何选择适合外贸的模板主题?
微信小程序·php
一 乐2 天前
游戏助手|游戏攻略|基于SprinBoot+vue的游戏攻略系统小程序(源码+数据库+文档)
数据库·vue.js·spring boot·后端·游戏·小程序