简单日历插件jquery.date_input.pack

html:

<link rel="stylesheet" type="text/css" href="css/jquery.date_input.pack.css">
......
<script type="text/javascript" src="js/jquery.date_input.pack.js"></script>
......
<script type="text/javascript">
  $(function(){
    $(‘.date_picker‘).date_input();
  })
</script>
<body>
<input placeholder="日期" style="width:250px; line-height:23px;display:inline-block" type="text" class="date_picker" id="datatimeo">
</body>

jquery.date_input.pack.js:

DateInput = (function($) {
    function DateInput(el, opts) {
        if (typeof(opts) != "object") opts = {};
        $.extend(this, DateInput.DEFAULT_OPTS, opts);
        this.input = $(el);
        this.bindMethodsToObj("show", "hide", "hideIfClickOutside", "keydownHandler", "selectDate");
        this.build();
        this.selectDate();
        this.hide()
    };
    DateInput.DEFAULT_OPTS = {
        month_names: ["一月份", "二月份", "三月份", "四月份", "五月份", "六月份", "七月份", "八月份", "九月份", "十月份", "十一月份", "十二月份"],
        short_month_names: ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"],
        short_day_names: ["日", "一", "二", "三", "四", "五", "六"],
        start_of_week: 1
    };
    DateInput.prototype = {
        build: function() {
            var monthNav = $(‘<p class="month_nav">‘ + ‘<span class="button prev" title="[Page-Up]">«</span>‘ + ‘ <span class="month_name"></span> ‘ + ‘<span class="button next" title="[Page-Down]">»</span>‘ + ‘</p>‘);
            this.monthNameSpan = $(".month_name", monthNav);
            $(".prev", monthNav).click(this.bindToObj(function() {
                this.moveMonthBy( - 1)
            }));
            $(".next", monthNav).click(this.bindToObj(function() {
                this.moveMonthBy(1)
            }));
            var yearNav = $(‘<p class="year_nav">‘ + ‘<span class="button prev" title="[Ctrl+Page-Up]">«</span>‘ + ‘ <span class="year_name"></span> ‘ + ‘<span class="button next" title="[Ctrl+Page-Down]">»</span>‘ + ‘</p>‘);
            this.yearNameSpan = $(".year_name", yearNav);
            $(".prev", yearNav).click(this.bindToObj(function() {
                this.moveMonthBy( - 12)
            }));
            $(".next", yearNav).click(this.bindToObj(function() {
                this.moveMonthBy(12)
            }));
            var nav = $(‘<div class="nav"></div>‘).append(monthNav, yearNav);
            var tableShell = "<table><thead><tr>";
            $(this.adjustDays(this.short_day_names)).each(function() {
                tableShell += "<th>" + this + "</th>"
            });
            tableShell += "</tr></thead><tbody></tbody></table>";
            this.dateSelector = this.rootLayers = $(‘<div class="date_selector"></div>‘).append(nav, tableShell).insertAfter(this.input);
            if ($.browser.msie && $.browser.version < 7) {
                this.ieframe = $(‘<iframe class="date_selector_ieframe" frameborder="0" src="#"></iframe>‘).insertBefore(this.dateSelector);
                this.rootLayers = this.rootLayers.add(this.ieframe);
                $(".button", nav).mouseover(function() {
                    $(this).addClass("hover")
                });
                $(".button", nav).mouseout(function() {
                    $(this).removeClass("hover")
                })
            };
            this.tbody = $("tbody", this.dateSelector);
            this.input.change(this.bindToObj(function() {
                this.selectDate()
            }));
            this.selectDate()
        },
        selectMonth: function(date) {
            var newMonth = new Date(date.getFullYear(), date.getMonth(), 1);
            if (!this.currentMonth || !(this.currentMonth.getFullYear() == newMonth.getFullYear() && this.currentMonth.getMonth() == newMonth.getMonth())) {
                this.currentMonth = newMonth;
                var rangeStart = this.rangeStart(date),
                rangeEnd = this.rangeEnd(date);
                var numDays = this.daysBetween(rangeStart, rangeEnd);
                var dayCells = "";
                for (var i = 0; i <= numDays; i++) {
                    var currentDay = new Date(rangeStart.getFullYear(), rangeStart.getMonth(), rangeStart.getDate() + i, 12, 00);
                    if (this.isFirstDayOfWeek(currentDay)) dayCells += "<tr>";
                    if (currentDay.getMonth() == date.getMonth()) {
                        dayCells += ‘<td class="selectable_day" date="‘ + this.dateToString(currentDay) + ‘">‘ + currentDay.getDate() + ‘</td>‘
                    } else {
                        dayCells += ‘<td class="unselected_month" date="‘ + this.dateToString(currentDay) + ‘">‘ + currentDay.getDate() + ‘</td>‘
                    };
                    if (this.isLastDayOfWeek(currentDay)) dayCells += "</tr>"
                };
                this.tbody.empty().append(dayCells);
                this.monthNameSpan.empty().append(this.monthName(date));
                this.yearNameSpan.empty().append(this.currentMonth.getFullYear());
                $(".selectable_day", this.tbody).click(this.bindToObj(function(event) {
                    this.changeInput($(event.target).attr("date"))
                }));
                $("td[date=" + this.dateToString(new Date()) + "]", this.tbody).addClass("today");
                $("td.selectable_day", this.tbody).mouseover(function() {
                    $(this).addClass("hover")
                });
                $("td.selectable_day", this.tbody).mouseout(function() {
                    $(this).removeClass("hover")
                })
            };
            $(‘.selected‘, this.tbody).removeClass("selected");
            $(‘td[date=‘ + this.selectedDateString + ‘]‘, this.tbody).addClass("selected")
        },
        selectDate: function(date) {
            if (typeof(date) == "undefined") {
                date = this.stringToDate(this.input.val())
            };
            if (!date) date = new Date();
            this.selectedDate = date;
            this.selectedDateString = this.dateToString(this.selectedDate);
            this.selectMonth(this.selectedDate)
        },
        changeInput: function(dateString) {
            this.input.val(dateString).change();
            this.hide()
        },
        show: function() {
            this.rootLayers.css("display", "block");
            $([window, document.body]).click(this.hideIfClickOutside);
            this.input.unbind("focus", this.show);
            $(document.body).keydown(this.keydownHandler);
            this.setPosition()
        },
        hide: function() {
            this.rootLayers.css("display", "none");
            $([window, document.body]).unbind("click", this.hideIfClickOutside);
            this.input.focus(this.show);
            $(document.body).unbind("keydown", this.keydownHandler)
        },
        hideIfClickOutside: function(event) {
            if (event.target != this.input[0] && !this.insideSelector(event)) {
                this.hide()
            }
        },
        insideSelector: function(event) {
            var offset = this.dateSelector.position();
            offset.right = offset.left + this.dateSelector.outerWidth();
            offset.bottom = offset.top + this.dateSelector.outerHeight();
            return event.pageY < offset.bottom && event.pageY > offset.top && event.pageX < offset.right && event.pageX > offset.left
        },
        keydownHandler: function(event) {
            switch (event.keyCode) {
            case 9:
            case 27:
                this.hide();
                return;
                break;
            case 13:
                this.changeInput(this.selectedDateString);
                break;
            case 33:
                this.moveDateMonthBy(event.ctrlKey ? -12 : -1);
                break;
            case 34:
                this.moveDateMonthBy(event.ctrlKey ? 12 : 1);
                break;
            case 38:
                this.moveDateBy( - 7);
                break;
            case 40:
                this.moveDateBy(7);
                break;
            case 37:
                this.moveDateBy( - 1);
                break;
            case 39:
                this.moveDateBy(1);
                break;
            default:
                return
            }
            event.preventDefault()
        },
        stringToDate: function(string) {
            var matches;
            if (matches = string.match(/^(\d{1,2}) ([^\s]+) (\d{4,4})$/)) {
                return new Date(matches[3], this.shortMonthNum(matches[2]), matches[1], 12, 00)
            } else {
                return null
            }
        },
        dateToString: function(date) {
            return date.getFullYear()+"-"+this.short_month_names[date.getMonth()]+"-" +date.getDate()
        },
        setPosition: function() {
            var offset = this.input.offset();
            this.rootLayers.css({
                top: offset.top + this.input.outerHeight(),
                left: offset.left
            });
            if (this.ieframe) {
                this.ieframe.css({
                    width: this.dateSelector.outerWidth(),
                    height: this.dateSelector.outerHeight()
                })
            }
        },
        moveDateBy: function(amount) {
            var newDate = new Date(this.selectedDate.getFullYear(), this.selectedDate.getMonth(), this.selectedDate.getDate() + amount);
            this.selectDate(newDate)
        },
        moveDateMonthBy: function(amount) {
            var newDate = new Date(this.selectedDate.getFullYear(), this.selectedDate.getMonth() + amount, this.selectedDate.getDate());
            if (newDate.getMonth() == this.selectedDate.getMonth() + amount + 1) {
                newDate.setDate(0)
            };
            this.selectDate(newDate)
        },
        moveMonthBy: function(amount) {
            var newMonth = new Date(this.currentMonth.getFullYear(), this.currentMonth.getMonth() + amount, this.currentMonth.getDate());
            this.selectMonth(newMonth)
        },
        monthName: function(date) {
            return this.month_names[date.getMonth()]
        },
        bindToObj: function(fn) {
            var self = this;
            return function() {
                return fn.apply(self, arguments)
            }
        },
        bindMethodsToObj: function() {
            for (var i = 0; i < arguments.length; i++) {
                this[arguments[i]] = this.bindToObj(this[arguments[i]])
            }
        },
        indexFor: function(array, value) {
            for (var i = 0; i < array.length; i++) {
                if (value == array[i]) return i
            }
        },
        monthNum: function(month_name) {
            return this.indexFor(this.month_names, month_name)
        },
        shortMonthNum: function(month_name) {
            return this.indexFor(this.short_month_names, month_name)
        },
        shortDayNum: function(day_name) {
            return this.indexFor(this.short_day_names, day_name)
        },
        daysBetween: function(start, end) {
            start = Date.UTC(start.getFullYear(), start.getMonth(), start.getDate());
            end = Date.UTC(end.getFullYear(), end.getMonth(), end.getDate());
            return (end - start) / 86400000
        },
        changeDayTo: function(dayOfWeek, date, direction) {
            var difference = direction * (Math.abs(date.getDay() - dayOfWeek - (direction * 7)) % 7);
            return new Date(date.getFullYear(), date.getMonth(), date.getDate() + difference)
        },
        rangeStart: function(date) {
            return this.changeDayTo(this.start_of_week, new Date(date.getFullYear(), date.getMonth()), -1)
        },
        rangeEnd: function(date) {
            return this.changeDayTo((this.start_of_week - 1) % 7, new Date(date.getFullYear(), date.getMonth() + 1, 0), 1)
        },
        isFirstDayOfWeek: function(date) {
            return date.getDay() == this.start_of_week
        },
        isLastDayOfWeek: function(date) {
            return date.getDay() == (this.start_of_week - 1) % 7
        },
        adjustDays: function(days) {
            var newDays = [];
            for (var i = 0; i < days.length; i++) {
                newDays[i] = days[(i + this.start_of_week) % 7]
            };
            return newDays
        }
    };
    $.fn.date_input = function(opts) {
        return this.each(function() {
            new DateInput(this, opts)
        })
    };
    $.date_input = {
        initialize: function(opts) {
            $("input.date_input").date_input(opts)
        }
    };
    return DateInput
})(jQuery);

jQuery.browser={};(function(){jQuery.browser.msie=false; jQuery.browser.version=0;if(navigator.userAgent.match(/MSIE ([0-9]+)./)){ jQuery.browser.msie=true;jQuery.browser.version=RegExp.$1;}})();

jquery.date_input.pack.css:

.date_selector, .date_selector *{width: auto;height: auto;border: none;background: none;margin: 0;padding: 0;text-align: left;text-decoration: none;}

.date_selector{background:#fbfbfb;border: 1px solid #ccc;padding: 10px;margin:0;margin-top:-1px;position: absolute;z-index:100000;display:none;border-radius: 3px;box-shadow: 0 0 5px #aaa;box-shadow:0 2px 2px #ccc;}

.date_selector_ieframe{position: absolute;z-index: 99999;display: none;}

.date_selector .nav{width: 17.5em;}

.date_selector .nav p{clear: none;}

.date_selector .month_nav, .date_selector .year_nav{margin: 0 0 3px 0;padding: 0;display: block;position: relative;text-align: center;}

.date_selector .month_nav{float: left;width: 55%;}

.date_selector .year_nav{float: right;width: 42%;margin-right: -8px;}

.date_selector .month_name, .date_selector .year_name{font-weight: bold;line-height: 20px;}

.date_selector .button{display: block;position: absolute;top: 0;width:18px;height:18px;line-height:16px;font-weight:bold;color:#5985c7;text-align: center;font-size:12px;overflow:hidden;border: 1px solid #ccc;border-radius:2px;}

.date_selector .button:hover, .date_selector .button.hover{background:#5985c7;color: #fff;cursor: pointer;border-color:#3a930d;}

.date_selector .prev{left: 0;}

.date_selector .next{right: 0;}

.date_selector table{border-spacing: 0;border-collapse: collapse;clear: both;margin: 0; width:220px;}

.date_selector th, .date_selector td{width: 2.5em;height: 2em;padding: 0 !important;text-align: center !important;color: #666;font-weight: normal;}

.date_selector th{font-size: 12px;}

.date_selector td{border:1px solid #f1f1f1;line-height: 2em;text-align: center;white-space: nowrap;color:#5985c7;background: #fff;}

.date_selector td.today{background: #eee;}

.date_selector td.unselected_month{color: #ccc;}

.date_selector td.selectable_day{cursor: pointer;}

.date_selector td.selected{background:#2b579a;color: #fff;font-weight: bold;}

.date_selector td.selectable_day:hover, .date_selector td.selectable_day.hover{background:#5985c7;color: #fff;}
时间: 2024-10-14 20:17:14

简单日历插件jquery.date_input.pack的相关文章

jQuery简单日历插件版

先来看demo:http://codepen.io/jonechen/pen/xOgZMz 插件代码: ; (function($) { var Calendar = function(ele, options) { this.ele = ele; this.opt = options; this.defaults = { color: 'blue', fontsize: '14px', } this.obj = $.extend({}, this.defaults, this.opt); };

10个漂亮的jQuery日历插件下载【转载】

10个漂亮的jQuery日历插件下载 2013-08-07 标签:jQuery日历插件jQuery日历jQuery插件 日期是非常重要的,随时随地.微薄或网站的日期选取器日历必须在那里.您可以使用任何的jQuery日历插件,对于任何不同的网络应用程序,如自由职业者的计费应用程序,事件管理应用程序或任何日期是非常重要的.今天,我们搜集了10个美丽的jQuery日历插件,你可以随时随地使用下载应用. glDatePicker glDatePicker是一种简单的,可定制的,轻巧的日期选择器. jQu

jQuery简单备忘录功能的日历插件

e-calendar是一款jQuery简单带备忘录功能的日历插件.通过该jQuery日历插件你可以记录或设置某天某个时刻发生的事情,具有工作备忘录的功能.通过简单的参数设置你就可以定制该日历插件的外观. 效果演示:http://www.htmleaf.com/Demo/201503281589.html 下载地址:http://www.htmleaf.com/jQuery/Calendar-Date-Time-picker/201503281588.html

jQuery插件之——简单日历

最近在研究js插件的开发,以前看大神们,对插件都是信手拈来,随便玩弄,感觉自己要是达到那种水平就好了,就开始自己研究插件开发了.研究了一段时间之后,就开始写了自己的第一个日历插件,由于是初学插件开发,代码的可读性可能有点差,希望各位大神可以多提点意见,以后维护代码,让这个插件更加的完整.下面就贴出代码. 首先,给插件来个整体的div容器 <div class="y-total"></div> 本人习惯给容器取class或id名时,加上自己独特的前缀,这样,有助于

一个简单的页面弹窗插件 jquery.pageMsgFrame.js

页面弹窗是网站中常用的交互效果,它可以强提示网站的某些信息给用户,或者作用于某些信息的修改等等功能. 这几天在做一个项目的时候,就顺捎把这个插件写一下,栽棵树,自己乘凉吧. 原创博文,转载请注明出处:http://www.cnblogs.com/dereksunok/p/3724764.html html代码: 1 <!doctype html> 2 <html> 3 <head> 4 <meta charset="utf-8" /> 5

【UI插件】简单的日历插件(下)—— 学习MVC思想

前言 我们上次写了一个简单的日历插件,但是只是一个半成品,而且做完后发现一些问题,于是我们今天尝试来解决这些问题 PS:距离上次貌似很久了 上次,我们大概遇到哪些问题呢: ① 既然想做一套UI库,那么就应该考虑其它UI库的接入问题 这个意思就是,我们的系统中所有UI插件应该有一些统一行为,我们如果希望统一为所有的插件加一点什么东西,需要有位置可加 这个意味着,可能我们所有的插件需要继承至一个抽象的UI类,并且该类提供了通用的几个事件点 ② 上次做的日历插件虽然说是简单,其耦合还是比较严重的(其实

基于jQuery的日历插件

上个星期看到同事做一个有关日历提醒功能的需求,为了找个插件也是费了不少心思,然后刚好有时间就试着写了一个简单demo 来看下最终效果图吧: 是长得丑了一点,不要吐槽我-.- 首先来说说这个日历主要的制作逻辑吧: ·一个月份最多有31天,需要一个7X6的表格去装载 ·如果知道了某个月份1号是星期几,这个月份有多少天,一个循环就可以显示某个月的日历了吧(眼睛都放光了*.*) ·加上一些控件让用户可以方便操作吧(比如可以输入年份.月份,可以点击选择年份.月份) 新建一个html文件,html结构: <

JavaScript之jQuery-7 jQuery 使用插件(使用插件、日历插件、表单验证插件)

一.jQuery 使用插件 插件的查找与帮助 - jQuery 官方网站的插件库(http://plugins.jquery.com) 提供了大量的插件.并给出去了每个插件的用户评级.版本及bug等 - 库中列出了每个插件的ZIP文件下载.演示.示例代码及教程 使用插件 - step 1:将插件包导入到页面中,并确保它在jQuery源文件之后 <script src="jqeury-1.11.1.js"></script> <script></

JQuery日历插件My97DatePicker日期范围限制

转:http://www.xiongge.club/744.html My97DatePicker是一个非常优秀的日历插件,不仅支持多种调用模式,还支持日期范围限制. 常规的调用比较简单,如下所示: 1 <input class="Wdate" id="d1" onclick="WdatePicker()" /> 下面重点说明日期范围限制: 1)静态限制 你可以给通过配置minDate(最小日期),maxDate(最大日期)为静态日期