小程序自定义下拉框

1.    下拉框显示效果
 
2.    wxml页面定义
<view class="condition">
    <view catchtap="select_item" class="item">
      商品类别
    </view>
    <view class='showImage'>
      <image src="../../images/triangle.png" />
    </view>
    <view class="item_list{{!select_type?' item_list_hide':' item_list_show'}}">
      <view wx:for="{{productTypeList}}" wx:key="{{index}}" catchtap="selectProductType" data-index="{{index}}" class="list">
        {{item.name}}
      </view>
    </view>
  </view>
3.    样式
首先在condition样式里把定位设置为相对定位,如此一来在这个层里面的元素都是相对定位。然后showImage是绝对定位,表示它在condition内部它的位置是绝对的
.condition {
   display: flex;
   position: relative;
   margin-top:10rpx;
 }

 .item_list{
  margin-top: 2rpx;
  position: absolute;
  top: 80rpx;
  left: 0;
  width: 100%;
  background-color: #fff;
  z-index: 100;
  font-size: 24rpx;
}
.item_list_show{
  top: 35px;
  animation: slide 200ms;
}
.item_list_hide{
  display: none !important;
}
.showImage{
  position: absolute;
  right: 250rpx;
  top: 18rpx;
}
.showImage image{
  width: 20rpx;
  height: 20rpx;
}
4.    js定义
页面定义的data-index可以用来获取列表下标
selectProductType: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    that.setData({
      categoryId: that.data.productTypeList[index].value,
      select_type: false,
    })
  },

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值