溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

微信小程序單選框如何自定義賦值

發(fā)布時(shí)間:2020-07-21 16:31:55 來源:億速云 閱讀:316 作者:小豬 欄目:web開發(fā)

這篇文章主要為大家展示了微信小程序單選框如何自定義賦值,內(nèi)容簡而易懂,希望大家可以學(xué)習(xí)一下,學(xué)習(xí)完之后肯定會(huì)有收獲的,下面讓小編帶大家一起來看看吧。

知識(shí)點(diǎn):

1、理解wx:if作用
2、理解三元運(yùn)算符的使用
3、利用偽元素after/before自定義內(nèi)容
4、定時(shí)器setTimeout的使用

照例先上代碼

wxml部分:

<view class='input-list'>
 <view class='list-l'>預(yù)計(jì)到店</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屬性指定需要執(zhí)行的動(dòng)畫-->
<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 &#63; '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 &#63; '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 &#63; '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 &#63; '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) {
 /* 動(dòng)畫部分 */
 // 第1步:創(chuàng)建動(dòng)畫實(shí)例 
 var animation = wx.createAnimation({
  duration: 200, //動(dòng)畫時(shí)長 
  timingFunction: "linear", //線性 
  delay: 0 //0則不延遲 
 });

 // 第2步:這個(gè)動(dòng)畫實(shí)例賦給當(dāng)前的動(dòng)畫實(shí)例 
 this.animation = animation;

 // 第3步:執(zhí)行第一組動(dòng)畫 
 animation.opacity(0).translateY(500).step();

 // 第4步:導(dǎo)出動(dòng)畫對(duì)象賦給數(shù)據(jù)對(duì)象儲(chǔ)存 
 this.setData({
  animationData: animation.export()
 })

 // 第5步:設(shè)置定時(shí)器到指定時(shí)候后,執(zhí)行第二組動(dòng)畫 
 setTimeout(function () {
  // 執(zhí)行第二組動(dòng)畫 
  animation.opacity(1).translateY(0).step();
  // 給數(shù)據(jù)對(duì)象儲(chǔ)存的第一組動(dòng)畫,更替為執(zhí)行完第二組動(dòng)畫的動(dòng)畫對(duì)象 
  this.setData({
  animationData: animation
  })

  //關(guān)閉 
  if (currentStatu == "close") {
  this.setData(
   {
   showModalStatus: false
   }
  );
  }
 }.bind(this), 200)

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

 chooseChecked: function (e) {
 console.log(e) //打印出來你會(huì)了解所設(shè)定參數(shù)的意義
 this.setData({
  _num: e.currentTarget.dataset.num,
  item: e.currentTarget.dataset.txt,
 })

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


 /**
 * 生命周期函數(shù)--監(jiān)聽頁面加載
 */
 onLoad: function (options) {

 },

 /**
 * 生命周期函數(shù)--監(jiān)聽頁面初次渲染完成
 */
 onReady: function () {

 },

 /**
 * 生命周期函數(shù)--監(jiān)聽頁面顯示
 */
 onShow: function () {

 },

 /**
 * 生命周期函數(shù)--監(jiān)聽頁面隱藏
 */
 onHide: function () {

 },

 /**
 * 生命周期函數(shù)--監(jiān)聽頁面卸載
 */
 onUnload: function () {

 },

 /**
 * 頁面相關(guān)事件處理函數(shù)--監(jiān)聽用戶下拉動(dòng)作
 */
 onPullDownRefresh: function () {

 },

 /**
 * 頁面上拉觸底事件的處理函數(shù)
 */
 onReachBottom: function () {

 },

 /**
 * 用戶點(diǎn)擊右上角分享
 */
 onShareAppMessage: function () {

 }
})

這是單選效果,復(fù)選效果 獲取其index(如wxml中屬性設(shè)定為 data-selectIndex=''{{index}}'' , 在js方法中打印出來對(duì)象的json數(shù)組,利用三元運(yùn)算添加class屬性完成),點(diǎn)擊它自身時(shí),改變其狀態(tài),最后setData已改變屬性的json數(shù)組。

以上就是關(guān)于微信小程序單選框如何自定義賦值的內(nèi)容,如果你們有學(xué)習(xí)到知識(shí)或者技能,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI