zoukankan      html  css  js  c++  java
  • 微信小程序模仿select组件

    js

    const computedBehavior = require("../../utils/computed");
    
    Component({
      behaviors: [computedBehavior],
      /**
       * 组件的属性列表
       */
      properties: {
        // 选项数组
        options: {
          type: Array,
          value: [],
          observer(options) {
            this.setData({
              options
            })
          }
        },
        // 值的字段
        valueKey: {
          type: String,
          value: 'value'
        },
        // 展示用的text的字段
        textKey: {
          type: String,
          value: 'text'
        },
        value: {
          type: Number | String,
          value: ''
        },
        defaultText: {
          type: String,
          value: '请选择'
        }
      },
      /**
       * 组件的初始数据
       */
      data: {
        selectShow: false, // 初始option不显示
        text: "", // 初始内容
        animationData: {} // 右边箭头的动画
      },
      computed: {
        showText() {
          const text = this.data.options.find(x => x[this.properties.valueKey] === this.properties.value) || {}
          return text[this.properties.textKey] || this.data.text || this.properties.defaultText
        }
      },
      /**
       * 组件的方法列表
       */
      methods: {
        //option的显示与否
        selectToggle() {
          const nowShow = this.data.selectShow;//获取当前option显示的状态
          //创建动画
          const animation = wx.createAnimation({
            timingFunction: "ease"
          })
          this.animation = animation;
          if (nowShow) {
            animation.rotate(0).step();
            this.setData({
              animationData: animation.export()
            })
          } else {
            animation.rotate(180).step();
            this.setData({
              animationData: animation.export()
            })
          }
          this.setData({
            selectShow: !nowShow
          })
        },
        //设置内容
        selectChange({ currentTarget }) {
          const index = currentTarget.dataset.index;
          const currentItem = this.properties.options[index]
          this.animation.rotate(0).step();
          this.setData({
            selectShow: false,
            text: currentItem[this.properties.textKey],
            animationData: this.animation.export()
          })
          this.triggerEvent("change", currentItem)
        },
        // 重置
        reset() {
          this.animation.rotate(0).step();
          this.setData({
            selectShow: false,
            text: "",
            animationData: this.animation.export()
          })
          this.triggerEvent("change", {})
        }
      }
    })
    

      

    wxml

    <view class="com-select-wrap">
      <view class="com-select-content" bindtap="selectToggle">
        <view class="com-select-value">{{showText}}</view>
        <image src="/img/icon_down.png" class="com-select-arrow" animation="{{animationData}}" />
      </view>
      <!-- cover-view防止点击传透 -->
      <cover-view class="com-select-list" wx:if="{{selectShow}}">
        <cover-view
          wx:for="{{options}}"
          data-index="{{index}}"
          wx:key
          class="com-select-item {{item[valueKey]===value?'active':''}}"
          catch:tap="selectChange"
        >{{item[textKey]}}</cover-view>
      </cover-view>
    </view>
    

     wxss

    .com-select-wrap {
       100%;
      border: 1px solid #f5f5f5;
      border-radius: 8rpx;
      position: relative;
      font-size: 28rpx;
    }
    .com-select-content {
      padding: 0 30rpx;
      display: flex;
      align-items: center;
      border-radius: 8rpx;
      background-color: #fff;
      height: 72rpx;
      line-height: 72rpx;
    }
    .com-select-arrow {
       20rpx;
      height: 12rpx;
      transition: all 0.3s ease;
    }
    .com-select-value {
      flex: 1;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    .com-select-list {
      position: absolute;
      top: 72rpx;
      left: 0;
      right: 0;
      z-index: 3;
      padding: 0 30rpx;
      box-sizing: border-box;
      border: 1px solid #f5f5f5;
      border-radius: 8rpx;
      overflow-y: auto;
      max-height: 420rpx;
      background-color: #fff;
      box-shadow: 0 10rpx 16rpx 0 rgba(0, 0, 0, 0.1);
    }
    .com-select-item {
      height: 84rpxpx;
      line-height: 84rpx;
      border-bottom: 1px solid #f5f5f5;
      text-align: left;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    .com-select-item.active {
      color: #fece2b;
    }
    .com-select-item:last-of-type {
      border-bottom: 0 none;
    }
  • 相关阅读:
    J2SE总结
    OSI模型与TCP/IP协议族
    poppler交叉编译
    摆脱技术思维,转向产品思维——寻找“万能”IDC的苦恼
    面向自由职业者和小型企业的开源开票工具
    3星|《中国做对了什么》:十几年前的文章集了,依旧不过时
    2星|《巴菲特致股东的信》:标题党,实际是1996年一次研讨会的发言记录,没有致股东的信
    3星|《不会被机器替代的人》:人在被服务的时候,更喜欢面对面跟人打交道,而不是跟机器打交道
    3星|《提高职场执行力》:执行力难关的根源是对话的无效性
    2星|《工业X.0》:物联网的资料汇编
  • 原文地址:https://www.cnblogs.com/haitaoblog/p/11991048.html
Copyright © 2011-2022 走看看