您现在的位置是:网站首页> 编程资料编程资料
js 实现picker 选择器示例详解_javascript技巧_
2023-05-24
391人已围观
简介 js 实现picker 选择器示例详解_javascript技巧_

前言
想必各位做移动端开发的小伙伴对picker选择器应该不陌生吧。你做微信小程序开发有自带的picker组件,做公众号开发可以使用weui提供的picker组件。除此之外,市面上开源的picker组件也是层出不穷,拿来即用。但如果叫你自己实现一个,你会如何实现呢?我花了点时间写了一个简单的demo,希望能给想自己动手实现一个picker选择器但又无从下手的小伙伴提供一个思路。
实现
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
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; // 摩擦系数 this.distanceY = 0; // 滑动距离 this.result = this.options.list[0]; this.render(); this.bindEventListener(); } render() { let html = ''; for (const item of this.options.list) { html += 'Demo:jsdemo.codeman.top/html/picker…
结语
至此,一个简单的picker选择器就实现了。如果小伙伴们想实现根据前一列选中项动态加载后一列数据的功能(例如省市区选择器)还需在此代码基础上自行实现。
以上就是js 实现picker 选择器示例详解的详细内容,更多关于js实现picker选择器的资料请关注其它相关文章!
您可能感兴趣的文章:
相关内容
- JavaScript中原始值和引用值深入讲解_javascript技巧_
- vue二次封装一个高频可复用组件的全过程_vue.js_
- 详细聊聊JS中不一样的深拷贝_javascript技巧_
- 一文带你简单理解Vue的data为何只能是函数_vue.js_
- typescript常见高级技巧总结_基础知识_
- JavaScript代码优雅,简洁的编写技巧总结_基础知识_
- js中对象深拷贝方法总结_基础知识_
- js检测浏览器夜晚/黑暗(dark)模式方法_基础知识_
- node.js生成与读取csv文件方法详解_node.js_
- JavaScript将数组转为对象与JSON对象字符串转数组方法详解_基础知识_
