微信小程序----button组件

2021-02-01 11:11:59 浏览数 (1)

展示效果图

button组件的常用属性

  1. size:default、mini----default为块级按钮、mini为小按钮
  2. type:primary、default、warn----primary提交成功、default默认灰色、warn警告色
  3. plain:true、false----按钮是否镂空,背景色透明
  4. disabled:true、false----是否禁用
  5. loading:true、false----名称前是否带 loading 图标

WXML

代码语言:javascript复制
<view class="tui-btn-group">
  <view class="tui-btn-content">
    <button size="{{defaultSize}}" bindtap="default" hover-class="other-button-hover"> default </button>
  </view>
  <view class="tui-btn-content">
    <button type="primary" size="{{primarySize}}" bindtap="primary"> primary </button>
  </view>
  <view class="tui-btn-content">
    <button type="warn" size="{{warnSize}}" bindtap="warn"> warn </button>
  </view>
  <view class="tui-btn-content">
    <button bindtap="setDisabled" disabled="{{disabled}}" type="primary">点击设置按钮disabled属性true</button>
  </view>
  <view class="tui-btn-content">
    <button bindtap="setPlain"  plain="{{plain}}">点击设置按钮plain属性</button>
  </view>
  <view class="tui-btn-content">
    <button bindtap="setLoading" loading="{{loading}}" type="warn">点击设置按钮loading属性</button>
  </view>
</view>

WXSS

代码语言:javascript复制
.tui-btn-group{
  padding: 10px;
}
.tui-btn-content{
  height: 60px;
  line-height: 60px;
}
/** 修改button默认的点击态样式类**/
.button-hover {
  background-color: red;
}
/** 添加自定义button点击态样式类**/
.other-button-hover {
  background-color: blue;
}

JS

代码语言:javascript复制
var types = ['default', 'primary', 'warn']
var pageObject = {
  data: {
    defaultSize: 'default',
    primarySize: 'default',
    warnSize: 'default',
    disabled: false,
    plain: false,
    loading: false
  },
  setDisabled: function (e) {
    this.setData({
      disabled: !this.data.disabled
    })
  },
  setPlain: function (e) {
    this.setData({
      plain: !this.data.plain
    })
  },
  setLoading: function (e) {
    this.setData({
      loading: !this.data.loading
    })
  }
}

//循环给'default', 'primary', 'warn'按钮创建函数
for (var i = 0; i < types.length;   i) {
  (function (type) {
    pageObject[type] = function (e) {
      var key = type   'Size'
      var changedData = {}
      changedData[key] =
        this.data[key] === 'default' ? 'mini' : 'default'
      this.setData(changedData)
    }
  })(types[i])
}

Page(pageObject);

0 人点赞