仿ios的选择器UI组件Picker.js
2017-06-28 本文已影响0人
Brighten_Sun
先上图让大家看看是什么效果
picker.jpg如果想使用那么接下来下载picker.js
github网址:http://ustbhuangyi.github.io/picker/
针对ES6的用法和require的用法
安装better-picker
npm install better-picker --save-dev
引入better-picker
import Picker from 'better-picker'
如果不支持import, 请使用
var Picker = require('better-picker')
也可以直接script标签src直接引入
如何使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<script type="text/javascript" src="picker.min.js"></script>
</head>
<body>
<!-- html的内容 -->
<div id="name">点我选择</div>
<script type="text/javascript">
//获取元素
var nameEl = document.getElementById('name');
//设置你要放内容的盒子
var data1 = [
{
text: '北京市',
value: 1
}, {
text: '哈尔滨市',
value: 2
}
];
//设置你要放内容的盒子
var data2 = [
{
text: '朝阳',
value: 1
},
{
text: '海淀',
value: 2
},
{
text: '西城',
value: 3
},
{
text: '丰台',
value: 4
},
{
text: '大兴',
value: 5
}
];
//设置你要放内容的盒子
var data3 = [
{
text: '小村庄',
value: 1
}
];
//初始化picker
var picker = new Picker({
data: [data1, data2, data3],
selectedIndex: [0, 0, 0],
title: '请选择你所在的小村庄'
});
//选择后的内容赋值
picker.on('picker.select', function (selectedVal, selectedIndex) {
nameEl.innerText = data1[selectedIndex[0]].text + ' ' + data2[selectedIndex[1]].text + ' ' + data3[selectedIndex[2]].text;
})
//当选择项发生变化时
picker.on('picker.change', function (index, selectedIndex) {
console.log(index+'11');
console.log(selectedIndex+'22');
});
//当你选择确定后
picker.on('picker.valuechange', function (selectedVal, selectedIndex) {
console.log(selectedVal+'33');
console.log(selectedIndex+'44');
});
//点击弹出选项框
nameEl.addEventListener('click', function () {
picker.show();
});
</script>
</body>
</html>