js?實現(xiàn)picker?選擇器示例詳解
前言
想必各位做移動端開發(fā)的小伙伴對picker選擇器應(yīng)該不陌生吧。你做微信小程序開發(fā)有自帶的picker組件,做公眾號開發(fā)可以使用weui提供的picker組件。除此之外,市面上開源的picker組件也是層出不窮,拿來即用。但如果叫你自己實現(xiàn)一個,你會如何實現(xiàn)呢?我花了點時間寫了一個簡單的demo,希望能給想自己動手實現(xiàn)一個picker選擇器但又無從下手的小伙伴提供一個思路。
實現(xiàn)
CSS
* { margin: 0; padding: 0; } .btn { height: 32px; padding: 0 15px; text-align: center; font-size: 14px; line-height: 32px; color: #FFF; border: none; background: #1890ff; border-radius: 2px; cursor: pointer; } .mask { position: fixed; top: 0; left: 0; right: 0; bottom: 0; z-index: 999; background: rgba(0, 0, 0, .6); animation: fadeIn .3s forwards; } .slide-box { position: fixed; left: 0; right: 0; bottom: 0; padding: 15px; border-radius: 10px 10px 0 0; background: #FFF; user-select: none; } .fade-in { animation: fadeIn .3s forwards; } @keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } } .fade-out { animation: fadeOut .3s forwards; } @keyframes fadeOut { from { opacity: 10; } to { opacity: 0; } } .slide-up { animation: slideUp .3s forwards; } @keyframes slideUp { from { transform: translate3d(0, 100%, 0); } to { transform: translate3d(0, 0, 0); } } .slide-down { animation: slideDown .3s forwards; } @keyframes slideDown { from { transform: translate3d(0, 0, 0); } to { transform: translate3d(0, 100%, 0); } } h4 { height: 24px; margin-bottom: 16px; font-size: 16px; line-height: 24px; text-align: center; } .picker-group { display: flex; } .picker-column { position: relative; flex: 1; height: 200px; margin: 0 auto; overflow: hidden; touch-action: none; } .picker-column::before { content: ''; position: absolute; top: 0; left: 0; right: 0; z-index: 1; height: 79px; border-bottom: 1px solid #ebebeb; background: linear-gradient(to bottom, rgba(255, 255, 255, .9), rgba(255, 255, 255, .6)); } .picker-column::after { content: ''; position: absolute; bottom: 0; left: 0; right: 0; z-index: 1; height: 79px; border-top: 1px solid #ebebeb; background: linear-gradient(to bottom, rgba(255, 255, 255, .6), rgba(255, 255, 255, .9)); } li { list-style: none; font-size: 14px; line-height: 40px; text-align: center; } .btn-sure { display: block; margin: 15px auto 0; }
HTML
<button class="btn btn-open" type="button">時間選擇器</button> <div hidden class="mask"> <div class="slide-box"> <h4>時間選擇器</h4> <div class="picker-group"> <div class="picker-column"> <ul class="picker-content"></ul> </div> <div class="picker-column"> <ul class="picker-content"></ul> </div> </div> <button class="btn btn-sure" type="button">確定</button> </div> </div>
js
class Picker { constructor(options) { this.options = Object.assign({}, options); this.isPointerdown = false; this.itemHeight = 40; // 列表項高度 this.maxY = this.itemHeight * 2; this.minY = this.itemHeight * (3 - this.options.list.length); this.lastY = 0; this.diffY = 0; this.translateY = 0; // 當前位置 this.friction = 0.95; // 摩擦系數(shù) this.distanceY = 0; // 滑動距離 this.result = this.options.list[0]; this.render(); this.bindEventListener(); } render() { let html = ''; for (const item of this.options.list) { html += '<li>' + item + '</li>'; } this.options.pickerContent.innerHTML = html; this.options.pickerContent.style.transform = 'translate3d(0px, ' + this.maxY + 'px, 0px)'; } handlePointerdown(e) { // 如果是鼠標點擊,只響應(yīng)左鍵 if (e.pointerType === 'mouse' && e.button !== 0) { return; } this.options.pickerColumn.setPointerCapture(e.pointerId); this.isPointerdown = true; this.lastY = e.clientY; this.diffY = 0; this.distanceY = 0; this.getTransform(); this.options.pickerContent.style.transform = 'translate3d(0px, ' + this.translateY + 'px, 0px)'; this.options.pickerContent.style.transition = 'none'; } handlePointermove(e) { if (this.isPointerdown) { this.diffY = e.clientY - this.lastY; this.translateY += this.diffY; this.lastY = e.clientY; this.options.pickerContent.style.transform = 'translate3d(0px, ' + this.translateY + 'px, 0px)'; } } handlePointerup(e) { if (this.isPointerdown) { this.isPointerdown = false; this.getTranslateY(); // 滑動距離與時長成正比且最短時長為300ms const duration = Math.max(Math.abs(this.distanceY) * 1.5, 300); this.options.pickerContent.style.transition = 'transform ' + duration + 'ms ease'; this.options.pickerContent.style.transform = 'translate3d(0px, ' + this.translateY + 'px, 0px)'; } } handlePointercancel(e) { if (this.isPointerdown) { this.isPointerdown = false; } } bindEventListener() { this.handlePointerdown = this.handlePointerdown.bind(this); this.handlePointermove = this.handlePointermove.bind(this); this.handlePointerup = this.handlePointerup.bind(this); this.handlePointercancel = this.handlePointercancel.bind(this); this.options.pickerColumn.addEventListener('pointerdown', this.handlePointerdown); this.options.pickerColumn.addEventListener('pointermove', this.handlePointermove); this.options.pickerColumn.addEventListener('pointerup', this.handlePointerup); this.options.pickerColumn.addEventListener('pointercancel', this.handlePointercancel); } getTransform() { const transform = window.getComputedStyle(this.options.pickerContent).getPropertyValue('transform'); this.translateY = parseFloat(transform.split(',')[5]); } getTranslateY() { let speed = this.diffY; while (Math.abs(speed) > 1) { speed *= this.friction; this.distanceY += speed; } // 邊界判斷 let y = this.translateY + this.distanceY; if (y > this.maxY) { this.translateY = this.maxY; this.distanceY = this.maxY - this.translateY; } else if (y < this.minY) { this.translateY = this.minY; this.distanceY = this.minY - this.translateY; } else { this.translateY = y; } // 計算停止位置使其為itemHeight的整數(shù)倍 let i = Math.round(this.translateY / this.itemHeight); this.translateY = i * this.itemHeight; this.result = this.options.list[2 - this.translateY / this.itemHeight]; } } // 調(diào)用方式 function createList(start, end) { const list = []; for (i = start; i < end; i++) { list[i] = i < 10 ? '0' + i : '' + i; } return list; } const hours = createList(0, 24), minutes = createList(0, 60); const pickerColumns = document.querySelectorAll('.picker-column'); const pickerContents = document.querySelectorAll('.picker-content'); const hourPicker = new Picker({ pickerColumn: pickerColumns[0], pickerContent: pickerContents[0], list: hours }); const minutePicker = new Picker({ pickerColumn: pickerColumns[1], pickerContent: pickerContents[1], list: minutes });
Demo:jsdemo.codeman.top/html/picker…
結(jié)語
至此,一個簡單的picker選擇器就實現(xiàn)了。如果小伙伴們想實現(xiàn)根據(jù)前一列選中項動態(tài)加載后一列數(shù)據(jù)的功能(例如省市區(qū)選擇器)還需在此代碼基礎(chǔ)上自行實現(xiàn)。
以上就是js 實現(xiàn)picker 選擇器示例詳解的詳細內(nèi)容,更多關(guān)于js實現(xiàn)picker選擇器的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
JS寫XSS cookie stealer來竊取密碼的步驟詳解
JavaScript是web中最常用的腳本開發(fā)語言,js可以自動執(zhí)行站點組件,管理站點內(nèi)容,在web業(yè)內(nèi)實現(xiàn)其他有用的函數(shù)。這篇文章主要介紹了JS寫XSS cookie stealer來竊取密碼的步驟詳解,需要的朋友可以參考下2017-11-1128個JavaScript常用字符串方法以及使用技巧總結(jié)
這篇文章主要給大家介紹了28個JavaScript常用字符串方法以及使用技巧的相關(guān)資料,文中統(tǒng)計的方法都非常實用,無論是日常工作還是面試,都建議多看一看,需要的朋友可以參考下2021-09-09js 實現(xiàn)獲取name 相同的頁面元素并循環(huán)遍歷的方法
下面小編就為大家?guī)硪黄猨s 實現(xiàn)獲取name 相同的頁面元素并循環(huán)遍歷的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02JavaScript 上傳文件(psd,壓縮包等),圖片,視頻的實現(xiàn)方法
本文通過實例代碼給大家介紹了JavaScript 上傳文件(psd,壓縮包等),圖片,視頻功能,需要的朋友可以參考下2017-06-06將字符串轉(zhuǎn)換成gb2312或者utf-8編碼的參數(shù)(js版)
直接在url中傳遞中文參數(shù)時,讀到的中文都是亂碼,那么我們應(yīng)該怎么將這些參數(shù)轉(zhuǎn)換呢,接下來與大家分享下將字符串轉(zhuǎn)換成utf-8或者gb2312編碼的參數(shù)的技巧2013-04-04