//datePicker日期控件 v1.0 //var calendar = new datePicker(); //calendar.init({ // 'trigger': '#demo1', /*选择器,触发弹出插件*/ // 'type': 'date',/*date 调出日期选择 datetime 调出日期时间选择 time 调出时间选择 ym 调出年月选择*/ // 'minDate':'1900-1-1',/*最小日期*/ // 'maxDate':new Date().getFullYear()+'-'+(new Date().getMonth()+1)+'-'+new Date().getDate(),/*最大日期*/ // 'onSubmit':function(){/*确认时触发事件*/ // var theSelectData=calendar.value; // }, // 'onClose':function(){/*取消时触发事件*/ // } //}); window.datePicker = (function() { var MobileCalendar = function() { this.gearDate; this.minY = 1900; this.minM = 1; this.minD = 1; this.maxY = 2099; this.maxM = 12; this.maxD = 31; this.value=""; }; var cssHtm='.gearYM,.gearDate,.gearDatetime,.gearTime{font-family:Microsoft Yahei;font-size:10px;background-color:rgba(0,0,0,0);display:block;/*position:fixed;*/top:0;left:0;width:100%;height:100%;z-index:9900;overflow:hidden;-webkit-animation-fill-mode:both;animation-fill-mode:both}.date_ctrl{vertical-align:middle;background-color:#d5d8df;color:#000;margin:0;height:auto;width:100%;position:absolute;left:0;bottom:0;z-index:9901;overflow:hidden;-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0)}.slideInUp{animation:slideInUp .3s ease;-webkit-animation:slideInUp .3s ease;}@-webkit-keyframes slideInUp{from{-webkit-transform:translate3d(0,100%,0);transform:translate3d(0,100%,0)}to{-webkit-transform:translate3d(0,0,0);transform:translate3d(0,0,0)}}.ym_roll,.date_roll,.datetime_roll,.time_roll{display:-webkit-box;width:100%;height:auto;overflow:hidden;font-weight:bold;background-color:transparent;-webkit-mask:-webkit-gradient(linear,0% 50%,0% 100%,from(#debb47),to(rgba(36,142,36,0)));-webkit-mask:-webkit-linear-gradient(top,#debb47 50%,rgba(36,142,36,0))}.ym_roll>div,.date_roll>div,.datetime_roll>div,.time_roll>div{font-size:2.3em;height:6em;float:left;background-color:transparent;position:relative;overflow:hidden;-webkit-box-flex:4}.ym_roll>div .gear,.date_roll>div .gear,.datetime_roll>div .gear,.time_roll>div .gear{width:100%;float:left;position:absolute;z-index:9902;margin-top:-6em}.date_roll_mask{-webkit-mask:-webkit-gradient(linear,0% 40%,0% 0%,from(#debb47),to(rgba(36,142,36,0)));-webkit-mask:-webkit-linear-gradient(bottom,#debb47 50%,rgba(36,142,36,0));padding:0 0 3em 0}.date_roll>div:nth-child(2){-webkit-box-flex:2}.date_roll>div:nth-child(1),.datetime_roll>div:nth-child(1){-webkit-box-flex:4}.datetime_roll>div:first-child{-webkit-box-flex:6}.datetime_roll>div:last-child{-webkit-box-flex:6}.date_grid{position:relative;top:2em;width:100%;height:2em;margin:0;box-sizing:border-box;z-index:0;border-top:1px solid #abaeb5;border-bottom:1px solid #abaeb5}.date_grid>div{color:#000;position:absolute;right:0;top:0;font-size:.8em;line-height:2.5em}.date_roll>div:nth-child(3) .date_grid>div{left:42%}.datetime_roll>div .date_grid>div{right:0}.datetime_roll>div:first-child .date_grid>div{left:auto;right:0%}.datetime_roll>div:last-child .date_grid>div{left:50%}.time_roll>div:nth-child(1) .date_grid>div{right:1em}.ym_roll>div:nth-child(1) .date_grid>div{right:.1em}.ym_roll>div .date_grid>div,.time_roll>div .date_grid>div{right:5em}.date_btn{color:#0575f2;font-size:30px;font-weight:bold;line-height:1em;text-align:center;padding:5px 20px;}.date_btn_box:before,.date_btn_box:after{content:"";position:absolute;height:1px;width:100%;display:block;background-color:#96979b;z-index:15;-webkit-transform:scaleY(0.33);transform:scaleY(0.33)}.date_btn_box{display:-webkit-box;-webkit-box-pack:justify;-webkit-box-align:stretch;background-color:#f1f2f4;position:relative}.date_btn_box:before{left:0;top:0;-webkit-transform-origin:50% 20%;transform-origin:50% 20%}.date_btn_box:after{left:0;bottom:0;-webkit-transform-origin:50% 70%;transform-origin:50% 70%}.date_roll>div:nth-child(1) .gear{text-indent:20%}.date_roll>div:nth-child(2) .gear{text-indent:-20%}.date_roll>div:nth-child(3) .gear{text-indent:-55%}.datetime_roll>div .gear{width:100%;text-indent:-25%}.datetime_roll>div:first-child .gear{text-indent:-10%}.datetime_roll>div:last-child .gear{text-indent:-50%}.ym_roll>div .gear,.time_roll>div .gear{width:100%;text-indent:-70%}.ym_roll>div:nth-child(1) .gear,.time_roll>div:nth-child(1) .gear{width:100%;text-indent:20%} .time_roll>div:nth-child(2) .gear{width:100%;text-indent:-20%} .time_roll>div:nth-child(3) .gear{width:100%;text-indent:-55%}.tooth{height:2em;line-height:2em;text-align:center}'; var cssEle = document.createElement("style"); cssEle.type = "text/css"; cssEle.appendChild(document.createTextNode(cssHtm)); document.getElementsByTagName("head")[0].appendChild(cssEle); var isClear = "hidden=\"hidden\""; MobileCalendar.prototype = { init: function(params) { this.type = params.type; this.trigger = document.querySelector(params.trigger); if (this.trigger.getAttribute("data-lcalendar") != null) { var arr = this.trigger.getAttribute("data-lcalendar").split(','); var minArr = arr[0].split('-'); this.minY = ~~minArr[0]; this.minM = ~~minArr[1]; this.minD = ~~minArr[2]; var maxArr = arr[1].split('-'); this.maxY = ~~maxArr[0]; this.maxM = ~~maxArr[1]; this.maxD = ~~maxArr[2]; }; if (params.minDate) { var minArr = params.minDate.split('-'); this.minY = ~~minArr[0]; this.minM = ~~minArr[1]; this.minD = ~~minArr[2]; }; if (params.maxDate) { var maxArr = params.maxDate.split('-'); this.maxY = ~~maxArr[0]; this.maxM = ~~maxArr[1]; this.maxD = ~~maxArr[2]; }; if(params.isClear == 'true') isClear = ''; this.onClose= params.onClose; this.onSubmit= params.onSubmit; this.onChange= params.onChange; this.onClear = params.onClear; this.bindEvent(this.type); }, bindEvent: function(type) { var _self = this; var isTouched = false , isMoved = false; var pree; var pickerType = ""; //呼出日期插件 function popupDate(e) { pickerType = "date"; closeOther(); _self.gearDate = document.createElement("div"); _self.gearDate.className = "gearDate"; _self.gearDate.innerHTML = '
'; document.body.appendChild(_self.gearDate); dateCtrlInit(); var lcalendar_cancel = _self.gearDate.querySelector(".lcalendar_cancel"); lcalendar_cancel.addEventListener('touchstart', closeMobileCalendar); var lcalender_clear = _self.gearDate.querySelector(".lcalender_clear"); lcalender_clear.addEventListener('touchstart', clearValue); /*var lcalendar_finish = _self.gearDate.querySelector(".lcalendar_finish"); lcalendar_finish.addEventListener('touchstart', finishMobileDate);*/ var lcalendar_bg = _self.gearDate.querySelector(".date_bg"); lcalendar_bg.addEventListener('click', closeMobileCalendar); var date_yy = _self.gearDate.querySelector(".date_yy"); var date_mm = _self.gearDate.querySelector(".date_mm"); var date_dd = _self.gearDate.querySelector(".date_dd"); date_yy.addEventListener('touchstart', gearTouchStart); date_mm.addEventListener('touchstart', gearTouchStart); date_dd.addEventListener('touchstart', gearTouchStart); date_yy.addEventListener('touchmove', gearTouchMove); date_mm.addEventListener('touchmove', gearTouchMove); date_dd.addEventListener('touchmove', gearTouchMove); date_yy.addEventListener('touchend', gearTouchEnd); date_mm.addEventListener('touchend', gearTouchEnd); date_dd.addEventListener('touchend', gearTouchEnd); //------------------------------------------------------------- lcalendar_cancel.addEventListener('click', closeMobileCalendar); lcalender_clear.addEventListener('click', clearValue); //lcalendar_finish.addEventListener('click', finishMobileDate); date_yy.addEventListener('mousedown', gearTouchStart); date_mm.addEventListener('mousedown', gearTouchStart); date_dd.addEventListener('mousedown', gearTouchStart); date_yy.addEventListener('mousemove', gearTouchMove); date_mm.addEventListener('mousemove', gearTouchMove); date_dd.addEventListener('mousemove', gearTouchMove); date_yy.addEventListener('mouseup', gearTouchEnd); date_mm.addEventListener('mouseup', gearTouchEnd); date_dd.addEventListener('mouseup', gearTouchEnd); _self.gearDate.querySelector(".date_roll_mask").addEventListener('mouseleave', gearTouchOut); _self.gearDate.querySelector(".date_roll_mask").addEventListener('mouseup', gearTouchOut); } //初始化年月日插件默认值 function dateCtrlInit() { var date = new Date(); var dateArr = { yy: date.getYear(), mm: date.getMonth(), dd: date.getDate() - 1 }; if (/^\d{4}-\d{1,2}-\d{1,2}$/.test(_self.trigger.value)) { rs = _self.trigger.value.match(/(^|-)\d{1,4}/g); dateArr.yy = rs[0] - _self.minY; dateArr.mm = rs[1].replace(/-/g, "") - 1; dateArr.dd = rs[2].replace(/-/g, "") - 1; } else { dateArr.yy = dateArr.yy + 1900 - _self.minY; } _self.gearDate.querySelector(".date_yy").setAttribute("val", dateArr.yy); _self.gearDate.querySelector(".date_mm").setAttribute("val", dateArr.mm); _self.gearDate.querySelector(".date_dd").setAttribute("val", dateArr.dd); setDateGearTooth(); } //呼出年月插件 function popupYM(e) { pickerType = "ym"; closeOther(); _self.gearDate = document.createElement("div"); _self.gearDate.className = "gearDate"; _self.gearDate.innerHTML = ' '; document.body.appendChild(_self.gearDate); ymCtrlInit(); var lcalendar_cancel = _self.gearDate.querySelector(".lcalendar_cancel"); lcalendar_cancel.addEventListener('touchstart', closeMobileCalendar); var lcalender_clear = _self.gearDate.querySelector(".lcalender_clear"); lcalender_clear.addEventListener('touchstart', clearValue); /*var lcalendar_finish = _self.gearDate.querySelector(".lcalendar_finish"); lcalendar_finish.addEventListener('touchstart', finishMobileYM);*/ var lcalendar_bg = _self.gearDate.querySelector(".date_bg"); lcalendar_bg.addEventListener('click', closeMobileCalendar); var date_yy = _self.gearDate.querySelector(".date_yy"); var date_mm = _self.gearDate.querySelector(".date_mm"); date_yy.addEventListener('touchstart', gearTouchStart); date_mm.addEventListener('touchstart', gearTouchStart); date_yy.addEventListener('touchmove', gearTouchMove); date_mm.addEventListener('touchmove', gearTouchMove); date_yy.addEventListener('touchend', gearTouchEnd); date_mm.addEventListener('touchend', gearTouchEnd); //------------------------------------------------------------- lcalendar_cancel.addEventListener('click', closeMobileCalendar); lcalender_clear.addEventListener('click', clearValue); //lcalendar_finish.addEventListener('click', finishMobileYM); date_yy.addEventListener('mousedown', gearTouchStart); date_mm.addEventListener('mousedown', gearTouchStart); date_yy.addEventListener('mousemove', gearTouchMove); date_mm.addEventListener('mousemove', gearTouchMove); date_yy.addEventListener('mouseup', gearTouchEnd); date_mm.addEventListener('mouseup', gearTouchEnd); _self.gearDate.querySelector(".date_roll_mask").addEventListener('mouseleave', gearTouchOut); _self.gearDate.querySelector(".date_roll_mask").addEventListener('mouseup', gearTouchOut); } //初始化年月插件默认值 function ymCtrlInit() { var date = new Date(); var dateArr = { yy: date.getYear(), mm: date.getMonth() }; if (/^\d{4}-\d{1,2}$/.test(_self.trigger.value)) { rs = _self.trigger.value.match(/(^|-)\d{1,4}/g); dateArr.yy = rs[0] - _self.minY; dateArr.mm = rs[1].replace(/-/g, "") - 1; } else { dateArr.yy = dateArr.yy + 1900 - _self.minY; } _self.gearDate.querySelector(".date_yy").setAttribute("val", dateArr.yy); _self.gearDate.querySelector(".date_mm").setAttribute("val", dateArr.mm); setDateGearTooth(); } //呼出日期+时间插件 function popupDateTime(e) { pickerType = "datetime"; closeOther(); _self.gearDate = document.createElement("div"); _self.gearDate.className = "gearDate"; _self.gearDate.innerHTML = ' '; document.body.appendChild(_self.gearDate); dateTimeCtrlInit(); var lcalendar_cancel = _self.gearDate.querySelector(".lcalendar_cancel"); lcalendar_cancel.addEventListener('touchstart', closeMobileCalendar); var lcalender_clear = _self.gearDate.querySelector(".lcalender_clear"); lcalender_clear.addEventListener('touchstart', clearValue); /*var lcalendar_finish = _self.gearDate.querySelector(".lcalendar_finish"); lcalendar_finish.addEventListener('touchstart', finishMobileDateTime);*/ var lcalendar_bg = _self.gearDate.querySelector(".date_bg"); lcalendar_bg.addEventListener('click', closeMobileCalendar); var date_yy = _self.gearDate.querySelector(".date_yy"); var date_mm = _self.gearDate.querySelector(".date_mm"); var date_dd = _self.gearDate.querySelector(".date_dd"); var time_hh = _self.gearDate.querySelector(".time_hh"); var time_mm = _self.gearDate.querySelector(".time_mm"); var time_ss = _self.gearDate.querySelector(".time_ss"); date_yy.addEventListener('touchstart', gearTouchStart); date_mm.addEventListener('touchstart', gearTouchStart); date_dd.addEventListener('touchstart', gearTouchStart); time_hh.addEventListener('touchstart', gearTouchStart); time_mm.addEventListener('touchstart', gearTouchStart); time_ss.addEventListener('touchstart', gearTouchStart); date_yy.addEventListener('touchmove', gearTouchMove); date_mm.addEventListener('touchmove', gearTouchMove); date_dd.addEventListener('touchmove', gearTouchMove); time_hh.addEventListener('touchmove', gearTouchMove); time_mm.addEventListener('touchmove', gearTouchMove); time_ss.addEventListener('touchmove', gearTouchMove); date_yy.addEventListener('touchend', gearTouchEnd); date_mm.addEventListener('touchend', gearTouchEnd); date_dd.addEventListener('touchend', gearTouchEnd); time_hh.addEventListener('touchend', gearTouchEnd); time_mm.addEventListener('touchend', gearTouchEnd); time_ss.addEventListener('touchend', gearTouchEnd); //------------------------------------------------------------- lcalendar_cancel.addEventListener('click', closeMobileCalendar); lcalender_clear.addEventListener('click', clearValue); //lcalendar_finish.addEventListener('click', finishMobileDateTime); date_yy.addEventListener('mousedown', gearTouchStart); date_mm.addEventListener('mousedown', gearTouchStart); date_dd.addEventListener('mousedown', gearTouchStart); time_hh.addEventListener('mousedown', gearTouchStart); time_mm.addEventListener('mousedown', gearTouchStart); time_ss.addEventListener('mousedown', gearTouchStart); date_yy.addEventListener('mousemove', gearTouchMove); date_mm.addEventListener('mousemove', gearTouchMove); date_dd.addEventListener('mousemove', gearTouchMove); time_hh.addEventListener('mousemove', gearTouchMove); time_mm.addEventListener('mousemove', gearTouchMove); time_ss.addEventListener('mousemove', gearTouchMove); date_yy.addEventListener('mouseup', gearTouchEnd); date_mm.addEventListener('mouseup', gearTouchEnd); date_dd.addEventListener('mouseup', gearTouchEnd); time_hh.addEventListener('mouseup', gearTouchEnd); time_mm.addEventListener('mouseup', gearTouchEnd); time_ss.addEventListener('mouseup', gearTouchEnd); _self.gearDate.querySelector(".date_roll_mask").addEventListener('mouseleave', gearTouchOut); _self.gearDate.querySelector(".date_roll_mask").addEventListener('mouseup', gearTouchOut); } //初始化年月日时分插件默认值 function dateTimeCtrlInit() { var date = new Date(); var dateArr = { yy: date.getYear(), mm: date.getMonth(), dd: date.getDate() - 1, hh: date.getHours(), mi: date.getMinutes(), ss: date.getSeconds() }; if (/^\d{4}-\d{1,2}-\d{1,2}\s\d{2}:\d{2}$/.test(_self.trigger.value)) { rs = _self.trigger.value.match(/(^|-|\s|:)\d{1,4}/g); dateArr.yy = rs[0] - _self.minY; dateArr.mm = rs[1].replace(/-/g, "") - 1; dateArr.dd = rs[2].replace(/-/g, "") - 1; dateArr.hh = parseInt(rs[3].replace(/\s0?/g, "")); dateArr.mi = parseInt(rs[4].replace(/:0?/g, "")); dateArr.ss = parseInt(rs[5].replace(/:0?/g, "")); } else { dateArr.yy = dateArr.yy + 1900 - _self.minY; } _self.gearDate.querySelector(".date_yy").setAttribute("val", dateArr.yy); _self.gearDate.querySelector(".date_mm").setAttribute("val", dateArr.mm); _self.gearDate.querySelector(".date_dd").setAttribute("val", dateArr.dd); setDateGearTooth(); _self.gearDate.querySelector(".time_hh").setAttribute("val", dateArr.hh); _self.gearDate.querySelector(".time_mm").setAttribute("val", dateArr.mi); _self.gearDate.querySelector(".time_ss").setAttribute("val", dateArr.ss); setTimeGearTooth(); } //呼出时间插件 function popupTime(e) { pickerType = "time"; closeOther(); _self.gearDate = document.createElement("div"); _self.gearDate.className = "gearDate"; _self.gearDate.innerHTML = ' '; document.body.appendChild(_self.gearDate); timeCtrlInit(); var lcalendar_cancel = _self.gearDate.querySelector(".lcalendar_cancel"); lcalendar_cancel.addEventListener('touchstart', closeMobileCalendar); var lcalender_clear = _self.gearDate.querySelector(".lcalender_clear"); lcalender_clear.addEventListener('touchstart', clearValue); /*var lcalendar_finish = _self.gearDate.querySelector(".lcalendar_finish"); lcalendar_finish.addEventListener('touchstart', finishMobileTime);*/ var lcalendar_bg = _self.gearDate.querySelector(".date_bg"); lcalendar_bg.addEventListener('click', closeMobileCalendar); var time_hh = _self.gearDate.querySelector(".time_hh"); var time_mm = _self.gearDate.querySelector(".time_mm"); var time_ss = _self.gearDate.querySelector(".time_ss"); time_hh.addEventListener('touchstart', gearTouchStart); time_mm.addEventListener('touchstart', gearTouchStart); time_ss.addEventListener('touchstart', gearTouchStart); time_hh.addEventListener('touchmove', gearTouchMove); time_mm.addEventListener('touchmove', gearTouchMove); time_ss.addEventListener('touchmove', gearTouchMove); time_hh.addEventListener('touchend', gearTouchEnd); time_mm.addEventListener('touchend', gearTouchEnd); time_ss.addEventListener('touchend', gearTouchEnd); //------------------------------------------------------------- lcalendar_cancel.addEventListener('click', closeMobileCalendar); lcalender_clear.addEventListener('click', clearValue); //lcalendar_finish.addEventListener('click', finishMobileTime); time_hh.addEventListener('mousedown', gearTouchStart); time_mm.addEventListener('mousedown', gearTouchStart); time_ss.addEventListener('mousedown', gearTouchStart); time_hh.addEventListener('mousemove', gearTouchMove); time_mm.addEventListener('mousemove', gearTouchMove); time_ss.addEventListener('mousemove', gearTouchMove); time_hh.addEventListener('mouseup', gearTouchEnd); time_mm.addEventListener('mouseup', gearTouchEnd); time_ss.addEventListener('mouseup', gearTouchEnd); _self.gearDate.querySelector(".date_roll_mask").addEventListener('mouseleave', gearTouchOut); _self.gearDate.querySelector(".date_roll_mask").addEventListener('mouseup', gearTouchOut); } //初始化时分插件默认值 function timeCtrlInit() { var d = new Date(); var e = { hh: d.getHours(), mm: d.getMinutes(), ss: d.getSeconds() }; if (/^\d{2}:\d{2}$/.test(_self.trigger.value)) { rs = _self.trigger.value.match(/(^|:)\d{2}/g); e.hh = parseInt(rs[0].replace(/^0?/g, "")); e.mm = parseInt(rs[1].replace(/:0?/g, "")); e.ss = parseInt(rs[2].replace(/:0?/g, "")); } _self.gearDate.querySelector(".time_hh").setAttribute("val", e.hh); _self.gearDate.querySelector(".time_mm").setAttribute("val", e.mm); _self.gearDate.querySelector(".time_ss").setAttribute("val", e.ss); setTimeGearTooth(); } //重置日期节点个数 function setDateGearTooth() { var passY = _self.maxY - _self.minY + 1; var date_yy = _self.gearDate.querySelector(".date_yy"); var itemStr = ""; if (date_yy && date_yy.getAttribute("val")) { //得到年份的值 var yyVal = parseInt(date_yy.getAttribute("val")); //p 当前节点前后需要展示的节点个数 for (var p = 0; p <= passY - 1; p++) { itemStr += "