微信小程序單選框自定義賦值
這里我們應(yīng)用之前一篇寫(xiě)過(guò)的彈框效果,單選框我們運(yùn)用偽元素自定義,不使用圖片, 這個(gè)例子可以運(yùn)用到很多情況;
知識(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)畫(huà)-->
<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) {
/* 動(dòng)畫(huà)部分 */
// 第1步:創(chuàng)建動(dòng)畫(huà)實(shí)例
var animation = wx.createAnimation({
duration: 200, //動(dòng)畫(huà)時(shí)長(zhǎng)
timingFunction: "linear", //線性
delay: 0 //0則不延遲
});
// 第2步:這個(gè)動(dòng)畫(huà)實(shí)例賦給當(dāng)前的動(dòng)畫(huà)實(shí)例
this.animation = animation;
// 第3步:執(zhí)行第一組動(dòng)畫(huà)
animation.opacity(0).translateY(500).step();
// 第4步:導(dǎo)出動(dòng)畫(huà)對(duì)象賦給數(shù)據(jù)對(duì)象儲(chǔ)存
this.setData({
animationData: animation.export()
})
// 第5步:設(shè)置定時(shí)器到指定時(shí)候后,執(zhí)行第二組動(dòng)畫(huà)
setTimeout(function () {
// 執(zhí)行第二組動(dòng)畫(huà)
animation.opacity(1).translateY(0).step();
// 給數(shù)據(jù)對(duì)象儲(chǔ)存的第一組動(dòng)畫(huà),更替為執(zhí)行完第二組動(dòng)畫(huà)的動(dòng)畫(huà)對(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) //打印出來(lái)你會(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)聽(tīng)頁(yè)面加載
*/
onLoad: function (options) {
},
/**
* 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面顯示
*/
onShow: function () {
},
/**
* 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面隱藏
*/
onHide: function () {
},
/**
* 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面卸載
*/
onUnload: function () {
},
/**
* 頁(yè)面相關(guān)事件處理函數(shù)--監(jiān)聽(tīng)用戶下拉動(dòng)作
*/
onPullDownRefresh: function () {
},
/**
* 頁(yè)面上拉觸底事件的處理函數(shù)
*/
onReachBottom: function () {
},
/**
* 用戶點(diǎn)擊右上角分享
*/
onShareAppMessage: function () {
}
})
這是單選效果,復(fù)選效果 獲取其index(如wxml中屬性設(shè)定為 data-selectIndex=''{{index}}'' , 在js方法中打印出來(lái)對(duì)象的json數(shù)組,利用三元運(yùn)算添加class屬性完成),點(diǎn)擊它自身時(shí),改變其狀態(tài),最后setData已改變屬性的json數(shù)組。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript 保存數(shù)組到Cookie的代碼
大部分的瀏覽器一個(gè)網(wǎng)站只支持保存20個(gè)Cookie,超過(guò)20個(gè)Cookie,舊的Cookie會(huì)被最新的Cookie代替。那么如果要有超過(guò)20個(gè)Cookie要保存只能將Cookie存為數(shù)組然后保存到Cookie。2010-04-04
JavaScript實(shí)現(xiàn)背景自動(dòng)切換小案例
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)背景自動(dòng)切換小案例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09
微信小程序?qū)崿F(xiàn)之手勢(shì)鎖功能實(shí)例代碼
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)之手勢(shì)鎖功能的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07
js通過(guò)Date對(duì)象實(shí)現(xiàn)倒計(jì)時(shí)動(dòng)畫(huà)效果
這篇文章主要為大家詳細(xì)介紹了js通過(guò)Date對(duì)象實(shí)現(xiàn)倒計(jì)時(shí)動(dòng)畫(huà)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
javascript實(shí)現(xiàn)固定側(cè)邊欄
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)固定側(cè)邊欄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-02-02
js面向?qū)ο蠓庋b級(jí)聯(lián)下拉菜單列表的實(shí)現(xiàn)步驟
這篇文章主要介紹了js面向?qū)ο蠓庋b級(jí)聯(lián)下拉菜單列表的實(shí)現(xiàn)步驟,幫助大家更好的理解和使用JavaScript,感興趣的朋友可以了解下2021-02-02
JS實(shí)現(xiàn)移動(dòng)端雙指縮放和旋轉(zhuǎn)方法
這篇文章主要介紹了JS實(shí)現(xiàn)移動(dòng)端雙指縮放和旋轉(zhuǎn)方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
JavaScript進(jìn)制轉(zhuǎn)換實(shí)現(xiàn)方法解析
這篇文章主要介紹了JavaScript進(jìn)制轉(zhuǎn)換實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了JavaScript進(jìn)制轉(zhuǎn)換中十進(jìn)制與其他進(jìn)制轉(zhuǎn)換、以及隨機(jī)顏色生成相關(guān)操作技巧,需要的朋友可以參考下2020-01-01

