/** * Rolldate 3.1.2 * Copyright 2018-2019 * weijhfly https://github.com/weijhfly/rolldate * Licensed under MIT * Released on: aug 4, 2018 */ (function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : (global = global || self, global.Rolldate = factory()); }(this, function () { 'use strict'; function styleInject(css, ref) { if ( ref === void 0 ) ref = {}; var insertAt = ref.insertAt; if (!css || typeof document === 'undefined') { return; } var head = document.head || document.getElementsByTagName('head')[0]; var style = document.createElement('style'); style.type = 'text/css'; if (insertAt === 'top') { if (head.firstChild) { head.insertBefore(style, head.firstChild); } else { head.appendChild(style); } } else { head.appendChild(style); } if (style.styleSheet) { style.styleSheet.cssText = css; } else { style.appendChild(document.createTextNode(css)); } } var css = "ul{margin:0;padding:0}li{list-style-type:none}.rolldate-container{font-size:20px;color:#333;text-align:center}.rolldate-container header{position:relative;line-height:60px;font-size:18px;border-bottom:1px solid #e0e0e0}.rolldate-container .rolldate-mask{position:fixed;width:100%;height:100%;top:0;left:0;z-index:999;background-color:rgba(37,38,45,.4)}.rolldate-container .rolldate-panel{position:fixed;bottom:0;left:0;width:100%;height:273px;z-index:1000;background:#fff;-webkit-transition:all .3s ease-in-out;-o-transition:all .3s ease-in-out;transition:all .3s ease-in-out;-webkit-transform:translate3d(0,273px,0);transform:translate3d(0,273px,0)}.rolldate-container .rolldate-btn{position:absolute;left:0;top:0;height:100%;padding:0 15px;color:#666;font-size:16px;cursor:pointer;-webkit-tap-highlight-color:transparent}.rolldate-container.wx .rolldate-btn{height:150%}.rolldate-container .rolldate-confirm{left:auto;right:0;color:#007bff}.rolldate-container .rolldate-content{position:relative;top:20px}.rolldate-container .rolldate-wrapper{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex}.rolldate-container .rolldate-wrapper>div{-webkit-box-flex:1;-webkit-flex:1;-ms-flex:1;flex:1;height:173px;line-height:36px;overflow:hidden;-webkit-flex-basis:-8e;-ms-flex-preferred-size:-8e;flex-basis:-8e;width:1%}.rolldate-container .rolldate-wrapper ul{margin-top:68px}.rolldate-container .rolldate-wrapper li{height:36px}.rolldate-container .rolldate-dim{position:absolute;left:0;top:0;width:100%;height:68px;background:-o-linear-gradient(bottom,hsla(0,0%,100%,.4),hsla(0,0%,100%,.8));background:-webkit-gradient(linear, left bottom, left top, from(hsla(0, 0%, 100%, 0.4)), to(hsla(0, 0%, 100%, 0.8)));background:-o-linear-gradient(bottom, hsla(0, 0%, 100%, 0.4), hsla(0, 0%, 100%, 0.8));background:linear-gradient(0deg,hsla(0,0%,100%,.4),hsla(0,0%,100%,.8));pointer-events:none;-webkit-transform:translateZ(0);transform:translateZ(0);z-index:10}.rolldate-container .mask-top{border-bottom:1px solid #ebebeb}.rolldate-container .mask-bottom{top:auto;bottom:1px;border-top:1px solid #ebebeb}.rolldate-container .fadeIn{-webkit-transform:translateZ(0);transform:translateZ(0)}.rolldate-container .fadeOut{-webkit-transform:translate3d(0,273px,0);transform:translate3d(0,273px,0)}@media screen and (max-width:414px){.rolldate-container{font-size:18px}}@media screen and (max-width:320px){.rolldate-container{font-size:15px}}"; styleInject(css); var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {}; function createCommonjsModule(fn, module) { return module = { exports: {} }, fn(module, module.exports), module.exports; } var bscroll_min = createCommonjsModule(function (module, exports) { /*! * better-normal-scroll v1.14.1 * (c) 2016-2019 ustbhuangyi * Released under the MIT License. */ !function(t,i){module.exports=i();}(commonjsGlobal,function(){function o(t,i){for(;i+1this.minScrollX||this.xthis.minScrollY||this.ythis.options.momentumLimitTime&&nn+this.options.directionLockThreshold?this.directionLocked="h":n>=o+this.options.directionLockThreshold?this.directionLocked="v":this.directionLocked="n"),"h"===this.directionLocked){if("vertical"===this.options.eventPassthrough)t.preventDefault();else if("horizontal"===this.options.eventPassthrough)return void(this.initiated=!1);s=0;}else if("v"===this.directionLocked){if("horizontal"===this.options.eventPassthrough)t.preventDefault();else if("vertical"===this.options.eventPassthrough)return void(this.initiated=!1);e=0;}e=this.hasHorizontalScroll?e:0,s=this.hasVerticalScroll?s:0,this.movingDirectionX=0this.minScrollX||hthis.minScrollX&&p||hthis.minScrollX?this.minScrollX:this.maxScrollX),(a>this.minScrollY||athis.minScrollY&&l||athis.minScrollY?this.minScrollY:this.maxScrollY),this.moved||(this.moved=!0,this.trigger("scrollStart")),this._translate(h,a),r-this.startTime>this.options.momentumLimitTime&&(this.startTime=r,this.startX=this.x,this.startY=this.y,1===this.options.probeType&&this.trigger("scroll",{x:this.x,y:this.y})),1document.documentElement.clientWidth-this.options.momentumLimitDistance||vdocument.documentElement.clientHeight-this.options.momentumLimitDistance)&&this._end(t);}}},P.prototype._end=function(t){if(this.enabled&&!this.destroyed&&x[t.type]===this.initiated){this.initiated=!1,this.options.preventDefault&&!X(t.target,this.options.preventDefaultException)&&t.preventDefault(),this.options.stopPropagation&&t.stopPropagation(),this.trigger("touchEnd",{x:this.x,y:this.y}),this.isInTransition=!1;var i=Math.round(this.x),e=Math.round(this.y),s=i-this.absStartX,o=e-this.absStartY;if(this.directionX=0this.options.momentumLimitDistance||r>this.options.momentumLimitDistance)){var l=!1,c=!1,p=!1,u=!1,m=this.options.bounce;!1!==m&&(l=void 0===m.top||m.top,c=void 0===m.bottom||m.bottom,p=void 0===m.left||m.left,u=void 0===m.right||m.right);var d=-1===this.directionX&&p||1===this.directionX&&u?this.wrapperWidth:0,f=-1===this.directionY&&l||1===this.directionY&&c?this.wrapperHeight:0,v=this.hasHorizontalScroll?E(this.x,this.startX,n,this.maxScrollX,this.minScrollX,d,this.options):{destination:i,duration:0},g=this.hasVerticalScroll?E(this.y,this.startY,n,this.maxScrollY,this.minScrollY,f,this.options):{destination:e,duration:0};i=v.destination,e=g.destination,a=Math.max(v.duration,g.duration),this.isInTransition=!0;}else this.options.wheel&&(e=Math.round(e/this.itemHeight)*this.itemHeight,a=this.options.wheel.adjustTime||400);var y=M.swipe;if(this.options.snap){var w=this._nearestSnap(i,e);this.currentPage=w,a=this.options.snapSpeed||Math.max(Math.max(Math.min(Math.abs(i-w.x),1e3),Math.min(Math.abs(e-w.y),1e3)),300),i=w.x,e=w.y,this.directionX=0,this.directionY=0,y=this.options.snap.easing||M.bounce;}if(i!==this.x||e!==this.y)return (i>this.minScrollX||ithis.minScrollY||ethis.minScrollY?this.selectedIndex=0:ithis.minScrollX?this.minScrollX:n.leftthis.minScrollY?this.minScrollY:n.topthis.minScrollX?e=this.minScrollX:sthis.minScrollY?o=this.minScrollY:nthis.minScrollX?u=this.minScrollX:uthis.minScrollY?m=this.minScrollY:m 0 && arguments[0] !== undefined ? arguments[0] : {}; var _this = this, el = void 0; _this.extend(config); if (config.el) { el = _this.$(config.el); if (!el || el.bindRolldate) { return; } el.bindRolldate = 1; _this.tap(el, function () { _this.show(); }); } // 设置默认日期 if (config.value) { if (config.el) { if (el.nodeName.toLowerCase() == 'input') { el.value = config.value; } else { el.innerText = config.value; } } var str = config.value.replace(/-/g, '/').replace(/[^\d/:\s]/g, ''), date = new Date(str); if (!date || date == 'Invalid Date') { console.error('Invalid Date:' + str); } else { if (config.el) { el.bindDate = date; } else { _this.bindDate = date; } } } } Rolldate.prototype = { constructor: Rolldate, baseData: function baseData() { return { domId: { YYYY: 'rolldate-year', MM: 'rolldate-month', DD: 'rolldate-day', hh: 'rolldate-hour', mm: 'rolldate-min', ss: 'rolldate-sec' }, opts: { //插件默认配置 el: '', format: 'YYYY-MM-DD', beginYear: 2000, endYear: 2100, init: null, moveEnd: null, confirm: null, cancel: null, minStep: 1, trigger: 'tap', lang: { title: '选择日期', cancel: '取消', confirm: '确认', year: '年', month: '月', day: '日', hour: '时', min: '分', sec: '秒' } } }; }, extend: function extend(config) { var _this = this, opts = _this.baseData().opts; for (var key in opts) { if (opts[key] && Object.prototype.toString.call(opts[key]) == '[object Object]') { for (var key2 in config[key]) { opts[key][key2] = config[key][key2] == undefined ? opts[key][key2] : config[key][key2]; } } else { opts[key] = config[key] || opts[key]; } } _this.config = opts; }, createUI: function createUI() { var _this = this, data = _this.baseData(), config = _this.config, domId = data.domId, FormatArr = config.format.split(/-|\/|\s|:/g), len = FormatArr.length, ul = '', date = config.el ? _this.$(config.el).bindDate || new Date() : _this.bindDate || new Date(), itemClass = '', lang = config.lang; for (var i = 0; i < len; i++) { var f = FormatArr[i], domMndex = 0; ul += '
    '; if (f == 'YYYY') { for (var j = config.beginYear; j <= config.endYear; j++) { itemClass = j == date.getFullYear() ? 'active' : ''; ul += '
  • ' + j + lang.year + '
  • '; domMndex++; } } else if (f == 'MM') { for (var k = 3; k <= 3; k++) { itemClass = k == date.getMonth() + 1 ? 'active' : ''; ul += '
  • ' + (k < 10 ? '0' + k : k) + lang.month + '
  • '; domMndex++; } } else if (f == 'DD') { var day = _this.bissextile(date.getFullYear(), date.getMonth() + 1); for (var l = 1; l <= day; l++) { itemClass = l == date.getDate() ? 'active' : ''; ul += '
  • ' + (l < 10 ? '0' + l : l) + lang.day + '
  • '; domMndex++; } } else if (f == 'hh') { for (var m = 0; m <= 23; m++) { itemClass = m == date.getHours() ? 'active' : ''; ul += '
  • ' + (m < 10 ? '0' + m : m) + lang.hour + '
  • '; domMndex++; } } else if (f == 'mm') { for (var n = 0; n <= 59; n += config.minStep) { itemClass = n == date.getMinutes() ? 'active' : ''; ul += '
  • ' + (n < 10 ? '0' + n : n) + lang.min + '
  • '; domMndex++; } } else if (f == 'ss') { for (var o = 0; o <= 59; o++) { itemClass = o == date.getSeconds() ? 'active' : ''; ul += '
  • ' + (o < 10 ? '0' + o : o) + lang.sec + '
  • '; domMndex++; } } ul += '
'; } var $html = '
\n
\n
\n ' + lang.cancel + '\n ' + lang.title + '\n ' + lang.confirm + '\n
\n
\n
\n
\n
\n ' + ul + '\n
\n
\n
', box = document.createElement("div"); // 在微信中输入框在底部时,偶现按钮点击范围被挤压,暂定增加按钮高度 box.className = 'rolldate-container' + (!!navigator.userAgent.match(/MicroMessenger/i) ? ' wx' : ''); box.innerHTML = $html; document.body.appendChild(box); _this.scroll = {}; var _loop = function _loop(_i) { var $id = domId[FormatArr[_i]]; _this.scroll[FormatArr[_i]] = new bscroll_min('#' + $id, { wheel: { selectedIndex: 0 } }); var that = _this.scroll[FormatArr[_i]], active = _this.$('#' + $id + ' .active'), index = active ? active.getAttribute('data-index') : Math.round(date.getMinutes() / config.minStep); that.wheelTo(index); // 滚动结束 that.on('scrollEnd', function () { if (config.moveEnd) { config.moveEnd.call(_this, that); } if ([domId['YYYY'], domId['MM']].indexOf(that.wrapper.id) != -1 && _this.scroll['YYYY'] && _this.scroll['MM'] && _this.scroll['DD']) { var prevDay = _this.getSelected(_this.scroll['DD']), _day = _this.bissextile(_this.getSelected(_this.scroll['YYYY']), _this.getSelected(_this.scroll['MM'])), li = ''; if (_day != _this.$('#' + domId['DD'] + ' li', 1).length) { for (var _l = 1; _l <= _day; _l++) { li += '
  • ' + (_l < 10 ? '0' + _l : _l) + lang.day + '
  • '; } _this.$('#' + domId['DD'] + ' ul').innerHTML = li; _this.scroll['DD'].refresh(); } } }); }; for (var _i = 0; _i < len; _i++) { _loop(_i); } _this.$('.rolldate-panel').className = 'rolldate-panel fadeIn'; }, $: function $(selector, flag) { if (typeof selector != 'string' && selector.nodeType) { return selector; } return flag ? document.querySelectorAll(selector) : document.querySelector(selector); }, tap: function tap(el, fn) { var _this = this, hasTouch = "ontouchstart" in window; if (hasTouch && _this.config.trigger == 'tap') { var o = {}; el.addEventListener('touchstart', function (e) { var t = e.touches[0]; o.startX = t.pageX; o.startY = t.pageY; o.sTime = +new Date(); }); el.addEventListener('touchend', function (e) { var t = e.changedTouches[0]; o.endX = t.pageX; o.endY = t.pageY; if (+new Date() - o.sTime < 300) { if (Math.abs(o.endX - o.startX) + Math.abs(o.endY - o.startY) < 20) { e.preventDefault(); fn.call(this, e); } } o = {}; }); } else { el.addEventListener('click', function (e) { fn.call(this, e); }); } }, show: function show() { var _this = this, config = _this.config, el = void 0; if (config.el) { el = _this.$(config.el); if (!el.bindRolldate) { return; } if (el.nodeName.toLowerCase() == 'input') { el.blur(); } } if (_this.$('.rolldate-container')) { return; } if (config.init && config.init.call(_this) === false) { return; } _this.createUI(); _this.event(); }, hide: function hide(flag) { var _this = this, el = _this.$('.rolldate-panel.fadeIn'); if (el) { el.className = 'rolldate-panel fadeOut'; _this.destroy(flag); } }, event: function event() { var _this = this, mask = _this.$('.rolldate-mask'), cancel = _this.$('.rolldate-cancel'), confirm = _this.$('.rolldate-confirm'); _this.tap(mask, function () { _this.hide(1); }); _this.tap(cancel, function () { _this.hide(1); }); _this.tap(confirm, function () { var config = _this.config, el = void 0, date = config.format, newDate = new Date(); for (var f in _this.scroll) { var d = _this.getSelected(_this.scroll[f]); date = date.replace(f, d); if (f == 'YYYY') { newDate.setFullYear(d); } else if (f == 'MM') { newDate.setMonth(d - 1); } else if (f == 'DD') { newDate.setDate(d); } else if (f == 'hh') { newDate.setHours(d); } else if (f == 'mm') { newDate.setMinutes(d); } else if (f == 'ss') { newDate.setSeconds(d); } } if (config.confirm) { var flag = config.confirm.call(_this, date); if (flag === false) { return false; } else if (flag) { date = flag; } } if (config.el) { el = _this.$(config.el); if (el.nodeName.toLowerCase() == 'input') { el.value = date; } else { el.innerText = date; } el.bindDate = newDate; } else { _this.bindDate = newDate; } _this.hide(); }); }, bissextile: function bissextile(year, month) { var day = void 0; if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) { day = 31; } else if (month == 4 || month == 6 || month == 11 || month == 9) { day = 30; } else if (month == 2) { if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) { //闰年 day = 29; } else { day = 28; } } return day; }, destroy: function destroy(flag) { var _this = this, config = _this.config; for (var i in _this.scroll) { _this.scroll[i].destroy(); } if (flag && config.cancel) { config.cancel.call(_this); } setTimeout(function () { var el = _this.$('.rolldate-container'); document.body.removeChild(el); }, 300); }, getSelected: function getSelected(scroll) { return this.$('#' + scroll.wrapper.id + ' li', 1)[scroll.getSelectedIndex()].innerText.replace(/\D/g, ''); } }; Rolldate.version = version; return Rolldate; }));