vue 實現(xiàn) ios 原生picker 效果及實現(xiàn)思路解析
以前最早實現(xiàn)了一個類似的時間選擇插件,但是適用范圍太窄,索性最近要把這個實現(xiàn)方式發(fā)布出來,就重寫了一個高復用的vue組件。
支持安卓4.0以上,safari 7以上
滾輪部分主要dom結構
<template data-filtered="filtered"> <div class="pd-select-item"> <div class="pd-select-line"></div> <ul class="pd-select-list"> <li class="pd-select-list-item">1</li> </ul> <ul class="pd-select-wheel"> <li class="pd-select-wheel-item">1</li> </ul> </div> </template> props props: { data: { type: Array, required: true }, type: { type: String, default: 'cycle' }, value: {} }
設置css樣式 使其垂直居中
.pd-select-line, .pd-select-list, .pd-select-wheel { position: absolute; left: 0; right: 0; top: 50%; transform: translateY(-50%); } .pd-select-list { overflow: hidden; }
滾輪3d樣式設置
/* 滾輪盒子 */ .pd-select-wheel { transform-style: preserve-3d; height: 30px; } /* 滾輪單項 */ .pd-select-wheel-item { white-space: nowrap; text-overflow: ellipsis; backface-visibility: hidden; position: absolute; top: 0px; width: 100%; overflow: hidden; }
主要注意2個屬性 transform-style: preserve-3d; backface-visibility: hidden;
第一個是3d布局,讓界面3D化,第二個是讓滾輪背后自動隱藏(上圖紅色部分,背面的dom節(jié)點 會自動隱藏)
如何實現(xiàn)3D 滾輪
盒子主要這句css transform: rotate3d(1, 0, 0, x deg);
item主要運用這句css transform: rotate3d(1, 0, 0, xdeg) translate3d(0px, 0px, [x]px);
上面2張圖展示了translate3d(0px, 0px, [x]px);這句話的效果 [x]就是圓的半徑
從上面的圖可以看見,我們只需旋轉每個dom自身,然后利用translate3d(0px, 0px, [x]px);把每個dom擴展開
就形成了圓環(huán).α就是每個dom自身旋轉的角度,因為這里只用了0到180°,所以用了個盒子在裝這些dom
行高 和角度計算
已知兩邊和夾角 算第三邊長度 ~=34px
http://tool.520101.com/calculator/sanjiaoxingjiaodu/
無限滾輪實現(xiàn)
/* 滾輪展示大小限定 */ spin: {start: 0, end: 9, branch: 9} /* 獲取spin 數(shù)據(jù) */ getSpinData (index) { index = index % this.listData.length return this.listData[index >= 0 ? index : index + this.listData.length] } /* 模運算 獲取數(shù)組有的索引 這樣就構成 圓環(huán)了 */
touchend做特殊處理
在touchend 里設置setCSS類型 把滾動數(shù)據(jù)取整,這樣停止的時候就是
一格一格的準確轉動到位
// other code .... /* 計算touchEnd移動的整數(shù)距離 */ let endMove = margin let endDeg = Math.round(updateDeg / deg) * deg if (type === 'end') { this.setListTransform(endMove, margin) this.setWheelDeg(endDeg) } else { this.setListTransform(updateMove, margin) this.setWheelDeg(updateDeg) } // other code .... 慣性緩動 // other code .... setWheelDeg (updateDeg, type, time = 1000) { if (type === 'end') { this.$refs.wheel.style.webkitTransition = `transform ${time}ms cubic-bezier(0.19, 1, 0.22, 1)` this.$refs.wheel.style.webkitTransform = `rotate3d(1, 0, 0, ${updateDeg}deg)` } else { this.$refs.wheel.style.webkitTransition = '' this.$refs.wheel.style.webkitTransform = `rotate3d(1, 0, 0, ${updateDeg}deg)` } } setListTransform (translateY = 0, marginTop = 0, type, time = 1000) { if (type === 'end') { this.$refs.list.style.webkitTransition = `transform ${time}ms cubic-bezier(0.19, 1, 0.22, 1)` this.$refs.list.style.webkitTransform = `translateY(${translateY - this.spin.branch * 34}px)` this.$refs.list.style.marginTop = `${-marginTop}px` this.$refs.list.setAttribute('scroll', translateY) console.log('end') } else { this.$refs.list.style.webkitTransition = '' this.$refs.list.style.webkitTransform = `translateY(${translateY - this.spin.branch * 34}px)` this.$refs.list.style.marginTop = `${-marginTop}px` this.$refs.list.setAttribute('scroll', translateY) } } // other code ....
獲取當前選中值
/* 在設置完css后獲取值 */ setStyle (move, type, time) { // ...other code /* 設置$emit 延遲 */ setTimeout(() => this.getPickValue(endMove), 1000) // ...other code } /* 獲取選中值 */ getPickValue (move) { let index = Math.abs(move / 34) let pickValue = this.getSpinData(index) this.$emit('input', pickValue) }
初始化設置
mounted () { /* 事件綁定 */ this.$el.addEventListener('touchstart', this.itemTouchStart) this.$el.addEventListener('touchmove', this.itemTouchMove) this.$el.addEventListener('touchend', this.itemTouchEnd) /* 初始化狀態(tài) */ let index = this.listData.indexOf(this.value) if (index === -1) { console.warn('當前初始值不存在,請檢查后listData范圍?。?) this.setListTransform() this.getPickValue(0) } else { let move = index * 34 /* 因為往上滑動所以是負 */ this.setStyle(-move) this.setListTransform(-move, -move) }
當展示為非無限滾輪的時
這里我們很好判斷,就是滾動的距離不能超過原始數(shù)的數(shù)組長度*34,且不能小于0(實際代碼中涉及方向)
/* 根據(jù)滾輪類型 line or cycle 判斷 updateMove最大距離 */ if (this.type === 'line') { if (updateMove > 0) { updateMove = 0 } if (updateMove < -(this.listData.length - 1) * singleHeight) { updateMove = -(this.listData.length - 1) * singleHeight } } /* 根據(jù)type 控制滾輪顯示效果 */ setHidden (index) { if (this.type === 'line') { return index < 0 || index > this.listData.length - 1 } else { return false } },
dom結構也增加了對應的響應
<div class="pd-select-item"> <div class="pd-select-line"></div> <div class="pd-select-list"> <ul class="pd-select-ul" ref="list"> <li class="pd-select-list-item" v-for="el,index in renderData " :class="{'hidden':setHidden(el.index)}" :key="index">{{el.value}}</li> </ul> </div> <ul class="pd-select-wheel" ref="wheel"> <li class="pd-select-wheel-item" :class="{'hidden':setHidden(el.index)}" :style="setWheelItemDeg(el.index)" :index="el.index" v-for="el,index in renderData " :key="index">{{el.value}}</li> </ul> </div>
總結
以上所述是小編給大家介紹的vue 實現(xiàn) ios 原生picker 效果及思路解析,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- vue學習之mintui picker選擇器實現(xiàn)省市二級聯(lián)動示例
- vue mint-ui學習筆記之picker的使用
- vue2.0 與 bootstrap datetimepicker的結合使用實例
- 基于Vue實現(xiàn)timepicker
- Vue2 配置 Axios api 接口調用文件的方法
- vue 里面使用axios 和封裝的示例代碼
- Vue axios 中提交表單數(shù)據(jù)(含上傳文件)
- VUE axios發(fā)送跨域請求需要注意的問題
- 詳解Vue.js 2.0 如何使用axios
- Vue.js實戰(zhàn)之使用Vuex + axios發(fā)送請求詳解
相關文章
詳解在Vue.js編寫更好的v-for循環(huán)的6種技巧
這篇文章主要介紹了詳解在Vue.js編寫更好的v-for循環(huán)的6種技巧,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-04-04公共Hooks封裝文件下載useDownloadFile實例詳解
這篇文章主要為大家介紹了公共Hooks封裝文件下載useDownloadFile實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11使用vue實現(xiàn)滑動滾動條來加載數(shù)據(jù)
在vuejs中,我們經(jīng)常使用axios來請求數(shù)據(jù),但是有時候,我們請求的數(shù)據(jù)量很大,那么我們如何實現(xiàn)滑動滾動條來加載數(shù)據(jù)呢,接下來小編就給大家介紹一下在vuejs中如何實現(xiàn)滑動滾動條來動態(tài)加載數(shù)據(jù),需要的朋友可以參考下2023-10-10