js?實(shí)現(xiàn)picker?選擇器示例詳解

前言
想必各位做移動(dòng)端開(kāi)發(fā)的小伙伴對(duì)picker選擇器應(yīng)該不陌生吧。你做微信小程序開(kāi)發(fā)有自帶的picker組件,做公眾號(hào)開(kāi)發(fā)可以使用weui提供的picker組件。除此之外,市面上開(kāi)源的picker組件也是層出不窮,拿來(lái)即用。但如果叫你自己實(shí)現(xiàn)一個(gè),你會(huì)如何實(shí)現(xiàn)呢?我花了點(diǎn)時(shí)間寫了一個(gè)簡(jiǎn)單的demo,希望能給想自己動(dòng)手實(shí)現(xiàn)一個(gè)picker選擇器但又無(wú)從下手的小伙伴提供一個(gè)思路。
實(shí)現(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">時(shí)間選擇器</button>
<div hidden class="mask">
<div class="slide-box">
<h4>時(shí)間選擇器</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; // 列表項(xiàng)高度
this.maxY = this.itemHeight * 2;
this.minY = this.itemHeight * (3 - this.options.list.length);
this.lastY = 0;
this.diffY = 0;
this.translateY = 0; // 當(dāng)前位置
this.friction = 0.95; // 摩擦系數(shù)
this.distanceY = 0; // 滑動(dòng)距離
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) {
// 如果是鼠標(biāo)點(diǎn)擊,只響應(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();
// 滑動(dòng)距離與時(shí)長(zhǎng)成正比且最短時(shí)長(zhǎng)為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;
}
// 計(jì)算停止位置使其為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é)語(yǔ)
至此,一個(gè)簡(jiǎn)單的picker選擇器就實(shí)現(xiàn)了。如果小伙伴們想實(shí)現(xiàn)根據(jù)前一列選中項(xiàng)動(dòng)態(tài)加載后一列數(shù)據(jù)的功能(例如省市區(qū)選擇器)還需在此代碼基礎(chǔ)上自行實(shí)現(xiàn)。
以上就是js 實(shí)現(xiàn)picker 選擇器示例詳解的詳細(xì)內(nèi)容,更多關(guān)于js實(shí)現(xiàn)picker選擇器的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
JS寫XSS cookie stealer來(lái)竊取密碼的步驟詳解
JavaScript是web中最常用的腳本開(kāi)發(fā)語(yǔ)言,js可以自動(dòng)執(zhí)行站點(diǎn)組件,管理站點(diǎn)內(nèi)容,在web業(yè)內(nèi)實(shí)現(xiàn)其他有用的函數(shù)。這篇文章主要介紹了JS寫XSS cookie stealer來(lái)竊取密碼的步驟詳解,需要的朋友可以參考下2017-11-11
28個(gè)JavaScript常用字符串方法以及使用技巧總結(jié)
這篇文章主要給大家介紹了28個(gè)JavaScript常用字符串方法以及使用技巧的相關(guān)資料,文中統(tǒng)計(jì)的方法都非常實(shí)用,無(wú)論是日常工作還是面試,都建議多看一看,需要的朋友可以參考下2021-09-09
uniapp監(jiān)聽(tīng)頁(yè)面滾動(dòng)2種常用方法
在uni-app中,監(jiān)聽(tīng)頁(yè)面滾動(dòng)可以使用onPageScroll生命周期函數(shù)或@scroll事件監(jiān)聽(tīng)器,onPageScroll適用于監(jiān)聽(tīng)整個(gè)頁(yè)面的滾動(dòng)事件,而@scroll事件監(jiān)聽(tīng)器適用于監(jiān)聽(tīng)特定組件如scroll-view的滾動(dòng),這兩種方法的選擇取決于監(jiān)聽(tīng)需求的不同,需要的朋友可以參考下2024-09-09
js 實(shí)現(xiàn)獲取name 相同的頁(yè)面元素并循環(huán)遍歷的方法
下面小編就為大家?guī)?lái)一篇js 實(shí)現(xiàn)獲取name 相同的頁(yè)面元素并循環(huán)遍歷的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
JavaScript 上傳文件(psd,壓縮包等),圖片,視頻的實(shí)現(xiàn)方法
本文通過(guò)實(shí)例代碼給大家介紹了JavaScript 上傳文件(psd,壓縮包等),圖片,視頻功能,需要的朋友可以參考下2017-06-06
JavaScript判斷頁(yè)面是否滾動(dòng)到底部的技巧
在 JavaScript 中,我們可以通過(guò)一些技巧來(lái)判斷頁(yè)面是否滾動(dòng)到底部,本文將介紹一些常用的方法,幫助你在項(xiàng)目中實(shí)現(xiàn)這一功能,文中通過(guò)代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11
將字符串轉(zhuǎn)換成gb2312或者utf-8編碼的參數(shù)(js版)
直接在url中傳遞中文參數(shù)時(shí),讀到的中文都是亂碼,那么我們應(yīng)該怎么將這些參數(shù)轉(zhuǎn)換呢,接下來(lái)與大家分享下將字符串轉(zhuǎn)換成utf-8或者gb2312編碼的參數(shù)的技巧2013-04-04

