uniapp手寫滾動選擇器的完整代碼(時間選擇器)
沒有符合項目要求的選擇器 就手寫了一個
效果展示
實現(xiàn)一個時間選擇器的功能,可以選擇小時和分鐘:
HTML/Template部分:
<picker-view class="sleepPage-time-picker" :indicator-style="indicatorStyle" :value="timeValue" @change="handleTimeChange" > <!-- 第一列:小時選擇 --> <picker-view-column> <view v-for="(hour, index) in hours" :key="index" :class="[ 'sleepPage-time-picker_item', { selected: timeValue[0] === index }, ]" > {{ hour }} <span class="sleepPage-time-picker_item-span" v-if="timeValue[0] === index" >時</span > </view> </picker-view-column> <!-- 第二列:分鐘選擇 --> <picker-view-column> <view v-for="(minute, index) in minutes" :key="index" :class="[ 'sleepPage-time-picker_item', { selected: timeValue[1] === index }, ]" > {{ minute }} <span class="sleepPage-time-picker_item-span" v-if="timeValue[1] === index" >分</span > </view> </picker-view-column> </picker-view>
<picker-view>
是一個小程序中的組件,用于實現(xiàn)滾動選擇器效果。:indicator-style
和:value
是組件的屬性綁定,分別用來設(shè)置選擇器的樣式和當前選擇的值。@change
是一個事件監(jiān)聽器,當選擇器的值發(fā)生改變時會觸發(fā)handleTimeChange
方法。
JavaScript部分:
data() { return { timeValue: [0, 0], // 默認選中的時間值,[小時索引, 分鐘索引] indicatorStyle: "height: 30px;background: rgba(237, 252, 249, 1);z-index: 0;", hours: [...Array(24).keys()].map((n) => n.toString().padStart(2, "0")), // 生成小時選項數(shù)組 minutes: [...Array(60).keys()].map((n) => n.toString().padStart(2, "0")), // 生成分鐘選項數(shù)組 }; }, methods: { handleTimeChange(e) { this.timeValue = e.detail.value; // 更新選擇的時間值 // 處理選擇后的邏輯,例如更新界面顯示的時間 console.log( "Selected Time:", this.hours[this.timeValue[0]], ":", this.minutes[this.timeValue[1]] ); }, }
data()
中定義了組件的數(shù)據(jù)狀態(tài),包括timeValue
表示當前選擇的小時和分鐘的索引,hours
和minutes
分別是小時和分鐘的選項數(shù)組。handleTimeChange(e)
方法是一個事件處理器,用來響應(yīng)選擇器數(shù)值改變事件。它更新timeValue
并可以執(zhí)行相應(yīng)的邏輯,例如打印或更新界面上顯示的選擇結(jié)果。
CSS部分:
.sleepPage-time-picker-box { display: flex; margin-bottom: 10px; } .sleepPage-time-picker { height: 90px; /* 設(shè)置選擇器的高度 */ width: 50%; /* 設(shè)置選擇器的寬度 */ margin: 2px; } .selected { color: rgba(40, 184, 129, 1); /* 設(shè)置選中項的文字顏色 */ } .sleepPage-time-picker_item { text-align: center; height: 30px; line-height: 30px; position: relative; } .sleepPage-time-picker_item-span { padding-left: 10px; position: absolute; right: 15px; }
CSS 部分定義了選擇器和其子元素的樣式,包括選擇器的整體布局和每個選項的樣式,以及選中項的特殊樣式。
完整代碼
<picker-view class="sleepPage-time-picker" :indicator-style="indicatorStyle" :value="timeValue" @change="handleTimeChange" > <picker-view-column> <view v-for="(hour, index) in hours" :key="index" :class="[ 'sleepPage-time-picker_item', { selected: timeValue[0] === index }, ]" > {{ hour }} <span class="sleepPage-time-picker_item-span" v-if="timeValue[0] === index" >時</span > </view> </picker-view-column> <picker-view-column> <view v-for="(minute, index) in minutes" :key="index" :class="[ 'sleepPage-time-picker_item', { selected: timeValue[1] === index }, ]" > {{ minute }} <span class="sleepPage-time-picker_item-span" v-if="timeValue[1] === index" >分</span > </view> </picker-view-column> </picker-view> timeValue: [0, 0], indicatorStyle: "height: 30px;background: rgba(237, 252, 249, 1);z-index: 0;", hours: [...Array(24).keys()].map((n) => n.toString().padStart(2, "0")), minutes: [...Array(60).keys()].map((n) => n.toString().padStart(2, "0")), handleTimeChange(e) { this.timeValue = e.detail.value; // 這里可以處理時間選擇后的邏輯,例如更新界面顯示的時間 console.log( "Selected Time:", this.hours[this.timeValue[0]], ":", this.minutes[this.timeValue[1]] ); }, .sleepPage-time-picker-box { display: flex; margin-bottom: 10px; .sleepPage-time-picker { // height: 300px; height: 90px; width: 50%; margin: 2px; } .selected { color: rgba(40, 184, 129, 1); } .sleepPage-time-picker_item { text-align: center; height: 30px; line-height: 30px; position: relative; } .sleepPage-time-picker_item-span { padding-left: 10px; position: absolute; right: 15px; } }
您好,我是肥晨。
歡迎關(guān)注我獲取前端學(xué)習(xí)資源,日常分享技術(shù)變革,生存法則;行業(yè)內(nèi)幕,洞察先機。
到此這篇關(guān)于uniapp手寫滾動選擇器的文章就介紹到這了,更多相關(guān)uniapp滾動選擇器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
js跨瀏覽器實現(xiàn)將字符串轉(zhuǎn)化為xml對象的方法
將字符串轉(zhuǎn)化為xml對象需要注意的是該死的ie多版本的問題,具體實現(xiàn)如下,感興趣的朋友不妨參考下本文,希望對大家有所幫助2013-09-09郁悶!ionic中獲取ng-model綁定的值為undefined如何解決
很是郁悶!ionic中獲取ng-model綁定的值為undefined,如何解決?2016-08-08用Javascript實現(xiàn)Sleep暫停功能代碼
ie和firefox都可以使用,有興趣可以試試2010-09-09Bootstrap Table 雙擊、單擊行獲取該行及全表內(nèi)容
這篇文章主要介紹了Bootstrap Table 雙擊、單擊行獲取該行內(nèi)容及獲取全表的內(nèi)容,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2018-08-08