美文网首页
小程序的一些组件

小程序的一些组件

作者: 老周_o_o | 来源:发表于2018-05-23 15:43 被阅读0次

一,下拉选择框

微信小程序里没有和HTML里的下拉框一样的组件,想要相同的效果只能自己写一个,我参考的“西江无月”的文章

  • 显示效果


  • wxml
<view class='select_box'>
<!--一直显示的已选择框-->
    <view class='select' catchtap='selectTap'>
        <text class='select_text'>{{selectData[index]}}</text>
        <!--一直显示的已选择框右侧的箭头图标-->
        <image class='select_img {{show&&"select_img_rotate"}}'       src='/images/more_unfold.png'></image>         
    </view>

<!--点击才显示的选项框  -->
    <view class='option_box' style='height:{{show?(selectData.length>5?300:selectData.length*60):0}}rpx;'>
        <text class='option' style='{{index==selectData.length-1&&"border:0;"}}' wx:for='{{selectData}}' wx:key='this' data-index='{{index}}' catchtap='optionTap'>{{item}}</text>
    </view>
</view>
<!--
  show&&"select_img_rotate"-----给显示框右边的下拉箭头添加动画,当show的值为true的时候,就启用select_img_rotate这个样式,旋转180度的样式

  height:{{show?(selectData.length>5?300:selectData.length*60):0}}rpx;-----给改变下拉框高度,实现下拉框的显示隐藏,每个下拉选项的高度为60,下拉框的最大高度这里设置的是300,所以这里写成当数据长度大于5时,下拉框高度为300,反之下拉框高度为数据长度乘以60

  index==selectData.length-1&&"border:0;"-----取消下拉选项的最后一个的下边框
  -->
  • js
Page({
  data: {
    show: false,//控制下拉列表的显示隐藏,false隐藏、true显示
    selectData: ['1', '2', '3', '4', '5', '6'],//下拉列表的数据
    index: 0//选择的下拉列表下标
  },
  // 点击下拉显示框
  selectTap() {
    this.setData({
      show: !this.data.show  //每点击一次已选择框,show的值就取反一次
    });
  },
  // 点击下拉列表
  optionTap(e) {
    let Index = e.currentTarget.dataset.index;//获取点击的下拉列表的下标
    this.setData({
      index: Index,
      show: !this.data.show
    });
}
})
  • wxss
page{
  background: #f3f7f7;
}
.select_box{
  background: #fff;
  width: 80%;
  margin: 30rpx auto;
  position: relative;
}
.select{
  box-sizing: border-box;
  width: 100%;
  height: 70rpx;
  border:1px solid #efefef;
  border-radius: 8rpx;
  display: flex;
  align-items: center;
  padding: 0 20rpx;
}
.select_text{
  font-size: 30rpx;
  flex: 1;
}
.select_img{
  width: 40rpx;
  height: 40rpx;
  display: block;
  transition:transform 0.3s;
}
.select_img_rotate{
  transform:rotate(180deg); 
}
.option_box{
  position: absolute;
  top: 70rpx;
  width: 100%;
  border:1px solid #efefef;
  box-sizing: border-box;
  height: 0;
  overflow-y: auto;
  border-top: 0;
  background: #fff;
  transition: height 0.3s;
}
.option{
  display: block;
  line-height: 40rpx;
  font-size: 30rpx;
  border-bottom: 1px solid #efefef;
  padding: 10rpx;
}

二,输入框

  <form bindsubmit="submit_e" action=""> 
    <input class='student_input' placeholder='bbb' placeholder_type='color: #c9dce2;font-size: 90rpx;' auto-focus='true' placeholder-class='input_placeholder' bindinput="input_e" bindconfirm='confirm_e'></input>
    <button formType="submit">提交</button> 
  </form>
  <!--
  placeholder_type属性用来规定提示语的样式,也可以用placeholder_class属性在wxss文件中指定;
  auto_focus属性,当页面只有一个输入框的时候自动聚焦并拉起输入键盘,开发工具上不显示键盘;
  bindsubmit属性,点击提交按钮时执行的函数,在js中定义函数;
  bindinput属性,输入字符时执行的函数,在js中定义函数;
  bindconfirm属性,按键盘上“回车”或“完成”时执行的函数,在js中定义函数;
-->
  • js
  submit_e: function (e) {
    console.log('form发生了submit事件,携带数据为:', e)
  },
  input_e(e){
    console.log('input执行了,目前的输入框内内容是:',e.detail.value)
  },
  confirm_e(e){
    console.log('按下了回车键,confirm执行了,输入框内内容是:',e.detail.value)
  }

相关文章

网友评论

      本文标题:小程序的一些组件

      本文链接:https://www.haomeiwen.com/subject/dwbrjftx.html