1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414 |
- /*!
- * popoPicker.js v1.0
- * https://po-po.github.io/popoPicker
- * Released under the MIT License.
- */
- ;(function (window,document) {
- 'use strict';
- function extend() {
- var name, options, src, copy;
- var length = arguments.length;
- var target = arguments[0];
- for (var i = 1; i < length; i++) {
- options = arguments[i];
- if (options != null) {
- for (name in options) {
- src = target[name];
- copy = options[name];
- if (copy !== undefined) {
- target[name] = copy;
- }
- }
- }
- }
- return target;
- }
- var popoPicker = function (el, option) {
- //触摸移动
- var self = this;
- var option = extend({
- wheels: [],
- container:'body',
- scrollType: '3d',
- background: 'light',
- display: "bottom",
- headTitle: '',
- init: function () {},
- getResult: function () {},
- save: function () {},
- cancel: function () {}
- }, option);
- var clickTarget = document.querySelectorAll(el);
- var rows = 5;
- var itemHeight = 34;
- var itemSize2d = 9;
- var itemSize3d = 9;
- var scroll3dAngle = 360 / (itemSize3d * 2);
- var rs = {
- result: [],
- scrollIdx: null,
- scrollEvt: []
- };
- function getselectedIdx(wheel) {
- var index = 0;
- for (var i = 0; i < wheel.data.length; i++) {
- if (wheel.data[i].value == wheel.selected) {
- index = i;
- break;
- } else {
- index = 0;
- }
- }
- return index;
- }
- function generateItems(wheel, start, end, is3d) {
- var data = wheel.data;
- var html = '',
- value,
- display,
- len = data.length,
- infinite = wheel.infinite,
- selectedIdx = getselectedIdx(wheel);
- //选中的位置
- start += selectedIdx;
- end += selectedIdx;
- for (var i = start; i <= end; i++) {
- var idx = (i < 0 ? len + (i % len) : i) % len;
- value = data[idx].value;
- display = data[idx].display;
- if (is3d) {
- var deg = 0;
- var show = "list-item";
- deg = -(i - selectedIdx) * scroll3dAngle % 360;
- if (!infinite) {
- if (i < 0 || i > (len - 1)) {
- show = "none"
- } else {
- show = "list-item"
- }
- }
- html += '<li data-index="' + i + '" data-val="' + value + '" style="transform:rotateX(' + deg + 'deg) translateZ(' + (itemHeight * rows / 2) + 'px); display: ' + show + '">' + display + '</li>';
- } else {
- var opacity = 1;
- if (!infinite) {
- if (i < 0 || i > (len - 1)) {
- opacity = 0
- } else {
- opacity = 1
- }
- }
- html += '<li data-index="' + i + '" data-val="' + value + '" style="opacity: ' + opacity + '">' + display + '</li>';
- }
- }
- return html;
- }
- function createEl(wheels) {
- var html = '';
- html += '<div class="p-select-wrap '+ (option.scrollType == '3d' ? 'p-3d' : '') + (option.display == 'center' ? ' p-center' : '') +(option.background == 'dark' ? ' dark' : '')+'">';
- html += '<div class="p-select-main">';
- html += '<div class="p-select-head">';
- if (option.display != 'center') {
- html += '<a href="javascript:void(0)" class="p-select-cancel-btn">取消</a>';
- }
- if (option.headTitle != '') {
- html += '<div class="p-select-title">' + option.headTitle + '</div>';
- }
- if (option.display != 'center') {
- html += '<a href="javascript:void(0)" class="p-select-submit-btn">确认</a>';
- }
- html += '</div>';
- html += '<div class="p-select-body">';
- html += '<div class="p-select-line" '+ (option.scrollType=='3d' ? 'style="transform: translateZ(' + (itemHeight * rows / 2 + 4) + 'px)"':'') +'></div>';
- for (var i = 0; i < wheels.length; i++) {
- var label = wheels[i].label;
- html += '<div class="p-select-item">';
- html += '<div class="p-select-col">';
- html += '<div class="p-select-list" '+ (option.scrollType=='3d' ? 'style="transform: translateZ(' + (itemHeight * rows / 2 +3) + 'px)"':'') +'>';
- html += '<ul class="p-select-ul">';
- html += generateItems(wheels[i], -itemSize2d, itemSize2d, false);
- html += '</ul>';
- html += '</div>';
- if (option.scrollType == '3d') {
- html += '<ul class="p-select-wheel">';
- html += generateItems(wheels[i], -itemSize3d, itemSize3d, true);
- html += '</ul>';
- }
- html += '</div>';
- if (label) {
- html += '<div class="p-select-col p-select-label" '+ (option.scrollType=='3d' ? 'style="transform: translateZ(' + (itemHeight * rows / 2 + 4) + 'px)"':'') +'>' + label + '</div>';
- }
- html += '</div>';
- }
- html += '</div>';
- if (option.display == 'center') {
- html += '<div class="p-select-foot">';
- html += '<a href="javascript:void(0)" class="p-select-cancel-btn">取消</a>';
- html += '<a href="javascript:void(0)" class="p-select-submit-btn">确认</a>';
- html += '</div>';
- }
- html += '</div>';
- html += '<div class="p-select-mask"></div>';
- html += '</div>';
- var node = document.createElement("div");
- node.className = "p-scroll";
- node.innerHTML = html;
- document.querySelector(option.container).appendChild(node)
- }
- function snap(pos) {
- var pos = Math.round(pos),
- n1 = Math.round(pos % itemHeight),
- n2 = itemHeight / 2;
- if (Math.abs(n1) < n2) {
- return pos - n1;
- } else {
- return pos - n1 + (pos > 0 ? itemHeight : -itemHeight);
- }
- }
- function getVal(pos, data, infinite, selectedIdx) {
- var len = data.length,
- index = (Math.round(-pos / itemHeight) - itemSize2d + selectedIdx) % len,
- index = index < 0 ? len + index : index,
- dataIndex = Math.round(-pos / itemHeight) - itemSize2d + selectedIdx,
- result = {
- value: data[index].value,
- display: data[index].display,
- dataIndex: dataIndex
- };
- return result;
- }
- function getPosition(el) {
- var style = getComputedStyle(el),
- matrix,
- pref = ['t', 'webkitT', 'MozT', 'OT', 'msT'],
- px;
- for (var i = 0; i < pref.length; i++) {
- var v = pref[i];
- if (style[v + 'ransform'] !== undefined) {
- matrix = style[v + 'ransform'];
- break;
- }
- }
- matrix = matrix.split(')')[0].split(', ');
- px = Number(matrix[13] || matrix[5]);
- return px;
- }
- function isPC() {
- var userAgentInfo = navigator.userAgent;
- var Agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];
- var flag = true;
- for (var i = 0; i < Agents.length; i++) {
- if (userAgentInfo.indexOf(Agents[i]) > 0) {
- flag = false;
- break;
- }
- }
- return flag;
- }
- function destroy() {
- rs = {
- result: [],
- scrollIdx: 0,
- scrollEvt :[],
- };
- var box = document.querySelector('.p-scroll');
- box.className += ' hide';
- setTimeout(function () {
- box.remove();
- }, 300)
- }
- this.Scroll = function (el, wheel, index, opt) {
- this.el = el;
- this.wheel = wheel;
- this.index = index;
- this.opt = extend({
- data: this.wheel.data,
- scrollEl2d: el.querySelector('.p-select-ul'),
- item2d: el.querySelectorAll('.p-select-ul li'),
- scrollY: -itemHeight * itemSize2d,
- marginTop: 0,
- dataLen: this.wheel.data.length,
- lastY: 1,
- moveY: 0,
- current: 0,
- infinite: this.wheel.infinite,
- rotateX: 0,
- selectedIdx: getselectedIdx(wheel),
- transTimer: null,
- scrollTimer: null,
- clickDown: false,
- inertia: true
- }, opt);
- if (option.scrollType == "3d") {
- this.opt.scrollEl3d = el.querySelector('.p-select-wheel');
- this.opt.item3d = el.querySelectorAll('.p-select-wheel li');
- }
- this.defaultScrollY = this.opt.scrollY;
- this.init();
- };
- this.Scroll.prototype = {
- init: function () {
- this.Run(0, this.opt.scrollY);
- if (isPC()) {
- this.el.addEventListener('mousedown', this.touchStart.bind(this), false);
- document.addEventListener('mousemove', this.touchMove.bind(this), false);
- document.addEventListener('mouseup', this.touchEnd.bind(this), false);
- } else {
- this.el.addEventListener('touchstart', this.touchStart.bind(this), false);
- this.el.addEventListener('touchmove', this.touchMove.bind(this), false);
- this.el.addEventListener('touchend', this.touchEnd.bind(this), false);
- this.el.addEventListener('touchcancel', this.touchEnd.bind(this), false);
- }
- var self = this;
- if (option.scrollType == '3d') {
- //3d点击事件
- this.itemClick(this.opt.item3d);
- } else {
- //2d点击事件
- this.itemClick(this.opt.item2d);
- }
- },
- itemClick: function (itemObj) {
- var self = this;
- for (var i = 0; i < itemObj.length; i++) {
- itemObj[i].addEventListener('click', function (e) {
- self.opt.inertia = false;
- var curIdx = Number(e.target.dataset.index);
- var lastIdx = rs.result[self.index].dataIndex;
- var diff = curIdx - lastIdx;
- self.cusTouch(diff);
- }, false)
- }
- },
- Run: function (interval, pos) {
- if (interval == 0) {
- this.opt.scrollEl2d.style.webkitTransition = "none";
- if (option.scrollType == "3d") {
- this.opt.scrollEl3d.style.webkitTransition = "none";
- }
- } else {
- this.opt.scrollEl2d.style.webkitTransition = "transform cubic-bezier(0.190, 1.000, 0.220, 1.000) " + interval + "ms";
- if (option.scrollType == "3d") {
- this.opt.scrollEl3d.style.webkitTransition = "transform cubic-bezier(0.190, 1.000, 0.220, 1.000) " + interval + "ms";
- }
- }
- this.opt.scrollEl2d.style.webkitTransform = "translate3d(0," + pos + "px, 0)";
- if (option.scrollType == "3d") {
- pos = Math.round(-(pos * (scroll3dAngle / itemHeight)) - 180);
- this.opt.scrollEl3d.style.webkitTransform = "rotateX(" + pos + "deg) translate3d(0,0,0)";
- }
- },
- scrollDone: function () {
- clearInterval(this.opt.scrollTimer);
- clearTimeout(this.opt.transTimer);
- },
- onMove: function (pos, cusDiff) {
- var self = this,
- pos = pos || getPosition(this.opt.scrollEl2d),
- index = Math.round(-pos / itemHeight) - itemSize2d,
- diff = index - this.opt.current;
- if (cusDiff != undefined || diff) {
- this.opt.current = index;
- //2D
- for (var i = 0; i < this.opt.item2d.length; i++) {
- var item = this.opt.item2d[i];
- var index = Number(item.getAttribute("data-index")) + diff,
- len = self.opt.dataLen,
- idx = (index < 0 ? len + (index % len) : index) % len,
- val = self.opt.data[idx].value,
- display = self.opt.data[idx].display;
- if (!self.opt.infinite) {
- if (index < 0 || index > (len - 1)) {
- item.style.opacity = "0"
- } else {
- item.style.opacity = "1"
- }
- }
- item.setAttribute("data-index", index);
- item.setAttribute("data-val", val);
- item.innerText = display;
- }
- if (option.scrollType == "3d") {
- //3D
- for (var i = 0; i < this.opt.item3d.length; i++) {
- var item = this.opt.item3d[i];
- var index = Number(item.getAttribute("data-index")) + diff,
- len = self.opt.dataLen,
- idx = (index < 0 ? len + (index % len) : index) % len,
- val = self.opt.data[idx].value,
- deg = -(index - self.opt.selectedIdx) * scroll3dAngle,
- display = self.opt.data[idx].display;
- if (!self.opt.infinite) {
- if (index < 0 || index > (len - 1)) {
- item.style.display = "none"
- } else {
- item.style.display = "list-item"
- }
- }
- item.setAttribute("data-index", index);
- item.setAttribute("data-val", val);
- item.setAttribute("data-idx", idx);
- item.innerText = display;
- item.style.webkitTransform = "rotateX(" + deg + "deg) translateZ(" + (itemHeight * rows / 2) + "px)";
- }
- }
- //2d margin-top
- this.opt.marginTop += diff * itemHeight;
- this.opt.scrollEl2d.style.marginTop = this.opt.marginTop + "px";
- }
- },
- touchStart: function (e) {
- if (e.type == 'touchstart') {
- this.opt.startY = e.targetTouches["0"].clientY;
- } else {
- this.opt.startY = e.clientY;
- this.opt.clickDown = true;
- }
- this.opt.startTime = new Date();
- },
- touchMove: function (e) {
- if (e.type == 'touchmove') {
- e.preventDefault();
- for (var i = 0; i < e.targetTouches.length; i++) {
- this.opt.curY = e.targetTouches[i].clientY;
- }
- } else {
- if (this.opt.clickDown) {
- this.opt.curY = e.clientY;
- } else {
- return false;
- }
- }
- this.opt.moveY = this.opt.curY - this.opt.startY;
- this.opt.distance = this.opt.scrollY + this.opt.moveY;
- if (this.opt.curY < this.opt.lastY) {
- //move up
- this.opt.direction = 1;
- } else if (this.opt.curY > this.opt.lastY) {
- //move down
- this.opt.direction = -1;
- }
- if (this.opt.direction) {
- this.onMove();
- this.opt.lastY = this.opt.curY;
- this.Run(0, this.opt.distance);
- }
- },
- touchEnd: function (e) {
- if (e.type == 'touchend') {
- this.opt.lastY = e.changedTouches["0"].clientY;
- } else {
- if (this.opt.clickDown) {
- this.opt.lastY = e.clientY;
- this.opt.clickDown = false;
- } else {
- return false;
- }
- }
- this.opt.endTime = new Date();
- var self = this,
- interval = this.opt.endTime - this.opt.startTime,
- speed = 500,
- addPos = 0;
- //模拟惯性
- if (this.opt.inertia && interval < 300) {
- speed = Math.abs(this.opt.moveY / interval);
- speed = Math.round(speed * 1000);
- addPos = speed / 3 * (this.opt.moveY < 0 ? -1 : 1);
- speed = speed < 500 ? 500 : speed;
- } else {
- addPos = this.opt.moveY;
- this.opt.inertia = true
- }
- this.opt.scrollY += snap(addPos);
- this.opt.moveY = 0;
- clearInterval(this.opt.scrollTimer);
- this.opt.scrollTimer = setInterval(function () {
- self.onMove();
- }, 100);
- clearTimeout(this.opt.transTimer);
- this.opt.transTimer = setTimeout(function () {
- self.onMove();
- self.scrollDone();
- }, speed);
- //非无限滚动阈值
- if (!this.opt.infinite) {
- var maxScroll = this.opt.selectedIdx * itemHeight - itemHeight * itemSize2d;
- var minScroll = this.opt.selectedIdx * itemHeight - itemHeight * (this.opt.dataLen - 1) - itemHeight * itemSize2d;
- if (this.opt.scrollY > maxScroll) {
- this.opt.scrollY = maxScroll;
- } else if (this.opt.scrollY < minScroll) {
- this.opt.scrollY = minScroll;
- }
- }
- this.Run(speed, this.opt.scrollY);
- var res = getVal(this.opt.scrollY, this.opt.data, this.opt.infinite, this.opt.selectedIdx);
- rs.result[this.index] = res;
- rs.scrollIdx = this.index;
- option.getResult(rs);
- },
- cusTouch: function (diff) {
- var e1 = {
- clientY: 0,
- targetTouches: [{clientY: 0}],
- type: "click"
- };
- var e2 = {
- clientY: -itemHeight * diff,
- targetTouches: [{clientY: -itemHeight * diff}],
- type: "click"
- };
- var e3 = {
- clientY: -itemHeight * diff,
- changedTouches: [{clientY: -itemHeight * diff}],
- type: "click"
- };
- this.touchStart(e1)
- this.touchMove(e2);
- this.touchEnd(e3);
- },
- scrollTo: function (val, interval) {
- this.wheel.selected = val;
- var selectedIdx = getselectedIdx(this.wheel);
- var diff = this.opt.selectedIdx - selectedIdx;
- this.opt.scrollY = this.defaultScrollY + diff * itemHeight;
- this.Run(interval, this.opt.scrollY);
- this.onMove(this.opt.scrollY, true);
- },
- removeItem: function (valArr) {
- var data = this.opt.data,
- resultVal = rs.result[this.index].value,
- removeLen = 0,
- moveEnd = false;
- for (var i = 0; i < data.length; i++) {
- for (var j = 0; j < valArr.length; j++) {
- if (data[i].value == valArr[j]) {
- //移除
- data.splice(i--, 1);
- removeLen++;
- } else {
- if (valArr[j] == resultVal) {
- moveEnd = true;
- }
- }
- }
- }
- if (moveEnd) {
- rs.result[this.index] = {
- value: data[data.length - 1].value,
- display: data[data.length - 1].display
- }
- }
- this.opt.dataLen = data.length;
- this.scrollTo(rs.result[this.index].value, 0);
- },
- appendItem: function (valArr) {
- var data = this.opt.data;
- //去重
- for (var i = 0; i < data.length; i++) {
- for (var j = 0; j < valArr.length; j++) {
- if (data[i].value == valArr[j].value) {
- valArr.splice(j--, 1)
- }
- }
- }
- //添加
- for (var i = 0; i < valArr.length; i++) {
- data.push(valArr[i]);
- }
- this.opt.dataLen = data.length;
- this.scrollTo(rs.result[this.index].value, 0);
- }
- };
- function init(wheels, target) {
- //创建DOM
- createEl(wheels);
- var el = document.querySelectorAll(".p-select-item");
- for (var i = 0; i < el.length; i++) {
- //滚动事件
- var scroll = new self.Scroll(el[i], wheels[i], i);
- //初始结果
- var res = getVal(scroll.opt.scrollY, scroll.opt.data, scroll.opt.infinite, scroll.opt.selectedIdx);
- rs.result.push(res);
- rs.scrollEvt.push(scroll);
- rs.scrollIdx = 0;
- }
- //传出初始结果
- option.init(rs);
- var submitBtn = document.querySelector('.p-select-submit-btn'),
- cancelBtn = document.querySelector('.p-select-cancel-btn'),
- mask = document.querySelector('.p-select-mask');
- submitBtn.addEventListener('click', function () {
- var attr_rs = [],
- display = [];
- for (var i = 0; i < rs.result.length; i++) {
- attr_rs.push(rs.result[i].value);
- display.push(rs.result[i].display)
- }
- target.setAttribute('data-value', attr_rs);
- if(target.isInput){
- target.value = display;
- }else{
- target.innerText = display;
- }
- option.getResult(rs);
- option.save(rs, target);
- self.target = target;
- destroy()
- }, false);
- cancelBtn.addEventListener('click', function () {
- option.cancel();
- destroy()
- }, false);
- mask.addEventListener('click', function () {
- option.cancel();
- destroy()
- }, false);
- document.querySelector('.p-scroll').addEventListener('touchmove', function (e) {
- e.preventDefault()
- }, false);
- }
- for (var i = 0; i < clickTarget.length; i++) {
- var item = clickTarget[i],
- wheels = option.wheels,
- isInput = item.localName == 'input' || item.localName == 'textarea',
- attr_rs = item.getAttribute('data-value');
- if(attr_rs){
- attr_rs = attr_rs.split(',');
- var display = [];
- for (var j=0;j<attr_rs.length;j++){
- wheels[j].selected = attr_rs[j];
- var idx = getselectedIdx(wheels[j])
- display.push(wheels[j].data[idx].display)
- }
- if(isInput){
- item.value = display;
- }else{
- item.innerText = display;
- }
- }
- if(isInput){
- item.setAttribute('readonly',"readonly");
- item.setAttribute('unselectable','on');
- }
- item.addEventListener('click', function () {
- var target = this,
- wheelsCopy = JSON.parse(JSON.stringify(wheels)),
- attr_rs = target.getAttribute('data-value');
- target.isInput = isInput;
- if(isInput){
- target.blur();
- }
- if (attr_rs) {
- attr_rs = attr_rs.split(',');
- for (var j = 0; j < attr_rs.length; j++) {
- wheelsCopy[j].selected = attr_rs[j];
- }
- init(wheelsCopy, target ,isInput);
- }else{
- init(wheels, target, isInput);
- }
- }, false)
- }
- };
- var popoDateTime = function(el,opt){
- var opt = extend({
- container:'body',
- scrollType: "3d",
- background:'light',
- showLabel: true, //显示label
- labelType:'symbol', //symbol符号 text文字 split分割(需date和time同时存在)
- display:"bottom",
- headResult: false,
- date:true,
- time:true,
- beginYear: new Date().getFullYear()-100,
- endYear:new Date().getFullYear()+100,
- startDate: '',
- save:function(){},
- cancel:function(){}
- },opt);
- function addZero(num){
- return num<10?"0"+num:String(num);
- }
- var d = new Date(),
- year = d.getFullYear(),
- month = d.getMonth() + 1,
- day = d.getDate(),
- hour = d.getHours(),
- minute = d.getMinutes(),
- beginYear = opt.beginYear,
- endYear = opt.endYear,
- yearData = [],
- monthData = [],
- dayData = [],
- hourData = [],
- minuteData = [],
- lastValue = '',
- wheels = [
- {
- infinite: false,
- selected: addZero(year),
- label:"<span>-</span>",
- data: yearData
- },
- {
- infinite: true,
- selected: addZero(month),
- label:"<span>-</span>",
- data: monthData
- },
- {
- infinite: true,
- selected: addZero(day),
- label:"",
- data: dayData
- },
- {
- infinite: true,
- selected: addZero(hour),
- label:"<span>:</span>",
- data: hourData
- },
- {
- infinite: true,
- selected: addZero(minute),
- label:"",
- data: minuteData
- },
- ];
- for(var i=beginYear;i<=endYear;i++){
- yearData.push({value:addZero(i),display:addZero(i)});
- }
- //月
- for(var i=1;i<=12;i++){
- monthData.push({value:addZero(i),display:addZero(i)});
- }
- //日
- for(var i=1;i<=31;i++){
- dayData.push({value:addZero(i),display:addZero(i)});
- }
- //时
- for(var i=0;i<=23;i++){
- hourData.push({value:addZero(i),display:addZero(i)});
- }
- //分
- for(var i=0;i<=59;i++){
- minuteData.push({value:addZero(i),display:addZero(i)});
- }
- //判断显示日期/时间
- if(!opt.date){
- wheels.splice(0,3);
- }
- if(!opt.time){
- wheels.splice(3,5);
- wheels[2].label = " ";
- }
- //Label控制
- if(!opt.showLabel){
- for(var i=0;i<wheels.length;i++){
- delete wheels[i].label;
- }
- }else{
- if(opt.labelType == 'text'){
- var text = ['年','月','日','时','分','秒']
- for(var i=0;i<wheels.length;i++){
- wheels[i].label = text[i];
- }
- }
- if(opt.labelType == 'split' && opt.date && opt.time){
- for(var i=0;i<wheels.length;i++){
- delete wheels[i].label;
- }
- wheels[2].label='<div class="p-select-time-split-1"></div>';
- }
- }
- //判断闰年
- function isLeapYear(year) {
- if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
- return true;
- } else {
- return false;
- }
- }
- //判断大小月
- function bigSmallMonth(dayObj,year,month){
- var smallMonth = (month=="04" || month=="06" || month=="09" || month=="11");
- dayObj.appendItem(
- [
- {value:"29",display:"29"},
- {value:"30",display:"30"},
- {value:"31",display:"31"}
- ]
- );
- if(smallMonth){
- dayObj.removeItem(["31"]);
- }else if(month=="02"){
- if(isLeapYear(year)){
- dayObj.removeItem(["30","31"]);
- }else{
- dayObj.removeItem(["29","30","31"]);
- }
- }
- }
- //获取结果
- function getLastVal(result) {
- if(result.length==5){
- lastValue = result[0].display+'-'+result[1].display+'-'+result[2].display+' '+result[3].display+':'+result[4].display;
- }else if (result.length==3){
- lastValue = result[0].display+'-'+result[1].display+'-'+result[2].display;
- } else{
- lastValue = result[0].display+':'+result[1].display;
- }
- if(opt.headResult){
- var title = document.querySelector('.p-select-title');
- title.innerText = lastValue;
- }
- }
- //日期滚轮
- var p = new popoPicker(el,{
- wheels: wheels,
- scrollType:opt.scrollType,
- background: opt.background,
- display:opt.display,
- headTitle:opt.headResult?1:'',
- container:opt.container,
- init:function(rs){
- if(opt.date){
- var year = rs.result[0].value;
- var month = rs.result[1].value;
- bigSmallMonth(rs.scrollEvt[2],year,month);
- }
- getLastVal(rs.result)
- },
- getResult:function(rs){
- var n1 = opt.date && (rs.scrollIdx== 0 || rs.scrollIdx== 1);
- year = rs.result[0].value,
- month = rs.result[1].value;
- if(n1){
- bigSmallMonth(rs.scrollEvt[2],year,month);
- }
- getLastVal(rs.result)
- },
- save:function(rs, target){
- if(target.isInput){
- target.value = lastValue;
- }else{
- target.innerText = lastValue;
- }
- opt.save(lastValue, target);
- },
- cancel:function(){
- opt.cancel();
- }
- });
- //判断是否有自定义初始值
- var target = document.querySelectorAll(el);
- for(var i=0;i<target.length;i++){
- var item = target[i],
- isInput = item.localName == 'input' || item.localName == 'textarea',
- val = isInput?item.value:item.innerText;
- if(val){
- val = val.match(/\d+/g);
- item.setAttribute('data-value',val);
- }
- }
- };
- var popoDateT = function(el,opt){
- var opt = extend({
- container:'body',
- scrollType: "3d",
- background:'light',
- showLabel: true, //显示label
- labelType:'symbol', //symbol符号 text文字 split分割(需date和time同时存在)
- display:"bottom",
- headResult: false,
- date:true,
- time:true,
- beginYear: new Date().getFullYear()-100,
- endYear:new Date().getFullYear()+100,
- startDate: '',
- save:function(){},
- cancel:function(){}
- },opt);
- function addZero(num){
- return num<10?"0"+num:String(num);
- }
- var d = new Date(),
- year = d.getFullYear(),
- month = d.getMonth() + 1,
- day = d.getDate(),
- hour = d.getHours(),
- minute = d.getMinutes(),
- beginYear = opt.beginYear,
- endYear = opt.endYear,
- yearData = [],
- monthData = [],
- dayData = [],
- // hourData = [],
- // minuteData = [],
- lastValue = '',
- wheels = [
- {
- infinite: false,
- selected: addZero(year),
- label:"<span>-</span>",
- data: yearData
- },
- {
- infinite: true,
- selected: addZero(month),
- label:"<span>-</span>",
- data: monthData
- },
- {
- infinite: true,
- selected: addZero(day),
- label:"",
- data: dayData
- }
- ];
- for(var i=beginYear;i<=endYear;i++){
- yearData.push({value:addZero(i),display:addZero(i)});
- }
- //月
- for(var i=1;i<=12;i++){
- monthData.push({value:addZero(i),display:addZero(i)});
- }
- //日
- for(var i=1;i<=31;i++){
- dayData.push({value:addZero(i),display:addZero(i)});
- }
- //Label控制
- if(!opt.showLabel){
- for(var i=0;i<wheels.length;i++){
- delete wheels[i].label;
- }
- }else{
- if(opt.labelType == 'text'){
- var text = ['年','月','日','时','分','秒']
- for(var i=0;i<wheels.length;i++){
- wheels[i].label = text[i];
- }
- }
- if(opt.labelType == 'split' && opt.date && opt.time){
- for(var i=0;i<wheels.length;i++){
- delete wheels[i].label;
- }
- wheels[2].label='<div class="p-select-time-split-1"></div>';
- }
- }
- //判断闰年
- function isLeapYear(year) {
- if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
- return true;
- } else {
- return false;
- }
- }
- //判断大小月
- function bigSmallMonth(dayObj,year,month){
- var smallMonth = (month=="04" || month=="06" || month=="09" || month=="11");
- dayObj.appendItem(
- [
- {value:"29",display:"29"},
- {value:"30",display:"30"},
- {value:"31",display:"31"}
- ]
- );
- if(smallMonth){
- dayObj.removeItem(["31"]);
- }else if(month=="02"){
- if(isLeapYear(year)){
- dayObj.removeItem(["30","31"]);
- }else{
- dayObj.removeItem(["29","30","31"]);
- }
- }
- }
- //获取结果
- function getLastVal(result) {
- if(result.length==5){
- lastValue = result[0].display+'-'+result[1].display+'-'+result[2].display+' '+result[3].display+':'+result[4].display;
- }else if (result.length==3){
- lastValue = result[0].display+'-'+result[1].display+'-'+result[2].display;
- } else{
- lastValue = result[0].display+':'+result[1].display;
- }
- if(opt.headResult){
- var title = document.querySelector('.p-select-title');
- title.innerText = lastValue;
- }
- }
- //日期滚轮
- var p = new popoPicker(el,{
- wheels: wheels,
- scrollType:opt.scrollType,
- background: opt.background,
- display:opt.display,
- headTitle:opt.headResult?1:'',
- container:opt.container,
- init:function(rs){
- if(opt.date){
- var year = rs.result[0].value;
- var month = rs.result[1].value;
- bigSmallMonth(rs.scrollEvt[2],year,month);
- }
- getLastVal(rs.result)
- },
- getResult:function(rs){
- var n1 = opt.date && (rs.scrollIdx== 0 || rs.scrollIdx== 1);
- year = rs.result[0].value,
- month = rs.result[1].value;
- if(n1){
- bigSmallMonth(rs.scrollEvt[2],year,month);
- }
- getLastVal(rs.result)
- },
- save:function(rs, target){
- if(target.isInput){
- target.value = lastValue;
- }else{
- target.innerText = lastValue;
- }
- opt.save(lastValue, target);
- },
- cancel:function(){
- opt.cancel();
- }
- });
- //判断是否有自定义初始值
- var target = document.querySelectorAll(el);
- for(var i=0;i<target.length;i++){
- var item = target[i],
- isInput = item.localName == 'input' || item.localName == 'textarea',
- val = isInput?item.value:item.innerText;
- if(val){
- val = val.match(/\d+/g);
- item.setAttribute('data-value',val);
- }
- }
- };
- var popoDate = function(el,opt){
- var opt = extend({
- container:'body',
- scrollType: "3d",
- background:'light',
- showLabel: true, //显示label
- labelType:'symbol', //symbol符号 text文字 split分割(需date和time同时存在)
- display:"bottom",
- headResult: false,
- // date:true,
- // time:true,
- beginYear: new Date().getFullYear()-100,
- endYear:new Date().getFullYear()+100,
- startDate: '',
- save:function(){},
- cancel:function(){}
- },opt);
- function addZero(num){
- return num<10?"0"+num:String(num);
- }
- var d = new Date(),
- year = d.getFullYear(),
- month = d.getMonth() + 1,
- day = d.getDate(),
- hour = d.getHours(),
- minute = d.getMinutes(),
- beginYear = opt.beginYear,
- endYear = opt.endYear,
- yearData = [],
- monthData = [],
- // dayData = [],
- // hourData = [],
- // minuteData = [],
- lastValue = '',
- wheels = [
- {
- infinite: false,
- selected: addZero(year),
- label:"<span>-</span>",
- data: yearData
- },
- {
- infinite: true,
- selected: addZero(month),
- label:"<span></span>",
- data: monthData
- },
- ];
- for(var i=beginYear;i<=endYear;i++){
- yearData.push({value:addZero(i),display:addZero(i)});
- }
- //月
- for(var i=1;i<=12;i++){
- monthData.push({value:addZero(i),display:addZero(i)});
- }
- //Label控制
- if(!opt.showLabel){
- for(var i=0;i<wheels.length;i++){
- delete wheels[i].label;
- }
- }else{
- if(opt.labelType == 'text'){
- var text = ['年','月','日','时','分','秒']
- for(var i=0;i<wheels.length;i++){
- wheels[i].label = text[i];
- }
- }
- if(opt.labelType == 'split' && opt.date && opt.time){
- for(var i=0;i<wheels.length;i++){
- delete wheels[i].label;
- }
- wheels[2].label='<div class="p-select-time-split-1"></div>';
- }
- }
- //判断闰年
- function isLeapYear(year) {
- if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
- return true;
- } else {
- return false;
- }
- }
- //判断大小月
- function bigSmallMonth(dayObj,year,month){
- var smallMonth = (month=="04" || month=="06" || month=="09" || month=="11");
- dayObj.appendItem(
- [
- {value:"29",display:"29"},
- {value:"30",display:"30"},
- {value:"31",display:"31"}
- ]
- );
- if(smallMonth){
- dayObj.removeItem(["31"]);
- }else if(month=="02"){
- if(isLeapYear(year)){
- dayObj.removeItem(["30","31"]);
- }else{
- dayObj.removeItem(["29","30","31"]);
- }
- }
- }
- //获取结果
- function getLastVal(result) {
- if(result.length==5){
- lastValue = result[0].display+'-'+result[1].display+'-'+result[2].display+' '+result[3].display+':'+result[4].display;
- }else if (result.length==3){
- lastValue = result[0].display+'-'+result[1].display+'-'+result[2].display;
- } else{
- lastValue = result[0].display+'-'+result[1].display;
- }
- if(opt.headResult){
- var title = document.querySelector('.p-select-title');
- title.innerText = lastValue;
- }
- }
- //日期滚轮
- var p = new popoPicker(el,{
- wheels: wheels,
- scrollType:opt.scrollType,
- background: opt.background,
- display:opt.display,
- headTitle:opt.headResult?1:'',
- container:opt.container,
- init:function(rs){
- if(opt.date){
- var year = rs.result[0].value;
- var month = rs.result[1].value;
- bigSmallMonth(rs.scrollEvt[2],year,month);
- }
- getLastVal(rs.result)
- },
- getResult:function(rs){
- var n1 = opt.date && (rs.scrollIdx== 0 || rs.scrollIdx== 1);
- year = rs.result[0].value,
- month = rs.result[1].value;
- if(n1){
- bigSmallMonth(rs.scrollEvt[2],year,month);
- }
- getLastVal(rs.result)
- },
- save:function(rs, target){
- if(target.isInput){
- target.value = lastValue;
- }else{
- target.innerText = lastValue;
- }
- opt.save(lastValue, target);
- },
- cancel:function(){
- opt.cancel();
- }
- });
- //判断是否有自定义初始值
- var target = document.querySelectorAll(el);
- for(var i=0;i<target.length;i++){
- var item = target[i],
- isInput = item.localName == 'input' || item.localName == 'textarea',
- val = isInput?item.value:item.innerText;
- if(val){
- val = val.match(/\d+/g);
- item.setAttribute('data-value',val);
- }
- }
- };
- var popoYear = function(el,opt){
- var opt = extend({
- container:'body',
- scrollType: "3d",
- background:'light',
- showLabel: true, //显示label
- labelType:'symbol', //symbol符号 text文字 split分割(需date和time同时存在)
- display:"bottom",
- headResult: false,
- // date:true,
- // time:true,
- beginYear: new Date().getFullYear()-100,
- endYear:new Date().getFullYear()+100,
- startDate: '',
- save:function(){},
- cancel:function(){}
- },opt);
- function addZero(num){
- return num<10?"0"+num:String(num);
- }
- var d = new Date(),
- year = d.getFullYear(),
- month = d.getMonth() + 1,
- day = d.getDate(),
- hour = d.getHours(),
- minute = d.getMinutes(),
- beginYear = opt.beginYear,
- endYear = opt.endYear,
- yearData = [],
- monthData = [],
- // dayData = [],
- // hourData = [],
- // minuteData = [],
- lastValue = '',
- wheels = [
- {
- infinite: false,
- selected: addZero(year),
- label:"<span></span>",
- data: yearData
- }
- ];
- for(var i=beginYear;i<=endYear;i++){
- yearData.push({value:addZero(i),display:addZero(i)});
- }
- //月
- for(var i=1;i<=12;i++){
- monthData.push({value:addZero(i),display:addZero(i)});
- }
- //Label控制
- if(!opt.showLabel){
- for(var i=0;i<wheels.length;i++){
- delete wheels[i].label;
- }
- }else{
- if(opt.labelType == 'text'){
- var text = ['年','月','日','时','分','秒']
- for(var i=0;i<wheels.length;i++){
- wheels[i].label = text[i];
- }
- }
- if(opt.labelType == 'split' && opt.date && opt.time){
- for(var i=0;i<wheels.length;i++){
- delete wheels[i].label;
- }
- wheels[2].label='<div class="p-select-time-split-1"></div>';
- }
- }
- //判断闰年
- function isLeapYear(year) {
- if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
- return true;
- } else {
- return false;
- }
- }
- //判断大小月
- function bigSmallMonth(dayObj,year,month){
- var smallMonth = (month=="04" || month=="06" || month=="09" || month=="11");
- dayObj.appendItem(
- [
- {value:"29",display:"29"},
- {value:"30",display:"30"},
- {value:"31",display:"31"}
- ]
- );
- if(smallMonth){
- dayObj.removeItem(["31"]);
- }else if(month=="02"){
- if(isLeapYear(year)){
- dayObj.removeItem(["30","31"]);
- }else{
- dayObj.removeItem(["29","30","31"]);
- }
- }
- }
- //获取结果
- function getLastVal(result) {
- if(result.length==5){
- lastValue = result[0].display+'-'+result[1].display+'-'+result[2].display+' '+result[3].display+':'+result[4].display;
- }else if (result.length==3){
- lastValue = result[0].display+'-'+result[1].display+'-'+result[2].display;
- } else{
- lastValue = result[0].display;
- }
- if(opt.headResult){
- var title = document.querySelector('.p-select-title');
- title.innerText = lastValue;
- }
- }
- //日期滚轮
- var p = new popoPicker(el,{
- wheels: wheels,
- scrollType:opt.scrollType,
- background: opt.background,
- display:opt.display,
- headTitle:opt.headResult?1:'',
- container:opt.container,
- init:function(rs){
- if(opt.date){
- var year = rs.result[0].value;
- bigSmallMonth(rs.scrollEvt[2],year);
- }
- getLastVal(rs.result)
- },
- getResult:function(rs){
- var n1 = opt.date && (rs.scrollIdx== 0 || rs.scrollIdx== 1);
- year = rs.result[0].value
- if(n1){
- bigSmallMonth(rs.scrollEvt[2],year);
- }
- getLastVal(rs.result)
- },
- save:function(rs, target){
- if(target.isInput){
- target.value = lastValue;
- }else{
- target.innerText = lastValue;
- }
- opt.save(lastValue, target);
- },
- cancel:function(){
- opt.cancel();
- }
- });
- //判断是否有自定义初始值
- var target = document.querySelectorAll(el);
- for(var i=0;i<target.length;i++){
- var item = target[i],
- isInput = item.localName == 'input' || item.localName == 'textarea',
- val = isInput?item.value:item.innerText;
- if(val){
- val = val.match(/\d+/g);
- item.setAttribute('data-value',val);
- }
- }
- };
- window.popoPicker = popoPicker;
- window.popoDateTime = popoDateTime;
- window.popoDateT = popoDateT;
- window.popoDate = popoDate;
- window.popoYear = popoYear;
- })(window,document);
|