欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

laydate.js在火狐下的定位问题

程序员文章站 2022-07-05 15:26:58
这个情况不知道在火狐的什么情况下会出现这个问题,但是他的demo定位在火狐下是没有问题的。 正常情况下展示位置是这样的 但是呢,在我的火狐下展示的位置是这样的. 哎,默默的读源码吧; 源码: 修改后的代码: 下面是所有的源码,laydate.js ......

这个情况不知道在火狐的什么情况下会出现这个问题,但是他的demo定位在火狐下是没有问题的。

正常情况下展示位置是这样的

laydate.js在火狐下的定位问题

但是呢,在我的火狐下展示的位置是这样的.

laydate.js在火狐下的定位问题

哎,默默的读源码吧;

源码:

laydate.js在火狐下的定位问题

修改后的代码:

laydate.js在火狐下的定位问题

下面是所有的源码,laydate.js

/**
 
 @name : laydate 5.0.9 日期时间控件
 @author: 贤心
 @site:http://www.layui.com/laydate/
 @license:mit
 
 */

;!function(){
  "use strict";

  var islayui = window.layui && layui.define, ready = {
    getpath: function(){
      var jspath = document.currentscript ? document.currentscript.src : function(){
        var js = document.scripts
        ,last = js.length - 1
        ,src;
        for(var i = last; i > 0; i--){
          if(js[i].readystate === 'interactive'){
            src = js[i].src;
            break;
          }
        }
        return src || js[last].src;
      }();
      return jspath.substring(0, jspath.lastindexof('/') + 1);
    }()
    
    //获取节点的style属性值
    ,getstyle: function(node, name){
      var style = node.currentstyle ? node.currentstyle : window.getcomputedstyle(node, null);
      return style[style.getpropertyvalue ? 'getpropertyvalue' : 'getattribute'](name);
    }
    
    //载入css配件
    ,link: function(href, fn, cssname){
      
      //未设置路径,则不主动加载css
      if(!laydate.path) return;
      
      var head = document.getelementsbytagname("head")[0], link = document.createelement('link');
      if(typeof fn === 'string') cssname = fn;
      var app = (cssname || href).replace(/\.|\//g, '');
      var id = 'layuicss-'+ app, timeout = 0;
      
      link.rel = 'stylesheet';
      link.href = laydate.path + href;
      link.id = id;
      
      if(!document.getelementbyid(id)){
        head.appendchild(link);
      }
      
      if(typeof fn !== 'function') return;
      
      //轮询css是否加载完毕
      (function poll() { 
        if(++timeout > 8 * 1000 / 100){
          return window.console && console.error('laydate.css: invalid');
        };
        parseint(ready.getstyle(document.getelementbyid(id), 'width')) === 1989 ? fn() : settimeout(poll, 100);
      }());
    }
  }

  ,laydate = {
    v: '5.0.9'
    ,config: {} //全局配置项
    ,index: (window.laydate && window.laydate.v) ? 100000 : 0
    ,path: ready.getpath
    
    //设置全局项
    ,set: function(options){
      var that = this;
      that.config = lay.extend({}, that.config, options);
      return that;
    }
    
    //主体css等待事件
    ,ready: function(fn){
      var cssname = 'laydate', ver = ''
      ,path = (islayui ? 'modules/laydate/' : 'theme/') + 'default/laydate.css?v='+ laydate.v + ver;
      islayui ? layui.addcss(path, fn, cssname) : ready.link(path, fn, cssname);
      return this;
    }
  }
  
  //操作当前实例
  ,thisdate = function(){
    var that = this;
    return {
      //提示框
      hint: function(content){
        that.hint.call(that, content);
      }
      ,config: that.config
    };
  }

  //字符常量
  ,mod_name = 'laydate', elem = '.layui-laydate', this = 'layui-this', show = 'layui-show', hide = 'layui-hide', disabled = 'laydate-disabled', tips_out = '开始日期超出了结束日期<br>建议重新选择', limit_year = [100, 200000]
  
  ,elem_static = 'layui-laydate-static', elem_list = 'layui-laydate-list', elem_selected = 'laydate-selected', elem_hint = 'layui-laydate-hint', elem_prev = 'laydate-day-prev', elem_next = 'laydate-day-next', elem_footer = 'layui-laydate-footer', elem_confirm = '.laydate-btns-confirm', elem_time_text = 'laydate-time-text', elem_time_btn = '.laydate-btns-time'
  
  //组件构造器
  ,class = function(options){
    var that = this;
    that.index = ++laydate.index;
    that.config = lay.extend({}, that.config, laydate.config, options);
    laydate.ready(function(){
      that.init();
    });
  }
  
  //dom查找
  ,lay = function(selector){   
    return new lay(selector);
  }
  
  //dom构造器
  ,lay = function(selector){
    var index = 0
    ,nativedom = typeof selector === 'object' ? [selector] : (
      this.selector = selector
      ,document.queryselectorall(selector || null)
    );
    for(; index < nativedom.length; index++){
      this.push(nativedom[index]);
    }
  };
  
  
  /*
    lay对象操作
  */
  
  lay.prototype = [];
  lay.prototype.constructor = lay;
  
  //普通对象深度扩展
  lay.extend = function(){
    var ai = 1, args = arguments
    ,clone = function(target, obj){
      target = target || (obj.constructor === array ? [] : {}); 
      for(var i in obj){
        //如果值为对象,则进入递归,继续深度合并
        target[i] = (obj[i] && (obj[i].constructor === object))
          ? clone(target[i], obj[i])
        : obj[i];
      }
      return target;
    }

    args[0] = typeof args[0] === 'object' ? args[0] : {};

    for(; ai < args.length; ai++){
      if(typeof args[ai] === 'object'){
        clone(args[0], args[ai])
      }
    }
    return args[0];
  };
  
  //ie版本
  lay.ie = function(){
    var agent = navigator.useragent.tolowercase();
    return (!!window.activexobject || "activexobject" in window) ? (
      (agent.match(/msie\s(\d+)/) || [])[1] || '11' //由于ie11并没有msie的标识
    ) : false;
  }();
  
  //中止冒泡
  lay.stope = function(e){
    e = e || window.event;
    e.stoppropagation 
      ? e.stoppropagation() 
    : e.cancelbubble = true;
  };
  
  //对象遍历
  lay.each = function(obj, fn){
    var key
    ,that = this;
    if(typeof fn !== 'function') return that;
    obj = obj || [];
    if(obj.constructor === object){
      for(key in obj){
        if(fn.call(obj[key], key, obj[key])) break;
      }
    } else {
      for(key = 0; key < obj.length; key++){
        if(fn.call(obj[key], key, obj[key])) break;
      }
    }
    return that;
  };
  
  //数字前置补零
  lay.digit = function(num, length, end){
    var str = '';
    num = string(num);
    length = length || 2;
    for(var i = num.length; i < length; i++){
      str += '0';
    }
    return num < math.pow(10, length) ? str + (num|0) : num;
  };
  
  //创建元素
  lay.elem = function(elemname, attr){
    var elem = document.createelement(elemname);
    lay.each(attr || {}, function(key, value){
      elem.setattribute(key, value);
    });
    return elem;
  };
  
  //追加字符
  lay.addstr = function(str, new_str){
    str = str.replace(/\s+/, ' ');
    new_str = new_str.replace(/\s+/, ' ').split(' ');
    lay.each(new_str, function(ii, item){
      if(!new regexp('\\b'+ item + '\\b').test(str)){
        str = str + ' ' + item;
      }
    });
    return str.replace(/^\s|\s$/, '');
  };
  
  //移除值
  lay.removestr = function(str, new_str){
    str = str.replace(/\s+/, ' ');
    new_str = new_str.replace(/\s+/, ' ').split(' ');
    lay.each(new_str, function(ii, item){
      var exp = new regexp('\\b'+ item + '\\b')
      if(exp.test(str)){
        str = str.replace(exp, '');
      }
    });
    return str.replace(/\s+/, ' ').replace(/^\s|\s$/, '');
  };
  
  //查找子元素
  lay.prototype.find = function(selector){
    var that = this;
    var index = 0, arr = []
    ,isobject = typeof selector === 'object';
    
    this.each(function(i, item){
      var nativedom = isobject ? [selector] : item.queryselectorall(selector || null);
      for(; index < nativedom.length; index++){
        arr.push(nativedom[index]);
      }
      that.shift();
    });
    
    if(!isobject){
      that.selector =  (that.selector ? that.selector + ' ' : '') + selector
    }
    
    lay.each(arr, function(i, item){
      that.push(item);
    });
    
    return that;
  };
  
  //dom遍历
  lay.prototype.each = function(fn){
    return lay.each.call(this, this, fn);
  };
  
  //添加css类
  lay.prototype.addclass = function(classname, type){
    return this.each(function(index, item){
      item.classname = lay[type ? 'removestr' : 'addstr'](item.classname, classname)
    });
  };
  
  //移除css类
  lay.prototype.removeclass = function(classname){
    return this.addclass(classname, true);
  };
  
  //是否包含css类
  lay.prototype.hasclass = function(classname){
    var has = false;
    this.each(function(index, item){
      if(new regexp('\\b'+ classname +'\\b').test(item.classname)){
        has = true;
      }
    });
    return has;
  };
  
  //添加或获取属性
  lay.prototype.attr = function(key, value){
    var that = this;
    return value === undefined ? function(){
      if(that.length > 0) return that[0].getattribute(key);
    }() : that.each(function(index, item){
      item.setattribute(key, value);
    });   
  };
  
  //移除属性
  lay.prototype.removeattr = function(key){
    return this.each(function(index, item){
      item.removeattribute(key);
    });
  };
  
  //设置html内容
  lay.prototype.html = function(html){
    return this.each(function(index, item){
      item.innerhtml = html;
    });
  };
  
  //设置值
  lay.prototype.val = function(value){
    return this.each(function(index, item){
      item.value = value;
    });
  };
  
  //追加内容
  lay.prototype.append = function(elem){
    return this.each(function(index, item){
      typeof elem === 'object' 
        ? item.appendchild(elem)
      :  item.innerhtml = item.innerhtml + elem;
    });
  };
  
  //移除内容
  lay.prototype.remove = function(elem){
    return this.each(function(index, item){
      elem ? item.removechild(elem) : item.parentnode.removechild(item);
    });
  };
  
  //事件绑定
  lay.prototype.on = function(eventname, fn){
    return this.each(function(index, item){
      item.attachevent ? item.attachevent('on' + eventname, function(e){
        e.target = e.srcelement;
        fn.call(item, e);
      }) : item.addeventlistener(eventname, fn, false);
    });
  };
  
  //解除事件
  lay.prototype.off = function(eventname, fn){
    return this.each(function(index, item){
      item.detachevent 
        ? item.detachevent('on'+ eventname, fn)  
      : item.removeeventlistener(eventname, fn, false);
    });
  };
  
  
  /*
    组件操作
  */
  
  
  //是否闰年
  class.isleapyear = function(year){
    return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
  };
  
  //默认配置
  class.prototype.config = {
    type: 'date' //控件类型,支持:year/month/date/time/datetime
    ,range: false //是否开启范围选择,即双控件
    ,format: 'yyyy-mm-dd' //默认日期格式
    ,value: null //默认日期,支持传入new date(),或者符合format参数设定的日期格式字符
    ,min: '1900-1-1' //有效最小日期,年月日必须用“-”分割,时分秒必须用“:”分割。注意:它并不是遵循 format 设定的格式。
    ,max: '2099-12-31' //有效最大日期,同上
    ,trigger: 'focus' //呼出控件的事件
    ,show: false //是否直接显示,如果设置true,则默认直接显示控件
    ,showbottom: true //是否显示底部栏
    ,btns: ['clear', 'now', 'confirm'] //右下角显示的按钮,会按照数组顺序排列
    ,lang: 'cn' //语言,只支持cn/en,即中文和英文
    ,theme: 'default' //主题
    ,position: null //控件定位方式定位, 默认absolute,支持:fixed/absolute/static
    ,calendar: false //是否开启公历重要节日,仅支持中文版
    ,mark: {} //日期备注,如重要事件或活动标记
    ,zindex: null //控件层叠顺序
    ,done: null //控件选择完毕后的回调,点击清空/现在/确定也均会触发
    ,change: null //日期时间改变后的回调
  };
  
  //多语言
  class.prototype.lang = function(){
    var that = this
    ,options = that.config
    ,text = {
      cn: {
        weeks: ['日', '一', '二', '三', '四', '五', '六']
        ,time: ['时', '分', '秒']
        ,timetips: '选择时间'
        ,starttime: '开始时间'
        ,endtime: '结束时间'
        ,datetips: '返回日期'
        ,month: ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二']
        ,tools: {
          confirm: '确定'
          ,clear: '清空'
          ,now: '现在'
        }
      }
      ,en: {
        weeks: ['su', 'mo', 'tu', 'we', 'th', 'fr', 'sa']
        ,time: ['hours', 'minutes', 'seconds']
        ,timetips: 'select time'
        ,starttime: 'start time'
        ,endtime: 'end time'
        ,datetips: 'select date'
        ,month: ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec']
        ,tools: {
          confirm: 'confirm'
          ,clear: 'clear'
          ,now: 'now'
        }
      }
    };
    return text[options.lang] || text['cn'];
  };
  
  //初始准备
  class.prototype.init = function(){
    var that = this
    ,options = that.config
    ,datetype = 'yyyy|y|mm|m|dd|d|hh|h|mm|m|ss|s'
    ,isstatic = options.position === 'static'
    ,format = {
      year: 'yyyy'
      ,month: 'yyyy-mm'
      ,date: 'yyyy-mm-dd'
      ,time: 'hh:mm:ss'
      ,datetime: 'yyyy-mm-dd hh:mm:ss'
    };
    
    options.elem = lay(options.elem);
    options.eventelem = lay(options.eventelem);
    
    if(!options.elem[0]) return;
    
    //日期范围分隔符
    if(options.range === true) options.range = '-';
    
    //根据不同type,初始化默认format
    if(options.format === format.date){
      options.format = format[options.type];
    }
    
    //将日期格式转化成数组
    that.format = options.format.match(new regexp(datetype + '|.', 'g')) || [];
    
    //生成正则表达式
    that.exp_if = ''; 
    that.exp_split = ''; 
    lay.each(that.format, function(i, item){
      var exp =  new regexp(datetype).test(item) 
        ? '\\d{'+ function(){
          if(new regexp(datetype).test(that.format[i === 0 ? i + 1 : i - 1]||'')){
            if(/^yyyy|y$/.test(item)) return 4;
            return item.length;
          }
          if(/^yyyy$/.test(item)) return '1,4';
          if(/^y$/.test(item)) return '1,308';
          return '1,2';
        }() +'}' 
      : '\\' + item;
      that.exp_if = that.exp_if + exp;
      that.exp_split = that.exp_split + '(' + exp + ')';
    });
    that.exp_if = new regexp('^'+ (
      options.range ? 
        that.exp_if + '\\s\\'+ options.range + '\\s' + that.exp_if
      : that.exp_if
    ) +'$');
    that.exp_split = new regexp('^'+ that.exp_split +'$', '');
    
    //如果不是input|textarea元素,则默认采用click事件
    if(!that.isinput(options.elem[0])){
      if(options.trigger === 'focus'){
        options.trigger = 'click';
      }
    }
    
    //设置唯一key
    if(!options.elem.attr('lay-key')){
      options.elem.attr('lay-key', that.index);
      options.eventelem.attr('lay-key', that.index);
    }
    
    //记录重要日期
    options.mark = lay.extend({}, (options.calendar && options.lang === 'cn') ? {
      '0-1-1': '元旦'
      ,'0-2-14': '情人'
      ,'0-3-8': '妇女'
      ,'0-3-12': '植树'
      ,'0-4-1': '愚人'
      ,'0-5-1': '劳动'
      ,'0-5-4': '青年'
      ,'0-6-1': '儿童'
      ,'0-9-10': '教师'
      ,'0-9-18': '国耻'
      ,'0-10-1': '国庆'
      ,'0-12-25': '圣诞'
    } : {}, options.mark);
    
    //获取限制内日期
    lay.each(['min', 'max'], function(i, item){
      var ymd = [], hms = [];
      if(typeof options[item] === 'number'){ //如果为数字
        var day = options[item]
        ,time = new date().gettime()
        ,stamp = 86400000 //代表一天的时间戳
        ,thisdate = new date(
          day ? (
            day < stamp ? time + day*stamp : day //如果数字小于一天的时间戳,则数字为天数,否则为时间戳
          ) : time
        );
        ymd = [thisdate.getfullyear(), thisdate.getmonth() + 1, thisdate.getdate()];
        day < stamp || (hms = [thisdate.gethours(), thisdate.getminutes(), thisdate.getseconds()]);
      } else {
        ymd = (options[item].match(/\d+-\d+-\d+/) || [''])[0].split('-');
        hms = (options[item].match(/\d+:\d+:\d+/) || [''])[0].split(':');
      }
      options[item] = {
        year: ymd[0] | 0 || new date().getfullyear()
        ,month: ymd[1] ? (ymd[1] | 0) - 1 : new date().getmonth()
        ,date: ymd[2] | 0 || new date().getdate()
        ,hours: hms[0] | 0
        ,minutes: hms[1] | 0
        ,seconds: hms[2] | 0
      };
    });
    
    that.elemid = 'layui-laydate'+ options.elem.attr('lay-key');
    
    if(options.show || isstatic) that.render();
    isstatic || that.events();
    
    //默认赋值
    if(options.value){
      if(options.value.constructor === date){
        that.setvalue(that.parse(0, that.systemdate(options.value))); 
      } else {
        that.setvalue(options.value); 
      }
    }
  };
  
  //控件主体渲染
  class.prototype.render = function(){
    var that = this
    ,options = that.config
    ,lang = that.lang()
    ,isstatic = options.position === 'static'
    
    //主面板
    ,elem = that.elem = lay.elem('div', {
      id: that.elemid
      ,'class': [
        'layui-laydate'
        ,options.range ? ' layui-laydate-range' : ''
        ,isstatic ? (' '+ elem_static) : ''
        ,options.theme && options.theme !== 'default' && !/^#/.test(options.theme) ? (' laydate-theme-' + options.theme) : ''
      ].join('')
    })
    
    //主区域
    ,elemmain = that.elemmain = []
    ,elemheader = that.elemheader = []
    ,elemcont = that.elemcont = []
    ,elemtable = that.table = []

    //底部区域
    ,divfooter = that.footer = lay.elem('div', {
      'class': elem_footer
    });
    
    if(options.zindex) elem.style.zindex = options.zindex;
    
    //单双日历区域
    lay.each(new array(2), function(i){
      if(!options.range && i > 0){
        return true;
      }

      //头部区域
      var divheader = lay.elem('div', {
        'class': 'layui-laydate-header'
      })
      
      //左右切换
      ,headerchild = [function(){ //上一年
        var elem = lay.elem('i', {
          'class': 'layui-icon laydate-icon laydate-prev-y'
        });
        elem.innerhtml = '';
        return elem;
      }(), function(){ //上一月
        var elem = lay.elem('i', {
          'class': 'layui-icon laydate-icon laydate-prev-m'
        });
        elem.innerhtml = '';
        return elem;
      }(), function(){ //年月选择
        var elem = lay.elem('div', {
          'class': 'laydate-set-ym'
        }), spany = lay.elem('span'), spanm = lay.elem('span');
        elem.appendchild(spany);
        elem.appendchild(spanm);
        return elem;
      }(), function(){ //下一月
        var elem = lay.elem('i', {
          'class': 'layui-icon laydate-icon laydate-next-m'
        });
        elem.innerhtml = '';
        return elem;
      }(), function(){ //下一年
        var elem = lay.elem('i', {
          'class': 'layui-icon laydate-icon laydate-next-y'
        });
        elem.innerhtml = '';
        return elem;
      }()]
      
      //日历内容区域
      ,divcontent = lay.elem('div', {
        'class': 'layui-laydate-content'
      })
      ,table = lay.elem('table')
      ,thead = lay.elem('thead'), theadtr = lay.elem('tr');
      
      //生成年月选择
      lay.each(headerchild, function(i, item){
        divheader.appendchild(item);
      });
      
       //生成表格
      thead.appendchild(theadtr);
      lay.each(new array(6), function(i){ //表体
        var tr = table.insertrow(0);
        lay.each(new array(7), function(j){
          if(i === 0){
            var th = lay.elem('th');
            th.innerhtml = lang.weeks[j];
            theadtr.appendchild(th);
          }
          tr.insertcell(j);
        });
      });
      table.insertbefore(thead, table.children[0]); //表头
      divcontent.appendchild(table);
      
      elemmain[i] = lay.elem('div', {
        'class': 'layui-laydate-main laydate-main-list-'+ i
      });
      
      elemmain[i].appendchild(divheader);
      elemmain[i].appendchild(divcontent);
      
      elemheader.push(headerchild);
      elemcont.push(divcontent);
      elemtable.push(table);
    });
    
    //生成底部栏
    lay(divfooter).html(function(){
      var html = [], btns = [];
      if(options.type === 'datetime'){
        html.push('<span lay-type="datetime" class="laydate-btns-time">'+ lang.timetips +'</span>');
      }
      lay.each(options.btns, function(i, item){
        var title = lang.tools[item] || 'btn';
        if(options.range && item === 'now') return;
        if(isstatic && item === 'clear') title = options.lang === 'cn' ? '重置' : 'reset';
        btns.push('<span lay-type="'+ item +'" class="laydate-btns-'+ item +'">'+ title +'</span>');
      });
      html.push('<div class="laydate-footer-btns">'+ btns.join('') +'</div>');
      return html.join('');
    }());
    
    //插入到主区域
    lay.each(elemmain, function(i, main){
      elem.appendchild(main);
    });
    options.showbottom && elem.appendchild(divfooter);
    
    //生成自定义主题
    if(/^#/.test(options.theme)){
      var style = lay.elem('style')
      ,styletext = [
        '#{{id}} .layui-laydate-header{background-color:{{theme}};}'
        ,'#{{id}} .layui-this{background-color:{{theme}} !important;}'
      ].join('').replace(/{{id}}/g, that.elemid).replace(/{{theme}}/g, options.theme);
      
      if('stylesheet' in style){
        style.setattribute('type', 'text/css');
        style.stylesheet.csstext = styletext;
      } else {
        style.innerhtml = styletext;
      }
      
      lay(elem).addclass('laydate-theme-molv');
      elem.appendchild(style);
    }
    
    //移除上一个控件
    that.remove(class.thiselemdate); 
    
    //如果是静态定位,则插入到指定的容器中,否则,插入到body
    isstatic ? options.elem.append(elem) : (
      document.body.appendchild(elem)
      ,that.position() //定位
    );
    
    that.checkdate().calendar(); //初始校验
    that.changeevent(); //日期切换
    
    class.thiselemdate = that.elemid;

    typeof options.ready === 'function' && options.ready(lay.extend({}, options.datetime, {
      month: options.datetime.month + 1
    }));
  };
  
  //控件移除
  class.prototype.remove = function(prev){
    var that = this
    ,options = that.config
    ,elem = lay('#'+ (prev || that.elemid));
    if(!elem.hasclass(elem_static)){
      that.checkdate(function(){
        elem.remove();
      });
    }
    return that;
  };
  
  //定位算法
  class.prototype.position = function(){
    var that = this
    ,options = that.config
    ,elem = that.bindelem || options.elem[0]
    ,rect = elem.getboundingclientrect() //绑定元素的坐标
    ,elemwidth = that.elem.offsetwidth //控件的宽度
    ,elemheight = that.elem.offsetheight //控件的高度
    
    //滚动条高度
    ,scrollarea = function(type){
      type = type ? 'scrollleft' : 'scrolltop';
      return document.body[type] | document.documentelement[type];
    }
    ,winarea = function(type){
      return document.documentelement[type ? 'clientwidth' : 'clientheight']
    }, margin = 5, left = rect.left, top = rect.bottom;
    
    //如果右侧超出边界
    if(left + elemwidth + margin > winarea('width')){
      left = winarea('width') - elemwidth - margin;
    }
    
    //如果底部超出边界
    if(top + elemheight + margin > winarea('height')){
      top = rect.top > elemheight //顶部是否有足够区域显示完全
        ? rect.top - elemheight 
      : winarea() - elemheight;
      top = top - margin*2;
    }
    
    if(options.position){
      that.elem.style.position = options.position;
    }
    that.elem.style.left = left + (options.position === 'fixed' ? 0 : scrollarea(1)) + 'px';
    that.elem.style.top = top + (options.position === 'fixed' ? 0 : scrollarea()) + 'px';
  };
  
  //提示
  class.prototype.hint = function(content){
    var that = this
    ,options = that.config
    ,div = lay.elem('div', {
      'class': elem_hint
    });
    
    div.innerhtml = content || '';
    lay(that.elem).find('.'+ elem_hint).remove();
    that.elem.appendchild(div);

    cleartimeout(that.hintimer);
    that.hintimer = settimeout(function(){
      lay(that.elem).find('.'+ elem_hint).remove();
    }, 3000);
  };
  
  //获取递增/减后的年月
  class.prototype.getasym = function(y, m, type){
    type ? m-- : m++;
    if(m < 0){
      m = 11;
      y--;
    }
    if(m > 11){
      m = 0;
      y++;
    }
    return [y, m];
  };
  
  //系统消息
  class.prototype.systemdate = function(newdate){
    var thisdate = newdate || new date();
    return {
      year: thisdate.getfullyear() //年
      ,month: thisdate.getmonth() //月
      ,date: thisdate.getdate() //日
      ,hours: newdate ? newdate.gethours() : 0 //时
      ,minutes: newdate ? newdate.getminutes() : 0 //分
      ,seconds: newdate ? newdate.getseconds() : 0 //秒
    }
  };
  
  //日期校验
  class.prototype.checkdate = function(fn){
    var that = this
    ,thisdate = new date()
    ,options = that.config
    ,datetime = options.datetime = options.datetime || that.systemdate()
    ,thismaxdate, error
    
    ,elem = that.bindelem || options.elem[0]
    ,valtype = that.isinput(elem) ? 'val' : 'html'
    ,value = that.isinput(elem) ? elem.value : (options.position === 'static' ? '' : elem.innerhtml)
    
    //校验日期有效数字
    ,checkvalid = function(datetime){
      if(datetime.year > limit_year[1]) datetime.year = limit_year[1], error = true; //不能超过20万年
      if(datetime.month > 11) datetime.month = 11, error = true;
      if(datetime.hours > 23) datetime.hours = 0, error = true;
      if(datetime.minutes > 59) datetime.minutes = 0, datetime.hours++, error = true;
      if(datetime.seconds > 59) datetime.seconds = 0, datetime.minutes++, error = true;
      
      //计算当前月的最后一天
      thismaxdate = laydate.getenddate(datetime.month + 1, datetime.year);
      if(datetime.date > thismaxdate) datetime.date = thismaxdate, error = true;
    }
    
    //获得初始化日期值
    ,initdate = function(datetime, value, index){
      var startend = ['starttime', 'endtime'];
      value = (value.match(that.exp_split) || []).slice(1);
      index = index || 0;
      if(options.range){
        that[startend[index]] = that[startend[index]] || {};
      }
      lay.each(that.format, function(i, item){
        var thisv = parsefloat(value[i]);
        if(value[i].length < item.length) error = true;
        if(/yyyy|y/.test(item)){ //年
          if(thisv < limit_year[0]) thisv = limit_year[0], error = true; //年不能低于100年
          datetime.year = thisv;
        } else if(/mm|m/.test(item)){ //月
          if(thisv < 1) thisv = 1, error = true;
          datetime.month = thisv - 1;
        } else if(/dd|d/.test(item)){ //日
          if(thisv < 1) thisv = 1, error = true;
          datetime.date = thisv;
        } else if(/hh|h/.test(item)){ //时
          if(thisv < 1) thisv = 0, error = true;
          datetime.hours = thisv;
          options.range && (that[startend[index]].hours = thisv);
        } else if(/mm|m/.test(item)){ //分
          if(thisv < 1) thisv = 0, error = true;
          datetime.minutes = thisv;
          options.range && (that[startend[index]].minutes = thisv);
        } else if(/ss|s/.test(item)){ //秒
          if(thisv < 1) thisv = 0, error = true;
          datetime.seconds = thisv;
          options.range && (that[startend[index]].seconds = thisv);
        }
      });
      checkvalid(datetime)
    };
    
    if(fn === 'limit') return checkvalid(datetime), that;
    
    value = value || options.value;
    if(typeof value === 'string'){
      value = value.replace(/\s+/g, ' ').replace(/^\s|\s$/g, '');
    }
    
    //如果点击了开始,单未选择结束就关闭,则重新选择开始
    if(that.startstate && !that.endstate){
      delete that.startstate;
      that.endstate = true;
    };

    if(typeof value === 'string' && value){
      if(that.exp_if.test(value)){ //校验日期格式
        if(options.range){
          value = value.split(' '+ options.range +' ');
          that.startdate = that.startdate || that.systemdate();
          that.enddate = that.enddate || that.systemdate();
          options.datetime = lay.extend({}, that.startdate);
          lay.each([that.startdate, that.enddate], function(i, item){
            initdate(item, value[i], i);
          });
        } else {
          initdate(datetime, value)
        }
      } else {
        that.hint('日期格式不合法<br>必须遵循下述格式:<br>'+ (
          options.range ? (options.format + ' '+ options.range +' ' + options.format) : options.format
        ) + '<br>已为你重置');
        error = true;
      }
    } else if(value && value.constructor === date){ //如果值为日期对象时
      options.datetime = that.systemdate(value);
    } else {
      options.datetime = that.systemdate();
      delete that.startstate;
      delete that.endstate;
      delete that.startdate;
      delete that.enddate;
      delete that.starttime;
      delete that.endtime;
    }

    checkvalid(datetime);

    if(error && value){
      that.setvalue(
        options.range ? (that.enddate ? that.parse() : '') : that.parse()
      );
    }
    fn && fn();
    return that;
  };
  
  //公历重要日期与自定义备注
  class.prototype.mark = function(td, ymd){
    var that = this
    ,mark, options = that.config;
    lay.each(options.mark, function(key, title){
      var keys = key.split('-');
      if((keys[0] == ymd[0] || keys[0] == 0) //每年的每月
      && (keys[1] == ymd[1] || keys[1] == 0) //每月的每日
      && keys[2] == ymd[2]){ //特定日
        mark = title || ymd[2];
      }
    });
    mark && td.html('<span class="laydate-day-mark">'+ mark +'</span>');
    
    return that;
  };
  
  //无效日期范围的标记
  class.prototype.limit = function(elem, date, index, time){
    var that = this
    ,options = that.config, timestrap = {}
    ,datetime = options[index > 41 ? 'enddate' : 'datetime']
    ,isout, thisdatetime = lay.extend({}, datetime, date || {});
    lay.each({
      now: thisdatetime
      ,min: options.min
      ,max: options.max
    }, function(key, item){
      timestrap[key] = that.newdate(lay.extend({
        year: item.year
        ,month: item.month
        ,date: item.date
      }, function(){
        var hms = {};
        lay.each(time, function(i, keys){
          hms[keys] = item[keys];
        });
        return hms;
      }())).gettime();  //time:是否比较时分秒
    });
    
    isout = timestrap.now < timestrap.min || timestrap.now > timestrap.max;
    elem && elem[isout ? 'addclass' : 'removeclass'](disabled);
    return isout;
  };
  
  //日历表
  class.prototype.calendar = function(value){
    var that = this
    ,options = that.config
    ,datetime = value || options.datetime
    ,thisdate = new date(), startweek, prevmaxdate, thismaxdate
    ,lang = that.lang()
    
    ,isalone = options.type !== 'date' && options.type !== 'datetime'
    ,index = value ? 1 : 0
    ,tds = lay(that.table[index]).find('td')
    ,elemym = lay(that.elemheader[index][2]).find('span');
    
    if(datetime.year < limit_year[0]) datetime.year = limit_year[0], that.hint('最低只能支持到公元'+ limit_year[0] +'年');
    if(datetime.year > limit_year[1]) datetime.year = limit_year[1], that.hint('最高只能支持到公元'+ limit_year[1] +'年');
    
    //记录初始值
    if(!that.firstdate){
      that.firstdate = lay.extend({}, datetime);
    }
    
    //计算当前月第一天的星期
    thisdate.setfullyear(datetime.year, datetime.month, 1);
    startweek = thisdate.getday();
    
    prevmaxdate = laydate.getenddate(datetime.month || 12, datetime.year); //计算上个月的最后一天
    thismaxdate = laydate.getenddate(datetime.month + 1, datetime.year); //计算当前月的最后一天
    
    //赋值日
    lay.each(tds, function(index, item){
      var ymd = [datetime.year, datetime.month], st = 0;
      item = lay(item);
      item.removeattr('class');
      if(index < startweek){
        st = prevmaxdate - startweek + index;
        item.addclass('laydate-day-prev');
        ymd = that.getasym(datetime.year, datetime.month, 'sub');
      } else if(index >= startweek && index < thismaxdate + startweek){
        st = index - startweek;
        if(!options.range){
          st + 1 === datetime.date && item.addclass(this);
        }
      } else {
        st = index - thismaxdate - startweek;
        item.addclass('laydate-day-next');
        ymd = that.getasym(datetime.year, datetime.month);
      }
      ymd[1]++;
      ymd[2] = st + 1;
      item.attr('lay-ymd', ymd.join('-')).html(ymd[2]);
      that.mark(item, ymd).limit(item, {
        year: ymd[0]
        ,month: ymd[1] - 1
        ,date: ymd[2]
      }, index);
    });  
    
    //同步头部年月
    lay(elemym[0]).attr('lay-ym', datetime.year + '-' + (datetime.month + 1));
    lay(elemym[1]).attr('lay-ym', datetime.year + '-' + (datetime.month + 1));
    
    if(options.lang === 'cn'){
      lay(elemym[0]).attr('lay-type', 'year').html(datetime.year + '年')
      lay(elemym[1]).attr('lay-type', 'month').html((datetime.month + 1) + '月');
    } else {
      lay(elemym[0]).attr('lay-type', 'month').html(lang.month[datetime.month]);
      lay(elemym[1]).attr('lay-type', 'year').html(datetime.year);
    }

    //初始默认选择器
    if(isalone){
      if(options.range){
        value ? that.enddate = (that.enddate || {
          year: datetime.year + (options.type === 'year' ? 1 : 0)
          ,month: datetime.month + (options.type === 'month' ? 0 : -1)
        }) : (that.startdate = that.startdate || {
          year: datetime.year
          ,month: datetime.month
        });
        if(value){
          that.listym = [
            [that.startdate.year, that.startdate.month + 1]
            ,[that.enddate.year, that.enddate.month + 1]
          ];  
          that.list(options.type, 0).list(options.type, 1);
          //同步按钮可点状态
          options.type === 'time' ? that.setbtnstatus('时间'
            ,lay.extend({}, that.systemdate(), that.starttime)
            ,lay.extend({}, that.systemdate(), that.endtime)
          ) : that.setbtnstatus(true);
        }        
      }
      if(!options.range){
        that.listym = [[datetime.year, datetime.month + 1]];
        that.list(options.type, 0);
      }
    }
    
    //赋值双日历
    if(options.range && !value){
      var eym = that.getasym(datetime.year, datetime.month)
      that.calendar(lay.extend({}, datetime, {
        year: eym[0]
        ,month: eym[1]
      }));
    }
    
    //通过检测当前有效日期,来设定确定按钮是否可点
    if(!options.range) that.limit(lay(that.footer).find(elem_confirm), null, 0, ['hours', 'minutes', 'seconds']);
    
    //标记选择范围
    if(options.range && value && !isalone) that.stamprange();
    return that;
  };

  //生成年月时分秒列表
  class.prototype.list = function(type, index){
    var that = this
    ,options = that.config
    ,datetime = options.datetime
    ,lang = that.lang()
    ,isalone = options.range && options.type !== 'date' && options.type !== 'datetime' //独立范围选择器
    
    ,ul = lay.elem('ul', {
      'class': elem_list + ' ' + ({
        year: 'laydate-year-list'
        ,month: 'laydate-month-list'
        ,time: 'laydate-time-list'
      })[type]
    })
    ,elemheader = that.elemheader[index]
    ,elemym = lay(elemheader[2]).find('span')
    ,elemcont = that.elemcont[index || 0]
    ,havelist = lay(elemcont).find('.'+ elem_list)[0]
    ,iscn = options.lang === 'cn'
    ,text = iscn ? '年' : ''
   
    ,listym = that.listym[index] || {}
    ,hms = ['hours', 'minutes', 'seconds']
    ,startend = ['starttime', 'endtime'][index];

    if(listym[0] < 1) listym[0] = 1;
    
    if(type === 'year'){ //年列表
      var yearnum, starty = yearnum = listym[0] - 7;
      if(starty < 1) starty = yearnum = 1;
      lay.each(new array(15), function(i){
        var li = lay.elem('li', {
          'lay-ym': yearnum
        }), ymd = {year: yearnum};
        yearnum == listym[0] && lay(li).addclass(this);
        li.innerhtml = yearnum + text;
        ul.appendchild(li);
        if(yearnum < that.firstdate.year){
          ymd.month = options.min.month;
          ymd.date = options.min.date;
        } else if(yearnum >= that.firstdate.year){
          ymd.month = options.max.month;
          ymd.date = options.max.date;
        }
        that.limit(lay(li), ymd, index);
        yearnum++;
      });
      lay(elemym[iscn ? 0 : 1]).attr('lay-ym', (yearnum - 8) + '-' + listym[1])
      .html((starty + text) + ' - ' + (yearnum - 1 + text));
    } else if(type === 'month'){ //月列表
      lay.each(new array(12), function(i){
        var li = lay.elem('li', {
          'lay-ym': i
        }), ymd = {year: listym[0], month: i};
        i + 1 == listym[1] && lay(li).addclass(this);
        li.innerhtml = lang.month[i] + (iscn ? '月' : '');
        ul.appendchild(li);
        if(listym[0] < that.firstdate.year){
          ymd.date = options.min.date;
        } else if(listym[0] >= that.firstdate.year){
          ymd.date = options.max.date;
        }
        that.limit(lay(li), ymd, index);
      });
      lay(elemym[iscn ? 0 : 1]).attr('lay-ym', listym[0] + '-' + listym[1])
      .html(listym[0] + text);
    } else if(type === 'time'){ //时间列表
      //检测时分秒状态是否在有效日期时间范围内
      var settimestatus = function(){
        lay(ul).find('ol').each(function(i, ol){
          lay(ol).find('li').each(function(ii, li){
            that.limit(lay(li), [{
              hours: ii
            }, {
              hours: that[startend].hours
              ,minutes: ii
            }, {
              hours: that[startend].hours
              ,minutes: that[startend].minutes
              ,seconds: ii
            }][i], index, [['hours'], ['hours', 'minutes'], ['hours', 'minutes', 'seconds']][i]);
          });
        });
        if(!options.range) that.limit(lay(that.footer).find(elem_confirm), that[startend], 0, ['hours', 'minutes', 'seconds']);
      };
      if(options.range){
        if(!that[startend]) that[startend] = {
          hours: 0
          ,minutes: 0
          ,seconds: 0
        };
      } else {
        that[startend] = datetime;
      }
      lay.each([24, 60, 60], function(i, item){
        var li = lay.elem('li'), childul = ['<p>'+ lang.time[i] +'</p><ol>'];
        lay.each(new array(item), function(ii){
          childul.push('<li'+ (that[startend][hms[i]] === ii ? ' class="'+ this +'"' : '') +'>'+ lay.digit(ii, 2) +'</li>');
        });
        li.innerhtml = childul.join('') + '</ol>';
        ul.appendchild(li);
      });
      settimestatus();
    }
    
    //插入容器
    if(havelist) elemcont.removechild(havelist);
    elemcont.appendchild(ul);
    
    //年月
    if(type === 'year' || type === 'month'){      
      //显示切换箭头
      lay(that.elemmain[index]).addclass('laydate-ym-show');
      
      //选中
      lay(ul).find('li').on('click', function(){
        var ym = lay(this).attr('lay-ym') | 0;
        if(lay(this).hasclass(disabled)) return;
        
        if(index === 0){
          datetime[type] = ym;
          if(isalone) that.startdate[type] = ym;
          that.limit(lay(that.footer).find(elem_confirm), null, 0);
        } else { //范围选择
          if(isalone){ //非date/datetime类型
            that.enddate[type] = ym;
          } else { //date/datetime类型
            var ym = type === 'year' 
              ? that.getasym(ym, listym[1] - 1, 'sub') 
            : that.getasym(listym[0], ym, 'sub');
            lay.extend(datetime, {
              year: ym[0]
              ,month: ym[1]
            });
          }
        }
        
        if(options.type === 'year' || options.type === 'month'){
          lay(ul).find('.'+ this).removeclass(this);
          lay(this).addclass(this);
          
          //如果为年月选择器,点击了年列表,则切换到月选择器
          if(options.type === 'month' && type === 'year'){
            that.listym[index][0] = ym;
            isalone && (that[['startdate', 'enddate'][index]].year = ym);
            that.list('month', index);
          }
        } else {
          that.checkdate('limit').calendar();
          that.closelist();
        }

        that.setbtnstatus(); //同步按钮可点状态
        options.range || that.done(null, 'change');
        lay(that.footer).find(elem_time_btn).removeclass(disabled);
      });
    } else {
      var span = lay.elem('span', {
        'class': elem_time_text
      }), scroll = function(){ //滚动条定位
        lay(ul).find('ol').each(function(i){
          var ol = this
          ,li = lay(ol).find('li')
          ol.scrolltop = 30*(that[startend][hms[i]] - 2);
          if(ol.scrolltop <= 0){
            li.each(function(ii, item){
              if(!lay(this).hasclass(disabled)){
                ol.scrolltop = 30*(ii - 2);
                return true;
              }
            });
          }
        });
      }, havespan = lay(elemheader[2]).find('.'+ elem_time_text);
      scroll()
      span.innerhtml = options.range ? [lang.starttime,lang.endtime][index] : lang.timetips
      lay(that.elemmain[index]).addclass('laydate-time-show');
      if(havespan[0]) havespan.remove();
      elemheader[2].appendchild(span);

      lay(ul).find('ol').each(function(i){
        var ol = this;
        //选择时分秒
        lay(ol).find('li').on('click', function(){
          var value = this.innerhtml | 0;
          if(lay(this).hasclass(disabled)) return;
          if(options.range){
            that[startend][hms[i]]  = value;
          } else {
            datetime[hms[i]] = value;
          }
          lay(ol).find('.'+ this).removeclass(this);
          lay(this).addclass(this);

          settimestatus();
          scroll();
          (that.enddate || options.type === 'time') && that.done(null, 'change');
          
          //同步按钮可点状态
          that.setbtnstatus();
        });
      });
    }
    
    return that;
  };
  
  //记录列表切换后的年月
  class.prototype.listym = [];
  
  //关闭列表
  class.prototype.closelist = function(){
    var that = this
    ,options = that.config;
    
    lay.each(that.elemcont, function(index, item){
      lay(this).find('.'+ elem_list).remove();
      lay(that.elemmain[index]).removeclass('laydate-ym-show laydate-time-show');
    });
    lay(that.elem).find('.'+ elem_time_text).remove();
  };
  
  //检测结束日期是否超出开始日期
  class.prototype.setbtnstatus = function(tips, start, end){
    var that = this
    ,options = that.config
    ,isout, elembtn = lay(that.footer).find(elem_confirm)
    ,isalone = options.range && options.type !== 'date' && options.type !== 'time';
    if(isalone){
      start = start || that.startdate;
      end = end || that.enddate;
      isout = that.newdate(start).gettime() > that.newdate(end).gettime();
      
      //如果不在有效日期内,直接禁用按钮,否则比较开始和结束日期
      (that.limit(null, start) || that.limit(null, end)) 
        ? elembtn.addclass(disabled)
      : elembtn[isout ? 'addclass' : 'removeclass'](disabled);
      
      //是否异常提示
      if(tips && isout) that.hint(
        typeof tips === 'string' ? tips_out.replace(/日期/g, tips) : tips_out
      );
    }
  };
  
  //转义为规定格式的日期字符
  class.prototype.parse = function(state, date){
    var that = this
    ,options = that.config
    ,datetime = date || (state 
      ? lay.extend({}, that.enddate, that.endtime)
    : (options.range ? lay.extend({}, that.startdate, that.starttime) : options.datetime))
    ,format = that.format.concat();

    //转义为规定格式
    lay.each(format, function(i, item){
      if(/yyyy|y/.test(item)){ //年
        format[i] = lay.digit(datetime.year, item.length);
      } else if(/mm|m/.test(item)){ //月
        format[i] = lay.digit(datetime.month + 1, item.length);
      } else if(/dd|d/.test(item)){ //日
        format[i] = lay.digit(datetime.date, item.length);
      } else if(/hh|h/.test(item)){ //时
        format[i] = lay.digit(datetime.hours, item.length);
      } else if(/mm|m/.test(item)){ //分
        format[i] = lay.digit(datetime.minutes, item.length);
      } else if(/ss|s/.test(item)){ //秒
        format[i] = lay.digit(datetime.seconds, item.length);
      }
    });
    
    //返回日期范围字符
    if(options.range && !state){
      return format.join('') + ' '+ options.range +' ' + that.parse(1);
    }
    
    return format.join('');
  };
  
  //创建指定日期时间对象
  class.prototype.newdate = function(datetime){
    datetime = datetime || {};
    return new date(
      datetime.year || 1
      ,datetime.month || 0
      ,datetime.date || 1
      ,datetime.hours || 0
      ,datetime.minutes || 0
      ,datetime.seconds || 0
    );
  };
  
  //赋值
  class.prototype.setvalue = function(value){
    var that = this
    ,options = that.config
    ,elem = that.bindelem || options.elem[0]
    ,valtype = that.isinput(elem) ? 'val' : 'html'
    
    options.position === 'static' || lay(elem)[valtype](value || '');
    return this;
  };
  
  //标记范围内的日期
  class.prototype.stamprange = function(){
    var that = this
    ,options = that.config
    ,starttime, endtime
    ,tds = lay(that.elem).find('td');
    
    if(options.range && !that.enddate) lay(that.footer).find(elem_confirm).addclass(disabled);
    if(!that.enddate) return;

    starttime = that.newdate({
      year: that.startdate.year
      ,month: that.startdate.month
      ,date: that.startdate.date
    }).gettime();
    
    endtime = that.newdate({
      year: that.enddate.year
      ,month: that.enddate.month
      ,date: that.enddate.date
    }).gettime();
    
    if(starttime > endtime) return that.hint(tips_out);
    
    lay.each(tds, function(i, item){
      var ymd = lay(item).attr('lay-ymd').split('-')
      ,thistime = that.newdate({
        year: ymd[0]
        ,month: ymd[1] - 1
        ,date: ymd[2]
      }).gettime();
      lay(item).removeclass(elem_selected + ' ' + this);
      if(thistime === starttime || thistime === endtime){
        lay(item).addclass(
          lay(item).hasclass(elem_prev) || lay(item).hasclass(elem_next)
            ? elem_selected
          : this
        );
      }
      if(thistime > starttime && thistime < endtime){
        lay(item).addclass(elem_selected);
      }
    });
  };
  
  //执行done/change回调
  class.prototype.done = function(param, type){
    var that = this
    ,options = that.config
    ,start = lay.extend({}, that.startdate ? lay.extend(that.startdate, that.starttime) : options.datetime)
    ,end = lay.extend({}, lay.extend(that.enddate, that.endtime))
    
    lay.each([start, end], function(i, item){
      if(!('month' in item)) return;
      lay.extend(item, {
        month: item.month + 1
      });
    });
    
    param = param || [that.parse(), start, end];
    typeof options[type || 'done'] === 'function' && options[type || 'done'].apply(options, param);
    
    return that;
  };
  
  //选择日期
  class.prototype.choose = function(td){
    var that = this
    ,options = that.config
    ,datetime = options.datetime

    ,tds = lay(that.elem).find('td')
    ,ymd = td.attr('lay-ymd').split('-')
    
    ,setdatetime = function(one){
      var thisdate = new date();
      
      //同步datetime
      one && lay.extend(datetime, ymd);
      
      //记录开始日期
      if(options.range){
        that.startdate ? lay.extend(that.startdate, ymd) : (
          that.startdate = lay.extend({}, ymd, that.starttime)
        );
        that.startymd = ymd;
      }
    };
    
    ymd = {
      year: ymd[0] | 0
      ,month: (ymd[1] | 0) - 1
      ,date: ymd[2] | 0
    };
    
    if(td.hasclass(disabled)) return;

    //范围选择
    if(options.range){
      
      lay.each(['starttime', 'endtime'], function(i, item){
        that[item] = that[item] || {
          hours: 0
          ,minutes: 0
          ,seconds: 0
        };
      });
      
      if(that.endstate){ //重新选择
        setdatetime();
        delete that.endstate;
        delete that.enddate;
        that.startstate = true;
        tds.removeclass(this + ' ' + elem_selected);
        td.addclass(this);
      } else if(that.startstate){ //选中截止
        td.addclass(this);
        
        that.enddate ? lay.extend(that.enddate, ymd) : (
          that.enddate = lay.extend({}, ymd, that.endtime)
        );
        
        //判断是否顺时或逆时选择
        if(that.newdate(ymd).gettime() < that.newdate(that.startymd).gettime()){
          var startdate = lay.extend({}, that.enddate, {
            hours: that.startdate.hours
            ,minutes: that.startdate.minutes
            ,seconds: that.startdate.seconds
          });
          lay.extend(that.enddate, that.startdate, {
            hours: that.enddate.hours
            ,minutes: that.enddate.minutes
            ,seconds: that.enddate.seconds
          });
          that.startdate = startdate;
        }
        
        options.showbottom || that.done();
        that.stamprange(); //标记范围内的日期
        that.endstate = true;
        that.done(null, 'change');
      } else { //选中开始
        td.addclass(this);
        setdatetime(); 
        that.startstate = true;
      }
      lay(that.footer).find(elem_confirm)[that.enddate ? 'removeclass' : 'addclass'](disabled);
    } else if(options.position === 'static'){ //直接嵌套的选中
      setdatetime(true);
      that.calendar().done().done(null, 'change');
    } else if(options.type === 'date'){
      setdatetime(true);
      that.setvalue(that.parse()).remove().done();
    } else if(options.type === 'datetime'){
      setdatetime(true);
      that.calendar().done(null, 'change');
    }
  };
  
  //底部按钮
  class.prototype.tool = function(btn, type){
    var that = this
    ,options = that.config
    ,datetime = options.datetime
    ,isstatic = options.position === 'static'
    ,active = {
      //选择时间
      datetime: function(){
        if(lay(btn).hasclass(disabled)) return;
        that.list('time', 0);
        options.range && that.list('time', 1);
        lay(btn).attr('lay-type', 'date').html(that.lang().datetips);
      }
      
      //选择日期
      ,date: function(){
        that.closelist();
        lay(btn).attr('lay-type', 'datetime').html(that.lang().timetips);
      }
      
      //清空、重置
      ,clear: function(){
        that.setvalue('').remove();
        isstatic && (
          lay.extend(datetime, that.firstdate)
          ,that.calendar()
        )
        options.range && (
          delete that.startstate
          ,delete that.endstate
          ,delete that.enddate
          ,delete that.starttime
          ,delete that.endtime
        );
        that.done(['', {}, {}]);
      }
      
      //现在
      ,now: function(){
        var thisdate = new date();
        lay.extend(datetime, that.systemdate(), {
          hours: thisdate.gethours()
          ,minutes: thisdate.getminutes()
          ,seconds: thisdate.getseconds()
        });
        that.setvalue(that.parse()).remove();
        isstatic && that.calendar();
        that.done();
      }
      
      //确定
      ,confirm: function(){
        if(options.range){
          if(!that.enddate) return that.hint('请先选择日期范围');
          if(lay(btn).hasclass(disabled)) return that.hint(
            options.type === 'time' ? tips_out.replace(/日期/g, '时间') : tips_out
          );
        } else {
          if(lay(btn).hasclass(disabled)) return that.hint('不在有效日期或时间范围内');
        }
        that.done();
        that.setvalue(that.parse()).remove()
      }
    };
    active[type] && active[type]();
  };
  
  //统一切换处理
  class.prototype.change = function(index){
    var that = this
    ,options = that.config
    ,datetime = options.datetime
    ,isalone = options.range && (options.type === 'year' || options.type === 'month')
    
    ,elemcont = that.elemcont[index || 0]
    ,listym = that.listym[index]
    ,addsubyeay = function(type){
      var startend = ['startdate', 'enddate'][index]
      ,isyear = lay(elemcont).find('.laydate-year-list')[0]
      ,ismonth = lay(elemcont).find('.laydate-month-list')[0];
      
      //切换年列表
      if(isyear){
        listym[0] = type ? listym[0] - 15 : listym[0] + 15;
        that.list('year', index);
      }
      
      if(ismonth){ //切换月面板中的年
        type ? listym[0]-- : listym[0]++;
        that.list('month', index);
      }
      
      if(isyear || ismonth){
        lay.extend(datetime, {
          year: listym[0]
        });
        if(isalone) that[startend].year = listym[0];
        options.range || that.done(null, 'change');
        that.setbtnstatus();      
        options.range || that.limit(lay(that.footer).find(elem_confirm), {
          year: listym[0]
        });
      }
      return isyear || ismonth;
    };
    
    return {
      prevyear: function(){
        if(addsubyeay('sub')) return;
        datetime.year--;
        that.checkdate('limit').calendar();
        options.range || that.done(null, 'change');
      }
      ,prevmonth: function(){
        var ym = that.getasym(datetime.year, datetime.month, 'sub');
        lay.extend(datetime, {
          year: ym[0]
          ,month: ym[1]
        });
        that.checkdate('limit').calendar();
        options.range || that.done(null, 'change');
      }
      ,nextmonth: function(){
        var ym = that.getasym(datetime.year, datetime.month);
        lay.extend(datetime, {
          year: ym[0]
          ,month: ym[1]
        });
        that.checkdate('limit').calendar();
        options.range || that.done(null, 'change');
      }
      ,nextyear: function(){
        if(addsubyeay()) return;
        datetime.year++
        that.checkdate('limit').calendar();
        options.range || that.done(null, 'change');
      }
    };
  };
  
  //日期切换事件
  class.prototype.changeevent = function(){
    var that = this
    ,options = that.config;

    //日期选择事件
    lay(that.elem).on('click', function(e){
      lay.stope(e);
    });
    
    //年月切换
    lay.each(that.elemheader, function(i, header){
      //上一年
      lay(header[0]).on('click', function(e){
        that.change(i).prevyear();
      });
      
      //上一月
      lay(header[1]).on('click', function(e){
        that.change(i).prevmonth();
      });
      
      //选择年月
      lay(header[2]).find('span').on('click', function(e){
        var othis = lay(this)
        ,layym = othis.attr('lay-ym')
        ,laytype = othis.attr('lay-type');
        
        if(!layym) return;
        
        layym = layym.split('-');

        that.listym[i] = [layym[0] | 0, layym[1] | 0];
        that.list(laytype, i);
        lay(that.footer).find(elem_time_btn).addclass(disabled);
      });

      //下一月
      lay(header[3]).on('click', function(e){
        that.change(i).nextmonth();
      });
      
      //下一年
      lay(header[4]).on('click', function(e){
        that.change(i).nextyear();
      });
    });
    
    //点击日期
    lay.each(that.table, function(i, table){
      var tds = lay(table).find('td');
      tds.on('click', function(){
        that.choose(lay(this));
      });
    });
    
    //点击底部按钮
    lay(that.footer).find('span').on('click', function(){
      var type = lay(this).attr('lay-type');
      that.tool(this, type);
    });
  };
  
  //是否输入框
  class.prototype.isinput = function(elem){
    return /input|textarea/.test(elem.tagname.tolocalelowercase());
  };

  //绑定的元素事件处理
  class.prototype.events = function(){
    var that = this
    ,options = that.config

    //绑定呼出控件事件
    ,showevent = function(elem, bind){
      elem.on(options.trigger, function(){
        bind && (that.bindelem = this);
        that.render();
      });
    };
    
    if(!options.elem[0] || options.elem[0].eventhandler) return;
    
    showevent(options.elem, 'bind');
    showevent(options.eventelem);
    
    //绑定关闭控件事件
    lay(document).on('click', function(e){
      if(e.target === options.elem[0] 
      || e.target === options.eventelem[0]
      || e.target === lay(options.closestop)[0]){
        return;
      }
      that.remove();
    }).on('keydown', function(e){
      if(e.keycode === 13){
        if(lay('#'+ that.elemid)[0] && that.elemid === class.thiselem){
          e.preventdefault();
          lay(that.footer).find(elem_confirm)[0].click();
        }
      }
    });
    
    //自适应定位
    lay(window).on('resize', function(){
      if(!that.elem || !lay(elem)[0]){
        return false;
      }
      that.position();
    });
    
    options.elem[0].eventhandler = true;
  };

  
  //核心接口
  laydate.render = function(options){
    var inst = new class(options);
    return thisdate.call(inst);
  };
  
  //得到某月的最后一天
  laydate.getenddate = function(month, year){
    var thisdate = new date();
    //设置日期为下个月的第一天
    thisdate.setfullyear(
      year || thisdate.getfullyear()
      ,month || (thisdate.getmonth() + 1)
    ,1);
    //减去一天,得到当前月最后一天
    return new date(thisdate.gettime() - 1000*60*60*24).getdate();
  };
  
  //暴露lay
  window.lay = window.lay || lay;
  
  //加载方式
  islayui ? (
    laydate.ready()
    ,layui.define(function(exports){ //layui加载
      laydate.path = layui.cache.dir;
      exports(mod_name, laydate);
    })
  ) : (
    (typeof define === 'function' && define.amd) ? define(function(){ //requirejs加载
      return laydate;
    }) : function(){ //普通script标签加载
      laydate.ready();
      window.laydate = laydate
    }()
  );

}();