您好,登錄后才能下訂單哦!
這篇文章主要介紹“微信小程序怎么自定義滾動選擇器”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“微信小程序怎么自定義滾動選擇器”文章能幫助大家解決問題。
js:
// pages/xuanzeqi/xuanzeqi.js Page({ /** * 頁面的初始數據 */ data: { list: ['0分', '1分', '2分', '3分', '4分', '5分', '6分', '7分', '8分', '9分', '10分', '11分', '12分', '13分', '14分', '15分', '16分', '17分', '18分', '19分', '20分', '21分', '22分', '23分', '24分', '25分', '26分', '27分', '28分', '29分', '30分', '31分', '32分', '33分', '34分', '35分', '36分', '37分', '38分', '39分', '40分', '41分', '42分', '43分', '44分', '45分', '46分', '47分', '48分', '49分', '50分', '51分', '52分', '53分', '54分', '55分', '56分', '57分', '58分', '59分'], box_height: 0,//選擇器的高度(非控制項,自動計算),單位px picker:{ //選擇器的控制變量 box_width: 200,//選擇器寬度,單位px choose_item_height: 30,//選擇器中每一項選項的高度,單位px choose_item_font_size:25,//選擇器中每一項選項字體大小,單位px scroll_animation: true,//是否使用動畫過渡 choose_item_number: 13,//選擇器內選項的個數(要求為單數) bgColor:'#01356f',//選擇器的背景顏色 choose_item_font_color:'white',//選擇器選項的字體顏色 }, mid_item:0, scroll_into_view:'item_0',//滾動到選項的id item_height_list:[],//存儲每一項距離頂端的y軸數據 picker_value:null,//選擇器的值 opacity_list:[],//透明階梯 cover_list: [],//遮蓋層屬性列表 touchY: -1, scrollTop:0, }, touchMove: function (e) { let that = this let touY = e.touches[0].pageY; if(that.data.touchY == -1){ that.data.touchY = touY } else{ let cha = that.data.touchY - touY that.setData({ scrollTop: that.data.scrollTop + cha }) that.data.touchY = touY } if (that.coverEndTimer) { clearTimeout(that.coverEndTimer); that.coverEndTimer = null; } that.coverEndTimer = setTimeout(function () { that.data.touchY = -1 }, 200); }, //監聽選擇器滾動事件 bindscrollevent:function(e){ let that = this // that.flashOpacity(e.detail.scrollTop) console.log(e) if (that.scrollEndTimer) { clearTimeout(that.scrollEndTimer); that.scrollEndTimer = null; } that.scrollEndTimer = setTimeout(function () { console.log("滑動結束"); // that.flashOpacity(e.detail.scrollTop) that.itemToMid(e.detail.scrollTop) that.data.scrollTop = e.detail.scrollTop }, 200); }, //更新透明度 flashOpacity:function(e){ let that = this that.setData({ item_height_list: that.data.item_height_list }) for (let i in that.data.item_height_list) { if (that.data.item_height_list[i].bottom > e && that.data.item_height_list[i].top >= e) { for(let j = 0; j < that.data.opacity_list.length; j++){ if(i - (j + 1) >= 0){ that.data.item_height_list[i - (j + 1)].opacity = that.data.opacity_list[j] } let a = parseInt(i) if(a + (j + 1) < that.data.list.length){ that.data.item_height_list[a + (j + 1)].opacity = that.data.opacity_list[j] } } let a = parseInt(i) for (let j in that.data.item_height_list) { if (!(j >= a - that.data.opacity_list.length && j <= a + that.data.opacity_list.length)){ that.data.item_height_list[j].opacity = 0 } } that.setData({ item_height_list: that.data.item_height_list }) break; } } }, //根據滾動距離滾動到選項中間 itemToMid:function(e){ let that = this console.log("執行了",e) for (let i in that.data.item_height_list) { if (that.data.item_height_list[i].bottom > e && that.data.item_height_list[i].top <= e) { console.log(that.data.item_height_list[i].bottom, that.data.item_height_list[i].top) if (i < that.data.mid_item - 1) { that.setData({ scroll_into_view: 'cushion_top_' + i }) } else { console.log(parseInt(i) - that.data.mid_item + 1) that.setData({ scroll_into_view: 'item_' + (parseInt(i) - that.data.mid_item + 1) }) } that.setData({ picker_value : that.data.list[i] }) break; } } }, //初始化 init:function(e){ let that = this //先計算該選擇器的高度(根據每項高度和項目個數計算)單位px //如果選擇器個數填寫為雙數,則強制-1變為單數 if (that.data.picker.choose_item_number % 2 == 0){ that.setData({ ['picker.choose_item_number'] : that.data.picker.choose_item_number - 1 }) } //通過乘積計算選擇器高度 that.setData({ box_height : that.data.picker.choose_item_number * that.data.picker.choose_item_height }) //計算選擇器中間項應該是第幾項 that.setData({ mid_item : (that.data.picker.choose_item_number + 1) / 2 }) //初始化遮蓋層透明階梯透明度從(0.1到0.9) let unit = Math.round(80 / (that.data.mid_item - 1)) /**階梯單元 */ for(let i = 0; i < that.data.mid_item - 1; i++){ that.data.opacity_list.push((80 - i * unit) / 100) } that.setData({ opacity_list: that.data.opacity_list }) //初始化遮蓋層列表 for(let i = 0; i < that.data.opacity_list.length; i++){ let row = {opacity: that.data.opacity_list[i]} that.data.cover_list.push(row) } that.data.cover_list.push({ opacity: 0 }) for(let i = 0; i < that.data.opacity_list.length; i++){ let row = { opacity: that.data.opacity_list[that.data.opacity_list.length - 1 - i] } that.data.cover_list.push(row) } that.setData({ cover_list: that.data.cover_list }) //初始化選擇器中每一項高度 //初始化選項透明度,用于突出選中選項 for(let i in that.data.list){ let row = { top: 0, bottom: 0}; // let topBase = (that.data.mid_item - 1) * that.data.picker.choose_item_height//頂端空白區域占用大小 let topBase = 0 row.top = topBase + i * that.data.picker.choose_item_height if(i == that.data.list.length - 1){ row.bottom = topBase + (parseInt(i) + 1) * that.data.picker.choose_item_height + 100 }else{ row.bottom = topBase + (parseInt(i) + 1) * that.data.picker.choose_item_height } that.data.item_height_list.push(row) that.setData({ item_height_list: that.data.item_height_list }) } }, /** * 生命周期函數--監聽頁面加載 */ onLoad: function (options) { let that = this that.init(); }, /** * 生命周期函數--監聽頁面初次渲染完成 */ onReady: function () { }, /** * 生命周期函數--監聽頁面顯示 */ onShow: function () { }, /** * 生命周期函數--監聽頁面隱藏 */ onHide: function () { }, /** * 生命周期函數--監聽頁面卸載 */ onUnload: function () { }, /** * 頁面相關事件處理函數--監聽用戶下拉動作 */ onPullDownRefresh: function () { }, /** * 頁面上拉觸底事件的處理函數 */ onReachBottom: function () { }, /** * 用戶點擊右上角分享 */ onShareAppMessage: function () { } })
WXML:
<view class="box" > <scroll-view class="scroll_box" scroll-y="{{true}}" scroll-with-animation="{{picker.scroll_animation}}" bindscroll="bindscrollevent" scroll-into-view="{{scroll_into_view}}" scrollTop='{{scrollTop}}'> <view wx:for="{{mid_item - 1}}" class="cushion" id="cushion_top_{{index}}">·</view> <view wx:for="{{list}}" class="choose_item" id="item_{{index}}">{{item}}</view> <view wx:for="{{mid_item - 1}}" class="cushion" id="cushion_bottom_{{index}}">·</view> </scroll-view> <!-- 透明度遮蓋 --> <view bindtouchmove="touchMove" > <view wx:for='{{cover_list}}' ></view> </view> </view> <view>{{picker_value}}</view>
wxss:
.box{ } .scroll_box{ height: 100%; width: 100%; } .choose_item{ width: 100%; text-align: center; } .cushion{ width: 100%; text-align: center; } .zhegai{ height: 30px; width: 100%; position: absolute; top: 0; left: 0; background: #01356f; opacity: 0.9 }
效果圖
需要修改選擇器直接修改data中picker內屬性即可。選項逐漸變淺的效果比較讓人頭疼,最初的做法是對list內每個選項都加一個屬性代表該選項的透明度,每次滑動都會修改顯示的選項透明度,但是實際使用起來發現給選項賦值透明度的過程非常明顯,導致體驗不好。最后采用了現在這種方法,在文字上加了一排遮蓋的view,這些view有不同的透明度從而展示出來漸變的效果,省去了每次滑動都要給選項修改透明度的煩惱。
關于“微信小程序怎么自定義滾動選擇器”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注億速云行業資訊頻道,小編每天都會為大家更新不同的知識點。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。