网络编程
位置:首页>> 网络编程>> JavaScript>> 微信小程序单选框自定义赋值

微信小程序单选框自定义赋值

作者:saluteNic_LU  发布时间:2024-04-18 09:49:50 

标签:微信小程序,单选框

这里我们应用之前一篇写过的弹框效果,单选框我们运用伪元素自定义,不使用图片, 这个例子可以运用到很多情况;

知识点:

1、理解wx:if作用
2、理解三元运算符的使用
3、利用伪元素after/before自定义内容
4、定时器setTimeout的使用

照例先上代码

wxml部分:


<view class='input-list'>
<view class='list-l'>预计到店</view>
<view class='list-r' bindtap='powerDrawer' data-statu="open">
<view class='arriveTime'>{{item}}</view>
</view>
</view>

<view class="drawer_screen" wx:if="{{showModalStatus}}" bindtap="powerDrawer" data-statu="close" catchtouchmove="preventTouchMove"></view>
<!--content-->
<!--使用animation属性指定需要执行的动画-->
<view animation="{{animationData}}" class="drawer_box" wx:if="{{showModalStatus}}" catchtouchmove="preventTouchMove">
<view class='modalBox'>
<view class='choosePush grey9'>房间整晚保留,14:00之前到店可能需要等房</view>
<view class="{{_num == 0 ? 'choosePush t' : 'choosePush'}}" bindtap='chooseChecked' data-num='0' data-txt='18:00以前'>
 18:00以前
 <view class='checkbox' wx:if="{{_num==0}}"></view>
</view>
<view class="{{_num == 1 ? 'choosePush t' : 'choosePush'}}" bindtap='chooseChecked' data-num='1' data-txt='20:00以前'>
 20:00以前
 <view class='checkbox' wx:if="{{_num==1}}"></view>
</view>
<view class="{{_num == 2 ? 'choosePush t' : 'choosePush'}}" bindtap='chooseChecked' data-num='2' data-txt='23:59以前'>
 23:59以前
 <view class='checkbox' wx:if="{{_num==2}}"></view>
</view>
<view class="{{_num == 3 ? 'choosePush t' : 'choosePush'}}" bindtap='chooseChecked' data-num='3' data-txt='次日凌晨6:00之前'>
 次日凌晨6:00之前
 <view class='checkbox' wx:if="{{_num==3}}"></view>
</view>
</view>
</view>

wxss部分:


.input-list {
padding: 40rpx;
border-bottom: 1px solid #eee;
display: flex;
position: relative;
}

.list-l {
flex: 2;
line-height: 50rpx;
}

.list-r {
flex: 5;
}

.arriveTime {
line-height: 50rpx;
}

.drawer_screen {
width: 100%;
height: 100%;
position: fixed;
top: 0;
left: 0;
z-index: 1000;
background: #000;
opacity: 0.5;
overflow: hidden;
}

/*content*/
.drawer_box {
width: 100vw;
height: auto;
padding: 0;
overflow: hidden;
position: fixed;
bottom: 0;
left: 0;
z-index: 1001;
background: #fff;
}

.modalBox {
padding: 0 40rpx;
font-size: 30rpx;
}

.choosePush {
text-align: center;
padding: 40rpx 0 ;
border-bottom: 1px solid #eee;
position: relative
}

.choosePush.t {
color: #1da0ee;
}

.checkbox {
position: absolute;
right: 0;
top: 38rpx;
height: 40rpx;
width: 40rpx;
border: 1px solid #1da0ee;
}

.checkbox::after {
content: '';
position: absolute;
height: 15rpx;
width: 25rpx;
border-left: 1px solid #1da0ee;
border-bottom: 1px solid #1da0ee;
transform: rotate(-45deg);
top: 6rpx;
right: 6rpx;
}

js部分:


Page({

data: {
showModalStatus: false,
_num: null,
haveChoosed: false,
sta: null,
item: '18:00',
},

preventTouchMove() { },

powerDrawer: function (e) {
console.log(e)
var currentStatu = e.currentTarget.dataset.statu;
this.util(currentStatu)
},
util: function (currentStatu) {
/* 动画部分 */
// 第1步:创建动画实例
var animation = wx.createAnimation({
 duration: 200, //动画时长
 timingFunction: "linear", //线性
 delay: 0 //0则不延迟
});

// 第2步:这个动画实例赋给当前的动画实例
this.animation = animation;

// 第3步:执行第一组动画
animation.opacity(0).translateY(500).step();

// 第4步:导出动画对象赋给数据对象储存
this.setData({
 animationData: animation.export()
})

// 第5步:设置定时器到指定时候后,执行第二组动画
setTimeout(function () {
 // 执行第二组动画
 animation.opacity(1).translateY(0).step();
 // 给数据对象储存的第一组动画,更替为执行完第二组动画的动画对象
 this.setData({
 animationData: animation
 })

//关闭
 if (currentStatu == "close") {
 this.setData(
  {
  showModalStatus: false
  }
 );
 }
}.bind(this), 200)

// 显示
if (currentStatu == "open") {
 this.setData(
 {
  showModalStatus: true
 }
 );
}
},

chooseChecked: function (e) {
console.log(e) //打印出来你会了解所设定参数的意义
this.setData({
 _num: e.currentTarget.dataset.num,
 item: e.currentTarget.dataset.txt,
})

setTimeout(function () {
 this.setData(
 {
  showModalStatus: false
 }
 );
}.bind(this), 2000)
},

/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {

},

/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {

},

/**
* 生命周期函数--监听页面显示
*/
onShow: function () {

},

/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {

},

/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {

},

/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {

},

/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {

},

/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {

}
})

这是单选效果,复选效果 获取其index(如wxml中属性设定为 data-selectIndex=''{{index}}'' , 在js方法中打印出来对象的json数组,利用三元运算添加class属性完成),点击它自身时,改变其状态,最后setData已改变属性的json数组。

来源:https://blog.csdn.net/weixin_42794596/article/details/82980359

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com