微信小程序自定義select下拉選項框的方法
本文實例為大家分享了微信小程序自定義select下拉選項框的具體代碼,供大家參考,具體內(nèi)容如下
第一步:創(chuàng)建組件所需的文件
第二步:開始配置組件
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: "請選擇", //初始內(nèi)容 ? ? animationData: {} //右邊箭頭的動畫 ? }, ? /** ? ?* 組件的方法列表 ? ?*/ ? methods: { ? ? //option的顯示與否 ? ? selectToggle: function () { ? ? ? var nowShow = this.data.selectShow; //獲取當(dāng)前option顯示的狀態(tài) ? ? ? //創(chuàng)建動畫 ? ? ? 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ù)是引入組件的頁面?zhèn)鬟^來的,所以這里獲取數(shù)據(jù)只有通過this.properties ? ? ? var nowIdx = e.target.dataset.index; //當(dāng)前點擊的索引 ? ? ? var nowText = nowData[nowIdx].text; //當(dāng)前點擊的內(nèi)容 ? ? ? //子組件觸發(fā)事件 ? ? ? var nowDate = { ? ? ? ? id: nowIdx, ? ? ? ? text: nowText ? ? ? } ? ? ? this.triggerEvent('myget', nowDate) ? ? ? //再次執(zhí)行動畫,注意這里一定,一定,一定是this.animation來使用動畫 ? ? ? this.animation.rotate(0).step(); ? ? ? this.setData({ ? ? ? ? selectShow: false, ? ? ? ? nowText: nowText, ? ? ? ? animationData: this.animation.export() ? ? ? }) ? ? } ? } })
第四步:引入組件,傳入組件所需數(shù)據(jù)
1、引入組件的頁面的json文件中配置
{ ? "usingComponents": { ? ? "Select": "../../components/select/select" ? }, ? "navigationBarTitleText": "" }
2、引入組件的頁面的wxml文件中配置
bind:myget=‘getDate’ 對組件的事件進行監(jiān)聽
<Select prop-array='{{selectArray}}' bind:myget='getDate'></Select>
3、引入組件的頁面的js文件中配置
data:{ ?? ?selectArray: [ ?? ??? ?{ ?? ??? ? ? ?"id": "01", ?? ??? ? ? ?"text": "停車A區(qū)" ?? ??? ?},? ?? ??? ?{ ?? ??? ? ? ?"id": "02", ?? ??? ? ? ?"text": "停車B區(qū)" ?? ??? ?} ?? ?] ?} getDate:function(e){ ? ? console.log(e.detail) }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
momentjs獲取上周、上月、前三個月的起始和結(jié)束時間(附完整代碼)
這篇文章主要給大家介紹了關(guān)于momentjs獲取上周、上月、前三個月的起始和結(jié)束時間的相關(guān)資料,在需要你進行日期處理的地方,必然少不了moment.js的使用,需要的朋友可以參考下2023-07-07jacascript DOM節(jié)點——元素節(jié)點、屬性節(jié)點、文本節(jié)點
這篇文章主要介紹了jacascript DOM節(jié)點——元素節(jié)點、屬性節(jié)點、文本節(jié)點,需要的朋友可以參考下2017-04-04js/jquery遍歷對象和數(shù)組的方法分析【forEach,map與each方法】
這篇文章主要介紹了js/jquery遍歷對象和數(shù)組的方法,結(jié)合實例形式分析了數(shù)組遍歷的forEach,map與each方法常見使用技巧,需要的朋友可以參考下2019-02-02學(xué)習(xí)JavaScript設(shè)計模式之策略模式
這篇文章主要為大家介紹了JavaScript設(shè)計模式中的策略模式,對JavaScript設(shè)計模式感興趣的小伙伴們可以參考一下2016-01-01javascript實現(xiàn)iframe框架延時加載的方法
這篇文章主要介紹了javascript實現(xiàn)iframe框架延時加載的方法,可基于setTimeout實現(xiàn)這一功能,是非常實用的技巧,需要的朋友可以參考下2014-10-10