//html布局
<view class='select_box'>
<view class='select' catchtap='selectTap'>
<text class='select_text'>{{selectData[index]}}</text>
<image class='select_img {{selectShow&&"select_img_rotate"}}' src='../../images/b2.png' background-size="contain"></image>
</view>
<view class='option_box' style='height:{{selectShow?(selectData.length>5?325:selectData.length*50):0}}rpx;'>
<text class='option' wx:for='{{selectData}}' wx:key='this' data-index='{{index}}' catchtap='optionTap'>{{item}}</text>
</view>
</view>
//css样式
.select_box{
width:45%;
height:70%;
border-radius: 14rpx;
position: relative;
}
.select_box .select{
box-sizing: border-box;
width: 100%;
height: 100%;
border-radius: 8rpx;
display: flex;
align-items: center;
padding: 0 10rpx;
}
.select_box .select .select_text{
font-size: 26rpx;
color: #777777;
line-height: 28rpx;
flex: 1;
}
.select_box .select .select_img{
width: 30rpx;
height: 30rpx;
display: block;
transition:transform 0.3s;
}
.select_box .select .select_img_rotate{
transform:rotate(180deg);
}
.select_box .option_box{
position: absolute;
top: calc(100% - 1px);
width: 100%;
box-sizing: border-box;
height: 0;
overflow-y: auto;
background: #fff;
transition: height 0.3s;
border-left:1px solid #efefef;
border-right:1px solid #efefef;
}
.select_box .option_box .option{
display: block;
line-height: 30rpx;
font-size: 26rpx;
border-top: 1px solid #efefef;
border-bottom: 1px solid #efefef;
padding: 10rpx;
}
js逻辑
data: {
selectShow: false,//控制下拉列表的显示隐藏,false隐藏、true显示
selectData: ['15:10','15:15','15:20'],//下拉列表的数据
index: 0,//选择的下拉列表下标
},
// 点击下拉显示框
selectTap() {
this.setData({
selectShow: !this.data.selectShow
});
},
// 点击下拉列表
optionTap(e) {
let Index = e.currentTarget.dataset.index;//获取点击的下拉列表的下标
this.setData({
index: Index,
selectShow: !this.data.selectShow
});
}
转载于
https://www.jianshu.com/p/1d62a193c320