微信小程序:实现节点进度条的效果;正在完成的节点有动态循环效果;横向,纵向排列

参考说明

微信小程序实现流程进度功能 - 知乎

上面的为一个节点进度条的例子,但并不完整,根据上述代码,进行修改完善,实现其效果

横向效果

代码

wxml

html 复制代码
<view class='order_process'>
  <view class='process_wrap' wx:for="{{processData}}" wx:key="index">
    <view class='process'>
      <view class='process_line' style="background:{{item.start}}"></view>
      <image class="process_icon {{item.icon === icon2 ? 'rotate-icon' : ''}}" src="{{item.icon}}"></image>
      <view class='process_line' style="background:{{item.end}}"></view>
    </view>
    <text class='process_name'>{{item.name}}</text>
  </view>
</view>

wxss

css 复制代码
.order_process {
  display: flex;
  flex-wrap: nowrap;
  padding: 10rpx 10rpx 20rpx 10rpx;
  background-color: #fff;
}
.process_wrap {
  display: flex;
  flex-direction: column;
  flex: 1;
  align-items: center;
}
.process {
  display: flex;
  align-items: center;
  width: 100%;
}
.process_icon {
  width: 35rpx;
  height: 35rpx;
}
.process_line {
  background: #eff3f6;
  flex: 1;
  height: 5rpx;
}
.process_name {
  font-size: 24rpx;
}
/* 定义旋转动画 */
@keyframes rotate360 {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
/* 应用到 .rotate-icon 类 */
.rotate-icon {
  animation: rotate360 5s linear infinite; /* 5秒完成一次旋转,线性时间函数,无限循环 */
}

js

javascript 复制代码
Page({
  data: {
    icon1: '../img/process_1.png',
    icon2: '../img/process_2.png',
    icon3: '../img/process_3.png',
    processData: [],//节点信息
  },
  onLoad: function () {
    // 初始化数据 processData
    const data = [{
        name: '节点1',
        start: '#fff',
        end: '#f9d9dd',
        icon: this.data.icon1,
        state: 0
      },
      {
        name: '节点2',
        start: '#f9d9dd',
        end: '#f9d9dd',
        icon: this.data.icon1,
        state: 0
      },
      {
        name: '节点3',
        start: '#f9d9dd',
        end: '#f9d9dd',
        icon: this.data.icon1,
        state: 1
      },
      {
        name: '节点4',
        start: '#f9d9dd',
        end: '#f9d9dd',
        icon: this.data.icon1,
        state: 0
      },
      {
        name: '节点5',
        start: '#f9d9dd',
        end: '#fff',
        icon: this.data.icon1,
        state: 0
      }
    ]
    this.setData({
      processData: data
    })
    // 处理节点信息
    this.setProcessIcon();
  },
  setProcessIcon: function () {
    const processArr = this.data.processData;
    let index = -1; // 记录状态为1的最后的位置
  
    // 首先找到状态为1的节点位置
    processArr.forEach((item, i) => {
      if (item.state === 1) {
        index = i;
        return false; // 找到后停止循环
      }
    });
  
    // 然后根据找到的位置设置图标和其他属性
    processArr.forEach((item, i) => {
      if (i === index) { // 当前正在处理的节点
        item.icon = this.data.icon2;
        item.start = "#f0a0a9";
        item.end = "#f9d9dd";
      } else if (i < index && index !== -1) { // 已完成的节点
        item.icon = this.data.icon3;
        item.start = "#f0a0a9";
        item.end = "#f0a0a9";
      } else { // 未完成的节点
        item.icon = this.data.icon1;
        item.start = "#f9d9dd";
        item.end = "#f9d9dd";
      }
    });
  
    // 特殊处理第一个和最后一个节点
    if (processArr.length > 0) {
      processArr[0].start = "#fff";
      processArr[processArr.length - 1].end = "#fff";
    }
  
    this.setData({
      processData: processArr
    });
  }
});

纵向效果

代码

wxml

html 复制代码
<view class='order_process1'>
  <view class='process_item1' wx:for="{{processData}}" wx:key="index">
    <view class='process_vertical1'>
      <view class='process_line_vertical1' style="background:{{item.start}};"></view>
      <image class="process_icon1 {{item.icon === icon2 ? 'rotate-icon1' : ''}}" src="{{item.icon}}"></image>
      <view class='process_line_vertical1' style="background:{{item.end}};"></view>
    </view>
    <text class='process_name1'>{{item.name}}</text>
  </view>
</view>

wxss

css 复制代码
.order_process1 {
  display: flex;
  flex-direction: column; /* 改为垂直排列 */
  padding: 10rpx;
  background-color: #fff;
}

.process_item1 {
  display: flex;
  flex-direction: row; /* 每个步骤项水平排列 */
  align-items: center; /* 垂直居中对齐 */
}

.process_vertical1 {
  display: flex;
  flex-direction: column; /* 步骤线垂直排列 */
  align-items: center; /* 图标居中 */
}

.process_icon1 {
  width: 35rpx;
  height: 35rpx;
}

.process_line_vertical1 {
  background: #eff3f6;
  width: 5rpx; /* 宽度变为5rpx */
  height: 40rpx; /* 高度根据需要设置 */
}

.process_name1 {
  font-size: 24rpx;
  margin-left: 20rpx; /* 文字与图标之间的间距 */
}

/* 定义旋转动画 */
@keyframes rotate360 {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

/* 应用到 .rotate-icon1 类 */
.rotate-icon1 {
  animation: rotate360 5s linear infinite; /* 5秒完成一次旋转,线性时间函数,无限循环 */
}

js

参见横向js代码

图片展示

process_1.png

process_2.png

process_3.png

相关推荐
腾讯云云开发1 小时前
3小时上线!云开发“零运维”外卖小程序指南
微信小程序·ai编程·小程序·云开发
腾讯云云开发1 小时前
云开发1天极速开店!1人搞定全流程
微信小程序·ai编程·小程序·云开发
腾讯云云开发2 小时前
预约+会员+营销全功能覆盖,1 小时上架美业小程序
低代码·微信小程序·小程序·云开发
腾讯云云开发2 小时前
云开发模板建站,1小时速建官网,企业品牌即刻上线
微信小程序·ai编程·小程序·云开发
腾讯云云开发2 小时前
手把手教你用CloudBase完成电商小程序全栈开发
微信小程序·ai编程·小程序·云开发
景彬3 小时前
小红书小组件开发 最早踩坑版
前端·微信小程序
00后程序员张6 小时前
iOS 26 App 运行状况全面解析 多工具协同监控与调试实战指南
android·ios·小程序·https·uni-app·iphone·webview
2501_916007477 小时前
iOS 混淆实战,多工具组合完成 IPA 混淆、加固与发布治理(iOS混淆|IPA加固|无源码混淆|App 防反编译)
android·ios·小程序·https·uni-app·iphone·webview
2501_915918417 小时前
怎么上架 App?iOS 应用上架完整流程详解与跨平台发布实战指南
android·ios·小程序·https·uni-app·iphone·webview
2501_916008897 小时前
iOS 混淆工具链实战 多工具组合完成 IPA 混淆与加固(iOS混淆|IPA加固|无源码加固|App 防反编译)
android·ios·小程序·https·uni-app·iphone·webview