微信小程序自定義select下拉選項(xiàng)框的方法
本文實(shí)例為大家分享了微信小程序自定義select下拉選項(xiàng)框的具體代碼,供大家參考,具體內(nèi)容如下
第一步:創(chuàng)建組件所需的文件
第二步:開(kāi)始配置組件
select.json
{ ? "component": true, ? "usingComponents": { ? ? "select": "./select" ? } }
第三步:自定義組件樣式及js
select.wxml
<view class='com-selectBox'> ? ? <view class='com-sContent' bindtap='selectToggle'> ? ? ? ? <view class='com-sTxt'>{{nowText}}</view> ? ? ? ? <image src='../../public/image/index/jinru1@2x.png' ?class='com-sImg' ?animation="{{animationData}}"></image> ? ? </view> ? ? <view class='com-sList' wx:if="{{selectShow}}"> ? ? ? ? <view wx:for="{{propArray}}" data-index="{{index}}" wx:key='' class='com-sItem' bindtap='setText'>{{item.text}}</view> ? ? </view> </view>
select.wxss
.com-selectBox{ ? width: 200px; } .com-sContent{ ? border: 1px solid #e2e2e2; ? background: white; ? font-size: 16px; ? position: relative; ? height: 30px; ? line-height: 30px; } .com-sImg{ ? position: absolute; ? right: 10px; ? top: 11px; ? width: 16px; ? height: 9px; ? transition: all .3s ease; } .com-sTxt{ ? overflow: hidden; ? text-overflow: ellipsis; ? white-space: nowrap; ? padding:0 20px 0 6px; ? font-size: 14px; } .com-sList{ ? background: white; ? width: inherit; ? position: absolute; ? border: 1px solid #e2e2e2; ? border-top: none; ? box-sizing: border-box; ? z-index: 3; ? max-height: 120px; ? overflow: auto; } .com-sItem{ ? height: 30px; ? line-height: 30px; ? border-top: 1px solid #e2e2e2; ? padding: 0 6px; ? text-align: left; ? overflow: hidden; ? text-overflow: ellipsis; ? white-space: nowrap; ? font-size: 14px; } .com-sItem:first-child{ ? border-top: none; }
select.js
Component({ ? /** ? ?* 組件的屬性列表 ? ?*/ ? properties: { ? ? propArray: { ? ? ? type: Array, ? ? } ? }, ? /** ? ?* 組件的初始數(shù)據(jù) ? ?*/ ? data: { ? ? selectShow: false, //初始o(jì)ption不顯示 ? ? nowText: "請(qǐng)選擇", //初始內(nèi)容 ? ? animationData: {} //右邊箭頭的動(dòng)畫(huà) ? }, ? /** ? ?* 組件的方法列表 ? ?*/ ? methods: { ? ? //option的顯示與否 ? ? selectToggle: function () { ? ? ? var nowShow = this.data.selectShow; //獲取當(dāng)前option顯示的狀態(tài) ? ? ? //創(chuàng)建動(dòng)畫(huà) ? ? ? var animation = wx.createAnimation({ ? ? ? ? timingFunction: "ease" ? ? ? }) ? ? ? this.animation = animation; ? ? ? if (nowShow) { ? ? ? ? animation.rotate(0).step(); ? ? ? ? this.setData({ ? ? ? ? ? animationData: animation.export() ? ? ? ? }) ? ? ? } else { ? ? ? ? animation.rotate(180).step(); ? ? ? ? this.setData({ ? ? ? ? ? animationData: animation.export() ? ? ? ? }) ? ? ? } ? ? ? this.setData({ ? ? ? ? selectShow: !nowShow ? ? ? }) ? ? }, ? ? //設(shè)置內(nèi)容 ? ? setText: function (e) { ? ? ? var nowData = this.properties.propArray; //當(dāng)前option的數(shù)據(jù)是引入組件的頁(yè)面?zhèn)鬟^(guò)來(lái)的,所以這里獲取數(shù)據(jù)只有通過(guò)this.properties ? ? ? var nowIdx = e.target.dataset.index; //當(dāng)前點(diǎn)擊的索引 ? ? ? var nowText = nowData[nowIdx].text; //當(dāng)前點(diǎn)擊的內(nèi)容 ? ? ? //子組件觸發(fā)事件 ? ? ? var nowDate = { ? ? ? ? id: nowIdx, ? ? ? ? text: nowText ? ? ? } ? ? ? this.triggerEvent('myget', nowDate) ? ? ? //再次執(zhí)行動(dòng)畫(huà),注意這里一定,一定,一定是this.animation來(lái)使用動(dòng)畫(huà) ? ? ? this.animation.rotate(0).step(); ? ? ? this.setData({ ? ? ? ? selectShow: false, ? ? ? ? nowText: nowText, ? ? ? ? animationData: this.animation.export() ? ? ? }) ? ? } ? } })
第四步:引入組件,傳入組件所需數(shù)據(jù)
1、引入組件的頁(yè)面的json文件中配置
{ ? "usingComponents": { ? ? "Select": "../../components/select/select" ? }, ? "navigationBarTitleText": "" }
2、引入組件的頁(yè)面的wxml文件中配置
bind:myget=‘getDate’ 對(duì)組件的事件進(jìn)行監(jiān)聽(tīng)
<Select prop-array='{{selectArray}}' bind:myget='getDate'></Select>
3、引入組件的頁(yè)面的js文件中配置
data:{ ?? ?selectArray: [ ?? ??? ?{ ?? ??? ? ? ?"id": "01", ?? ??? ? ? ?"text": "停車A區(qū)" ?? ??? ?},? ?? ??? ?{ ?? ??? ? ? ?"id": "02", ?? ??? ? ? ?"text": "停車B區(qū)" ?? ??? ?} ?? ?] ?} getDate:function(e){ ? ? console.log(e.detail) }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
momentjs獲取上周、上月、前三個(gè)月的起始和結(jié)束時(shí)間(附完整代碼)
這篇文章主要給大家介紹了關(guān)于momentjs獲取上周、上月、前三個(gè)月的起始和結(jié)束時(shí)間的相關(guān)資料,在需要你進(jìn)行日期處理的地方,必然少不了moment.js的使用,需要的朋友可以參考下2023-07-07jacascript DOM節(jié)點(diǎn)——元素節(jié)點(diǎn)、屬性節(jié)點(diǎn)、文本節(jié)點(diǎn)
這篇文章主要介紹了jacascript DOM節(jié)點(diǎn)——元素節(jié)點(diǎn)、屬性節(jié)點(diǎn)、文本節(jié)點(diǎn),需要的朋友可以參考下2017-04-04js/jquery遍歷對(duì)象和數(shù)組的方法分析【forEach,map與each方法】
這篇文章主要介紹了js/jquery遍歷對(duì)象和數(shù)組的方法,結(jié)合實(shí)例形式分析了數(shù)組遍歷的forEach,map與each方法常見(jiàn)使用技巧,需要的朋友可以參考下2019-02-02學(xué)習(xí)JavaScript設(shè)計(jì)模式之策略模式
這篇文章主要為大家介紹了JavaScript設(shè)計(jì)模式中的策略模式,對(duì)JavaScript設(shè)計(jì)模式感興趣的小伙伴們可以參考一下2016-01-01js給網(wǎng)頁(yè)加上背景音樂(lè)及選擇音效的方法
這篇文章主要介紹了js給網(wǎng)頁(yè)加上背景音樂(lè)及選擇音效的方法,涉及javascript操作音頻的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03uniapp頁(yè)面間傳參的幾種方法實(shí)例總結(jié)
在進(jìn)行頁(yè)面的跳轉(zhuǎn)的時(shí)候,往往需要我們將一些參數(shù)攜帶著傳遞過(guò)去,下面這篇文章主要給大家介紹了關(guān)于uniapp頁(yè)面間傳參的幾種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12javascript實(shí)現(xiàn)iframe框架延時(shí)加載的方法
這篇文章主要介紹了javascript實(shí)現(xiàn)iframe框架延時(shí)加載的方法,可基于setTimeout實(shí)現(xiàn)這一功能,是非常實(shí)用的技巧,需要的朋友可以參考下2014-10-10