/* plugin: zepto.mdatertimer author: 吕大豹 desc: 移动端日期时间选择控件 version: 1.0 */ (function ($) { /*tap event*/ !function(a){var b={},c={};c.attachEvent=function(b,c,d){return"addEventListener"in a?b.addEventListener(c,d,!1):void 0},c.fireFakeEvent=function(a,b){return document.createEvent?a.target.dispatchEvent(c.createEvent(b)):void 0},c.createEvent=function(b){if(document.createEvent){var c=a.document.createEvent("HTMLEvents");return c.initEvent(b,!0,!0),c.eventName=b,c}},c.getRealEvent=function(a){return a.originalEvent&&a.originalEvent.touches&&a.originalEvent.touches.length?a.originalEvent.touches[0]:a.touches&&a.touches.length?a.touches[0]:a};var d=[{test:("propertyIsEnumerable"in a||"hasOwnProperty"in document)&&(a.propertyIsEnumerable("ontouchstart")||document.hasOwnProperty("ontouchstart")),events:{start:"touchstart",move:"touchmove",end:"touchend"}},{test:a.navigator.msPointerEnabled,events:{start:"MSPointerDown",move:"MSPointerMove",end:"MSPointerUp"}},{test:a.navigator.pointerEnabled,events:{start:"pointerdown",move:"pointermove",end:"pointerup"}}];b.options={eventName:"tap",fingerMaxOffset:11};var e,f,g,h,i={};e=function(a){return c.attachEvent(document.body,h[a],g[a])},g={start:function(a){a=c.getRealEvent(a),i.start=[a.pageX,a.pageY],i.offset=[0,0]},move:function(a){return i.start||i.move?(a=c.getRealEvent(a),i.move=[a.pageX,a.pageY],void(i.offset=[Math.abs(i.move[0]-i.start[0]),Math.abs(i.move[1]-i.start[1])])):!1},end:function(d){if(d=c.getRealEvent(d),i.offset[0]b?-1:1);k=c/k;gb&&10>h)){this.directionLocked||this.options.freeScroll||(this.directionLocked=b>h+this.options.directionLockThreshold?"h":h>=b+this.options.directionLockThreshold?"v":"n");if("h"==this.directionLocked){if("vertical"==this.options.eventPassthrough)a.preventDefault();else if("horizontal"==this.options.eventPassthrough){this.initiated=!1;return}e=0}else if("v"==this.directionLocked){if("horizontal"== this.options.eventPassthrough)a.preventDefault();else if("vertical"==this.options.eventPassthrough){this.initiated=!1;return}c=0}c=this.hasHorizontalScroll?c:0;e=this.hasVerticalScroll?e:0;a=this.x+c;b=this.y+e;if(0c?1:0;this.directionY=0e?1:0;this.moved||this._execEvent("scrollStart");this.moved=!0;this._translate(a, b);300c&&100>h&&100>g)this._execEvent("flick");else if(this.options.momentum&&300>c&&(b=this.hasHorizontalScroll?d.momentum(this.x,this.startX,c,this.maxScrollX,this.options.bounce?this.wrapperWidth:0,this.options.deceleration):{destination:e,duration:0},c=this.hasVerticalScroll?d.momentum(this.y,this.startY,c,this.maxScrollY, this.options.bounce?this.wrapperHeight:0,this.options.deceleration):{destination:k,duration:0},e=b.destination,k=c.destination,b=f.max(b.duration,c.duration),this.isInTransition=1),this.options.snap&&(this.currentPage=l=this._nearestSnap(e,k),b=this.options.snapSpeed||f.max(f.max(f.min(f.abs(e-l.x),1E3),f.min(f.abs(k-l.y),1E3)),300),e=l.x,k=l.y,this.directionY=this.directionX=0,l=this.options.bounceEasing),e!=this.x||k!=this.y){if(0this.maxScrollX;this.hasVerticalScroll=this.options.scrollY&&0>this.maxScrollY;this.hasHorizontalScroll||(this.maxScrollX=0,this.scrollerWidth=this.wrapperWidth);this.hasVerticalScroll||(this.maxScrollY=0,this.scrollerHeight=this.wrapperHeight);this.directionY=this.directionX=this.endTime=0;this.wrapperOffset=d.offset(this.wrapper);this._execEvent("refresh");this.resetPosition()},on:function(a,b){this._events[a]||(this._events[a]=[]);this._events[a].push(b)},off:function(a, b){if(this._events[a]){var c=this._events[a].indexOf(b);-1b&&c++,0a&&e++,this.goToPage(c,e)):(c=this.x+f.round(this.hasHorizontalScroll?b:0),e=this.y+f.round(this.hasVerticalScroll?a:0),0-this.scrollerWidth;){this.pages[a]=[];for(l=b=0;l>-this.scrollerHeight;)this.pages[a][b]={x:f.max(g,this.maxScrollX),y:f.max(l,this.maxScrollY),width:e,height:m,cx:g-d,cy:l-h},l-=m,b++;g-=e;a++}else for(m=this.options.snap,b=m.length,e=-1;athis.maxScrollX&&c++}this.goToPage(this.currentPage.pageX||0,this.currentPage.pageY||0,0);0===this.options.snapThreshold%1?this.snapThresholdY=this.snapThresholdX=this.options.snapThreshold:(this.snapThresholdX= f.round(this.pages[this.currentPage.pageX][this.currentPage.pageY].width*this.options.snapThreshold),this.snapThresholdY=f.round(this.pages[this.currentPage.pageX][this.currentPage.pageY].height*this.options.snapThreshold))}});this.on("flick",function(){var a=this.options.snapSpeed||f.max(f.max(f.min(f.abs(this.x-this.startX),1E3),f.min(f.abs(this.y-this.startY),1E3)),300);this.goToPage(this.currentPage.pageX+this.directionX,this.currentPage.pageY+this.directionY,a)})},_nearestSnap:function(a,b){if(!this.pages.length)return{x:0, y:0,pageX:0,pageY:0};var c=0,e=this.pages.length,d=0;if(f.abs(a-this.absStartX)=this.pages[c][0].cx){a=this.pages[c][0].x;break}for(e=this.pages[c].length;d=this.pages[0][d].cy){b=this.pages[0][d].y;break}c==this.currentPage.pageX&&(c+=this.directionX,0>c?c=0:c>=this.pages.length&&(c=this.pages.length- 1),a=this.pages[c][0].x);d==this.currentPage.pageY&&(d+=this.directionY,0>d?d=0:d>=this.pages[0].length&&(d=this.pages[0].length-1),b=this.pages[0][d].y);return{x:a,y:b,pageX:c,pageY:d}},goToPage:function(a,b,c,d){d=d||this.options.bounceEasing;a>=this.pages.length?a=this.pages.length-1:0>a&&(a=0);b>=this.pages[a].length?b=this.pages[a].length-1:0>b&&(b=0);var g=this.pages[a][b].x,h=this.pages[a][b].y;c=void 0===c?this.options.snapSpeed||f.max(f.max(f.min(f.abs(g-this.x),1E3),f.min(f.abs(h-this.y), 1E3)),300):c;this.currentPage={x:g,y:h,pageX:a,pageY:b};this.scrollTo(g,h,c,d)},next:function(a,b){var c=this.currentPage.pageX,d=this.currentPage.pageY;c++;c>=this.pages.length&&this.hasVerticalScroll&&(c=0,d++);this.goToPage(c,d,a,b)},prev:function(a,b){var c=this.currentPage.pageX,d=this.currentPage.pageY;c--;0>c&&this.hasVerticalScroll&&(c=0,d--);this.goToPage(c,d,a,b)},_initKeys:function(a){a={pageUp:33,pageDown:34,end:35,home:36,left:37,up:38,right:39,down:40};var b;if("object"==typeof this.options.keyBindings)for(b in this.options.keyBindings)"string"== typeof this.options.keyBindings[b]&&(this.options.keyBindings[b]=this.options.keyBindings[b].toUpperCase().charCodeAt(0));else this.options.keyBindings={};for(b in a)this.options.keyBindings[b]=this.options.keyBindings[b]||a[b];d.addEvent(g,"keydown",this);this.on("destroy",function(){d.removeEvent(g,"keydown",this)})},_key:function(a){if(this.enabled){var b=this.options.snap,c=b?this.currentPage.pageX:this.x,e=b?this.currentPage.pageY:this.y,g=d.getTime(),h=this.keyTime||0,n;this.options.useTransition&& this.isInTransition&&(n=this.getComputedPosition(),this._translate(f.round(n.x),f.round(n.y)),this.isInTransition=!1);this.keyAcceleration=200>g-h?f.min(this.keyAcceleration+.25,50):0;switch(a.keyCode){case this.options.keyBindings.pageUp:this.hasHorizontalScroll&&!this.hasVerticalScroll?c+=b?1:this.wrapperWidth:e+=b?1:this.wrapperHeight;break;case this.options.keyBindings.pageDown:this.hasHorizontalScroll&&!this.hasVerticalScroll?c-=b?1:this.wrapperWidth:e-=b?1:this.wrapperHeight;break;case this.options.keyBindings.end:c= b?this.pages.length-1:this.maxScrollX;e=b?this.pages[0].length-1:this.maxScrollY;break;case this.options.keyBindings.home:e=c=0;break;case this.options.keyBindings.left:c+=b?-1:5+this.keyAcceleration>>0;break;case this.options.keyBindings.up:e+=b?1:5+this.keyAcceleration>>0;break;case this.options.keyBindings.right:c-=b?-1:5+this.keyAcceleration>>0;break;case this.options.keyBindings.down:e-=b?1:5+this.keyAcceleration>>0;break;default:return}b?this.goToPage(c,e):(0=p?(g.isAnimating=!1,g._translate(a,b),g.resetPosition(g.options.bounceTime)||g._execEvent("scrollEnd")):(q=(q-m)/c,r=e(q),q=(a-n)*r+n,r=(b-l)*r+l,g._translate(q,r),g.isAnimating&&u(f),3==g.options.probeType&&g._execEvent("scroll"))}var g=this,n=this.x,l=this.y,m=d.getTime(), p=m+c;this.isAnimating=!0;f()},handleEvent:function(a){switch(a.type){case "touchstart":case "pointerdown":case "MSPointerDown":case "mousedown":this._start(a);break;case "touchmove":case "pointermove":case "MSPointerMove":case "mousemove":this._move(a);break;case "touchend":case "pointerup":case "MSPointerUp":case "mouseup":case "touchcancel":case "pointercancel":case "MSPointerCancel":case "mousecancel":this._end(a);break;case "orientationchange":case "resize":this._resize();break;case "transitionend":case "webkitTransitionEnd":case "oTransitionEnd":case "MSTransitionEnd":this._transitionEnd(a); break;case "wheel":case "DOMMouseScroll":case "mousewheel":this._wheel(a);break;case "keydown":this._key(a);break;case "click":a._constructed||(a.preventDefault(),a.stopPropagation())}}};t.prototype={handleEvent:function(a){switch(a.type){case "touchstart":case "pointerdown":case "MSPointerDown":case "mousedown":this._start(a);break;case "touchmove":case "pointermove":case "MSPointerMove":case "mousemove":this._move(a);break;case "touchend":case "pointerup":case "MSPointerUp":case "mouseup":case "touchcancel":case "pointercancel":case "MSPointerCancel":case "mousecancel":this._end(a)}}, destroy:function(){this.options.interactive&&(d.removeEvent(this.indicator,"touchstart",this),d.removeEvent(this.indicator,d.prefixPointerEvent("pointerdown"),this),d.removeEvent(this.indicator,"mousedown",this),d.removeEvent(g,"touchmove",this),d.removeEvent(g,d.prefixPointerEvent("pointermove"),this),d.removeEvent(g,"mousemove",this),d.removeEvent(g,"touchend",this),d.removeEvent(g,d.prefixPointerEvent("pointerup"),this),d.removeEvent(g,"mouseup",this));this.options.defaultScrollbars&&this.wrapper.parentNode.removeChild(this.wrapper)}, _start:function(a){var b=a.touches?a.touches[0]:a;a.preventDefault();a.stopPropagation();this.transitionTime();this.initiated=!0;this.moved=!1;this.lastPointX=b.pageX;this.lastPointY=b.pageY;this.startTime=d.getTime();this.options.disableTouch||d.addEvent(g,"touchmove",this);this.options.disablePointer||d.addEvent(g,d.prefixPointerEvent("pointermove"),this);this.options.disableMouse||d.addEvent(g,"mousemove",this);this.scroller._execEvent("beforeScrollStart")},_move:function(a){var b=a.touches?a.touches[0]: a,c,e,f=d.getTime();this.moved||this.scroller._execEvent("scrollStart");this.moved=!0;c=b.pageX-this.lastPointX;this.lastPointX=b.pageX;e=b.pageY-this.lastPointY;this.lastPointY=b.pageY;this._pos(this.x+c,this.y+e);1==this.scroller.options.probeType&&300this.maxBoundaryX?"scale"==this.options.shrink?(this.width=f.max(this.indicatorWidth- (a-this.maxPosX),8),this.indicatorStyle.width=this.width+"px",a=this.maxPosX+this.indicatorWidth-this.width):a=this.maxBoundaryX:"scale"==this.options.shrink&&this.width!=this.indicatorWidth&&(this.width=this.indicatorWidth,this.indicatorStyle.width=this.width+"px"),bthis.maxBoundaryY?"scale"==this.options.shrink?(this.height=f.max(this.indicatorHeight- 3*(b-this.maxPosY),8),this.indicatorStyle.height=this.height+"px",b=this.maxPosY+this.indicatorHeight-this.height):b=this.maxBoundaryY:"scale"==this.options.shrink&&this.height!=this.indicatorHeight&&(this.height=this.indicatorHeight,this.indicatorStyle.height=this.height+"px"));this.x=a;this.y=b;this.scroller.options.useTransform?this.indicatorStyle[d.style.transform]="translate("+a+"px,"+b+"px)"+this.scroller.translateZ:(this.indicatorStyle.left=a+"px",this.indicatorStyle.top=b+"px")},_pos:function(a, b){0>a?a=0:a>this.maxPosX&&(a=this.maxPosX);0>b?b=0:b>this.maxPosY&&(b=this.maxPosY);a=this.options.listenX?f.round(a/this.sizeRatioX):this.scroller.x;b=this.options.listenY?f.round(b/this.sizeRatioY):this.scroller.y;this.scroller.scrollTo(a,b)},fade:function(a,b){if(!b||this.visible){clearTimeout(this.fadeTimeout);this.fadeTimeout=null;var c=a?250:500,e=a?0:300;this.wrapperStyle[d.style.transitionDuration]=c+"ms";this.fadeTimeout=setTimeout(function(a){this.wrapperStyle.opacity=a;this.visible=+a}.bind(this, a?"1":"0"),e)}}};p.utils=d;"undefined"!=typeof module&&module.exports?module.exports=p:g.IScroll=p})(window,document,Math); $.fn.mdatetimer = function(opts){ var defaults = { mode : 1, //时间选择器模式:1:年月日,2:年月日时分(24小时),3:年月日时分(12小时),4:年月日时分秒 format : 2, //时间格式化方式:1:2015年06月10日 17时30分46秒,2:2015-05-10 17:30:46 years : [2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017], //年份,数组或字符串'2000~2015' nowbtn : true, onOk : null, onCancel : null, }; var option = $.extend(defaults, opts); //通用函数 var F = { //计算某年某月有多少天 getDaysInMonth : function(year, month){ return new Date(year, month+1, 0).getDate(); }, getMonth : function(m){ return ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二'][m]; }, //计算年某月的最后一天日期 getLastDayInMonth : function(year, month){ return new Date(year, month, this.getDaysInMonth(year, month)); }, //小于10的数字前加0 preZero : function(num){ num = parseInt(num); if(num<10){ return '0'+num; } else{ return num; } }, formatDate : function(year, month, day, hour, minute, second){ month = F.preZero(month+1); day = F.preZero(day); hour = F.preZero(hour); minute = F.preZero(minute); if(option.mode!=3){ second = F.preZero(second); } else{ //可能点“现在”取时间,需根据时间判断上下午 if(second == 'am'){ second = '上午'; } else if(second == 'pm'){ second = '下午'; } else{ //传入的是秒数 if(hour<12){ second = '上午'; } else{ second = '下午'; } } } var result = ''; if(option.format==1){ result += year+'年'+month+'月'+day+'日'; if(option.mode!=1 && hour){ result += ' '+hour+'时'+minute+'分'; if(option.mode!=2){ if(!isNaN(parseInt(second))){ result += second+'秒'; } else{ result += ' '+second; } } } } else{ result += year+'-'+month+'-'+day; if(option.mode!=1 && hour){ result += ' '+hour+':'+minute+''; if(option.mode!=2){ if(!isNaN(parseInt(second))){ result += ':'+second+''; } else{ result += ' '+second; } } } } return result; }, getDateValue : function(value){ if(option.format==2){ return new Date(value); } else{ var array = value.replace(/\D ?/g,',').slice(0,-1).split(','); if(array.length==7){ //带有“上午、下午” if(value.indexOf('上午')>=0){ return new Date(array[0], array[1]-1, array[2], array[3], array[4]); } else{ return new Date(array[0], array[1]-1, array[2], parseInt(array[3])+12, array[4]); } } else{ return new Date(array[0], array[1]-1, array[2], array[3], array[4], array[5]); } } } } //滑动配置项 var scrollConf = { snap : 'li', snapSpeed: 600, probeType : 1, tap : true }; var input = $(this), itemHeight = 40; var picker = { init : function(){ var _this = this; _this.renderHTML(); var container = $('.mt_poppanel'), mpYear = $('.mt_year', container), mpMonth = $('.mt_month', container), mpDay = $('.mt_day', container), mpHour = $('.mt_hour', container), mpMinute = $('.mt_minute', container), mpSecond = $('.mt_second', container); //初始化year var defaultDate = input.val()!='' ? F.getDateValue(input.val()) : new Date(), dYear = defaultDate.getFullYear(), dMonth = defaultDate.getMonth(), dDate = defaultDate.getDate(), dHour = defaultDate.getHours(), dMinute = defaultDate.getMinutes(), dSecond = defaultDate.getSeconds(); var yearStr = ''; for(var i=0; i'+y+'年'; } yearStr += '
  • '; mpYear.find('ul').append(yearStr); //初始化month var monthStr = ''; for(var j=1; j<=12; j++){ var sel = j==dMonth ? 'selected' : ''; monthStr += '
  • '+F.preZero(j)+'月
  • '; } monthStr += '
  • '; mpMonth.find('ul').append(monthStr); //初始化day var dayStr = ''; var defaultDays = F.getDaysInMonth(dYear, dMonth); for(var k=1; k<=defaultDays; k++){ var sel = k==dDate ? 'selected' : ''; dayStr += '
  • '+F.preZero(k)+'日
  • '; } dayStr += '
  • '; mpDay.find('ul').append(dayStr); if(option.mode==1){ //把不该显示的删除掉 $('.mt_body', container).css('height', 200); $('.mt_year, .mt_month, .mt_day', container).css('height', '100%'); $('.mt_sepline, .mt_hour, .mt_minute, .mt_second, .mt_indicate.cate2', container).remove(); } if(!option.nowbtn){ $('.mt_setnow', container).remove(); $('.mt_cancel, .mt_ok', container).css('float', 'none'); $('.mt_cancel', container).css('border-right', '1px solid #4eccc4'); } document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false); //初始化scroll var elHeight = itemHeight; var yearScroll = new IScroll('.mt_year', scrollConf); yearScroll.on('scroll', function(){ _this.updateSelected(mpYear, this); }); yearScroll.on('scrollEnd', function(){ _this.updateSelected(mpYear, this); _this.updateDay(mpYear, mpMonth, mpDay); }); var monthScroll = new IScroll('.mt_month', scrollConf); monthScroll.on('scroll', function(){ _this.updateSelected(mpMonth, this); }); monthScroll.on('scrollEnd', function(){ _this.updateSelected(mpMonth, this); _this.updateDay(mpYear, mpMonth, mpDay); }); var dayScroll = new IScroll('.mt_day', scrollConf); dayScroll.on('scroll', function(){ _this.updateSelected(mpDay, this); }); dayScroll.on('scrollEnd', function(){ _this.updateSelected(mpDay, this); }); this.yearScroll = yearScroll; this.monthScroll = monthScroll; this.dayScroll = dayScroll; //初始化时分秒 //初始化hour if(option.mode != 1){ var hourStr = ''; var hourcount = option.mode == 3 ? 12 : 24; for(var l=1; l<=hourcount; l++){ var sel = l==dHour ? 'selected' : ''; hourStr += '
  • '+F.preZero(l)+'时
  • '; } hourStr += '
  • '; mpHour.find('ul').append(hourStr); var hourScroll = new IScroll('.mt_hour', scrollConf); hourScroll.on('scroll', function(){ _this.updateSelected(mpHour, this); }); hourScroll.on('scrollEnd', function(){ _this.updateSelected(mpHour, this); }); this.hourScroll = hourScroll; //初始化minute var minuteStr = ''; for(var m=0; m<=60; m++){ var sel = m==dMinute ? 'selected' : ''; minuteStr += '
  • '+F.preZero(m)+'分
  • '; } minuteStr += '
  • '; mpMinute.find('ul').append(minuteStr); var minuteScroll = new IScroll('.mt_minute', scrollConf); minuteScroll.on('scroll', function(){ _this.updateSelected(mpMinute, this); }); minuteScroll.on('scrollEnd', function(){ _this.updateSelected(mpMinute, this); }); this.minuteScroll = minuteScroll; //初始化second var secondStr = ''; if(option.mode==4){ for(var n=0; n<=60; n++){ var sel = n==dSecond ? 'selected' : ''; secondStr += '
  • '+F.preZero(n)+'秒
  • '; } } else if(option.mode==3){ var sel1 = dHour<=12 ? 'selected' : ''; var sel2 = dHour>=12 ? 'selected' : ''; secondStr += '
  • 上午
  • 下午
  • '; } secondStr += '
  • '; mpSecond.find('ul').append(secondStr); var secondScroll = new IScroll('.mt_second', scrollConf); secondScroll.on('scroll', function(){ _this.updateSelected(mpSecond, this); }); secondScroll.on('scrollEnd', function(){ _this.updateSelected(mpSecond, this); }); this.secondScroll = secondScroll; if(option.mode==2 || option.mode==3){ $('.mt_second .mt_note').html(' '); } } //初始化点击input事件 input.on('tap', function(){ if(container.hasClass('show')){ _this.hidePanel(); } else{ _this.showPanel(); var year = mpYear.find('.selected').data('year'); var month = mpMonth.find('.selected').data('month'); var day = mpDay.find('.selected').data('day'); var hour = mpHour.find('.selected').data('hour'); var minute = mpMinute.find('.selected').data('minute'); var second = mpSecond.find('.selected').data('second'); _this.setValue(year, month, day, hour, minute, second); } }); //初始化点击li mpYear.on('tap', 'li', function(){ _this.checkYear($(this)); }); mpMonth.on('tap', 'li', function(){ _this.checkMonth($(this)); }); mpDay.on('tap', 'li', function(){ _this.checkDay($(this)); }); if(option.mode != 1){ mpHour.on('tap', 'li', function(){ _this.checkHour($(this)); }); mpMinute.on('tap', 'li', function(){ _this.checkMinute($(this)); }); mpSecond.on('tap', 'li', function(){ _this.checkSecond($(this)); }); } //初始化点击事件 $('.mt_ok', container).on('tap', function(){ var year = mpYear.find('.selected').data('year'); var month = mpMonth.find('.selected').data('month'); var day = mpDay.find('.selected').data('day'); var hour = mpHour.find('.selected').data('hour'); var minute = mpMinute.find('.selected').data('minute'); var second = mpSecond.find('.selected').data('second'); input.val(F.formatDate(year, month, day, hour, minute, second)); _this.hidePanel(); option.onOk && typeof option.onOk=='function' && option.onOk(container); }); $('.mt_cancel', container).on('tap', function(){ _this.hidePanel(); option.onCancel && typeof option.onCancel=='function' && option.onCancel(container); }); $('.mt_setnow', container).on('tap', function(){ var n = new Date(); input.val(F.formatDate(n.getFullYear(), n.getMonth(), n.getDate(), n.getHours(), n.getMinutes(), n.getSeconds())); _this.hidePanel(); }); $('.mt_mask').on('tap', function(){ _this.hidePanel(); }); //初始化原有的数据 this.setValue(dYear, dMonth, dDate, dHour, dMinute, dSecond); }, renderHTML : function(){ var stime = option.timeStart + ':00'; var etime = option.timeStart + option.timeNum + ':00'; var html = '

    请选择时间

    • 选择年份
    • 选择月份
    • 选择日期
    • 选择时
    • 选择分
    • 选择秒
    '; $(document.body).append(html); }, updateSelected : function(container, iscroll){ var index = (-iscroll.y) / itemHeight + 2; var current = container.find('li').eq(index); current.addClass('selected').siblings().removeClass('selected'); }, showPanel : function(container){ $('.mt_poppanel, .mt_mask').addClass('show'); }, hidePanel : function(){ $('.mt_poppanel, .mt_mask').removeClass('show'); }, setValue : function(y, m, d, h, min, s){ var yearItem = $('.mt_year li[data-year="'+y+'"]'), monthItem = $('.mt_month li[data-month="'+m+'"]'), dayItem = $('.mt_day li[data-day="'+d+'"]'), hourItem = $('.mt_hour li[data-hour="'+h+'"]'), minuteItem = $('.mt_minute li[data-minute="'+min+'"]'), secondItem = $('.mt_second li[data-second="'+s+'"]'); if(option.mode==3){ if(h-12>0){ s = 'pm'; hourItem = $('.mt_hour li[data-hour="'+(h-12)+'"]'); } else{ s = 'am'; } secondItem = $('.mt_second li[data-second="'+s+'"]'); } this.checkYear(yearItem); this.checkMonth(monthItem); this.checkDay(dayItem); if(option.mode!=1){ this.checkHour(hourItem); this.checkMinute(minuteItem); this.checkSecond(secondItem); } }, //滚动的时候动态调节日期,用于计算闰年的日期数 updateDay : function(mpYear, mpMonth, mpDay){ var _this = this; var checkedYear = mpYear.find('li.selected').data('year'); var checkedMonth = mpMonth.find('li.selected').data('month'); var checkedDay = mpDay.find('li.selected').data('day'); var days = F.getDaysInMonth(checkedYear, checkedMonth); var dayStr = '
  • 选择日期
  • '; for(var k=1; k<=days; k++){ var sel = k==checkedDay ? 'selected' : ''; dayStr += '
  • '+F.preZero(k)+'日
  • '; } dayStr += '
  • '; mpDay.find('ul').html(dayStr); //内容改变后,iscroll的滚动会发生错误,所以在此将日期scorll重新初始化一遍 this.dayScroll.destroy(); this.dayScroll = new IScroll('.mt_day', scrollConf); this.dayScroll.on('scroll', function(){ _this.updateSelected(mpDay, this); }); this.dayScroll.on('scrollEnd', function(){ _this.updateSelected(mpDay, this); }); //然后给day重新选择 setTimeout(function(){ var dayEl = mpDay.find('li[data-day="'+checkedDay+'"]'); if(dayEl.length>0){ _this.checkDay(dayEl); } },10); }, checkYear : function(el){ if(el.text()=='')return; var target = el.prev('li').prev('li'); this.yearScroll.scrollToElement(target[0]); }, checkMonth : function(el){ if(el.text()=='')return; var target = el.prev('li').prev('li'); this.monthScroll.scrollToElement(target[0]); }, checkDay : function(el){ if(el.text()=='')return; var target = el.prev('li').prev('li'); this.dayScroll.scrollToElement(target[0]); }, checkHour : function(el){ if(el.text()=='')return; var target = el.prev('li').prev('li'); this.hourScroll.scrollToElement(target[0]); }, checkMinute : function(el){ if(el.text()=='')return; var target = el.prev('li').prev('li'); this.minuteScroll.scrollToElement(target[0]); }, checkSecond : function(el){ if(el.text()=='')return; if(option.mode<3)return; var target = el.prev('li').prev('li'); this.secondScroll.scrollToElement(target[0]); } } picker.init(); return picker; } return $.fn.mdatetimer; })(Zepto); //www.sucaijiayuan.com