微信小程序·自定义时间轴组件

这篇博客介绍了一个用于记录事件的时间轴组件的实现。组件支持动态设置步骤、活动状态的颜色,并提供了相应的WXML和WXSS代码。通过设置`activeClass`、`activeBgColor`和`activeBdColor`属性,可以改变活动节点的背景色和边框色。使用代码示例展示了如何在步骤列表中应用该组件,显示事件时间和操作内容。时间轴的样式可根据实际需求进行自定义。
摘要由CSDN通过智能技术生成

背景

总有许许多多的事件需要记录~~因此编写了自定义时间轴组件满足一些相关需求

效果

时间记录在右侧
时间记录在左侧

组件代码


Component({
  /**
   * 组件的属性列表
   */
  properties: {
    steps: {
      type: Array,
      value: []
    },
    activeClass: {
      type: String,
      value: ''
    },
    activeBgColor: {
      type: String,
      value: '#000'
    },
    activeBdColor: {
      type: String,
      value: '#fff'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {

  }
})

<view class="item-content {{activeClass}}">
  <view class="icon-content">
    <text class="out-icon" style="background-color: {{iconStyle.getBgColor(activeClass, activeBgColor)}}; border-color: {{iconStyle.getBgColor(activeClass, activeBdColor)}};"></text>
    <view class="line"></view>
  </view>
  <view class="content">
    <slot></slot>
  </view>
</view>
<wxs module="iconStyle">
  var getBgColor = function (className, activeBgColor) {
    if (className.indexOf('first') !== -1) {
      return activeBgColor
    } else {
      return
    }
  }
  var getBdColor = function (className, activeBdColor) {
    if (className.indexOf('first') !== -1) {
      return activeBdColor
    } else {
      return
    }
  }
  module.exports = {
    getBgColor: getBgColor,
    getBdColor: getBdColor
  }
</wxs>
.item-content {
  display: flex;
  align-items: flex-start;
  position: relative;
}

.item-content .icon-content {
  height: 100%;
  position: absolute;
  margin-left: -30rpx;
}

.item-content .icon-content .line {
  width: 2rpx;
  height: calc(100% - 20rpx);
  position: absolute;
  background-color: #e5e5e5;
  top: 20rpx;
}

.item-content .icon-content {
  width: 32rpx;
  display: flex;
  justify-content: center;
}

.out-icon {
  display: block;
  width: 20rpx;
  height: 20rpx;
  border-radius: 50%;
  line-height: 20rpx;
  background-color: #e5e5e5;
  z-index: 2;
}

.content {
  margin: -6rpx 0 20rpx 20rpx;
}

.first-item .icon-content .out-icon {
  border: 4rpx solid;
}

.last-item .line {
  display: none;
}
{
  "component": true
}

使用代码

        <view class="content">
            <my-steps wx:for="{{stepList}}" wx:key="index" activeClass="{{index === 0 ? 'first-item' : '' }} {{index === stepList.length-1 ? 'last-item' : '' }}" activeBgColor="pink" activeBdColor="rgb(255, 213, 220)">
                <view class="item {{index === 0 ? 'first-item' : '' }} {{index === stepList.length-1 ? 'last-item' : '' }}">
                	<!-- 具体样式自定义设置 -->
                    <view class="">{{item.eventTime}}</view>
                    <view class="">操作内容:{{item.eventType}}</view>
                    <view class="">
                        <text>操作人:{{item.operatorPerson}};</text>
                    </view>
                </view>
            </my-steps>
        </view>
        stepList: [
            {
                eventTime: "05-09 00:00",
                eventType: '获取信息',
                operatorPerson: "张三"
            },
            {
                eventTime: "05-09 00:00",
                eventType: '获取信息',
                operatorPerson: "张三"
            },
            {
                eventTime: "05-09 00:00",
                eventType: '获取信息',
                operatorPerson: "张三"
            }
        ]

备注

时间轴相关内容的布局设计需根据实际场景自定义~~
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微信小程序是一种基于微信平台的应用程序,它可以在微信中直接运行,无需下载安装。而自定义组件是小程序中的一种重要功能,它允许开发者将一些常用的UI元素封装成组件,以便在不同的页面中复用。 自定义组件具有以下特点: 1. 组件是由wxml、wxss和js文件组成,可以独立定义样式和逻辑。 2. 组件可以接受外部传入的数据,通过属性进行配置。 3. 组件可以触发事件,向外部传递消息。 4. 组件可以包含子组件,形成组件的嵌套结构。 使用自定义组件的步骤如下: 1. 在小程序项目中创建一个新的文件夹,用于存放自定义组件的相关文件。 2. 在该文件夹中创建一个wxml文件,定义组件的结构。 3. 在同一文件夹中创建一个wxss文件,定义组件的样式。 4. 在同一文件夹中创建一个js文件,定义组件的逻辑。 5. 在需要使用该组件的页面中引入组件,并在wxml中使用组件标签。 例如,我们创建一个名为"custom-component"的自定义组件,其文件结构如下: ``` custom-component/ ├── custom-component.wxml ├── custom-component.wxss └── custom-component.js ``` 在custom-component.wxml中定义组件的结构,例如: ```html <view class="custom-component"> <text>{{text}}</text> <button bindtap="handleClick">点击按钮</button> </view> ``` 在custom-component.wxss中定义组件的样式,例如: ```css .custom-component { background-color: #f5f5f5; padding: 10px; } ``` 在custom-component.js中定义组件的逻辑,例如: ```javascript Component({ properties: { text: { type: String, value: '默认文本' } }, methods: { handleClick() { this.triggerEvent('click', { message: '按钮被点击了' }); } } }) ``` 在需要使用该组件的页面中引入组件,并在wxml中使用组件标签,例如: ```html <custom-component text="Hello World" bind:click="handleCustomComponentClick"></custom-component> ``` 以上就是微信小程序自定义组件的简单介绍。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值