Chinaunix首页 | 论坛 | 博客
  • 博客访问: 486727
  • 博文数量: 226
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2111
  • 用 户 组: 普通用户
  • 注册时间: 2014-06-20 09:02
个人简介

web web web

文章分类

全部博文(226)

文章存档

2020年(2)

2019年(1)

2018年(3)

2017年(26)

2016年(57)

2015年(60)

2014年(77)

我的朋友

分类: Web开发

2014-10-14 15:25:18

先看一个简单的日历控件如下所示:

实现


用到的img


                                                                                               

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">

  1. <html xmlns="">
  2. <head>
  3.     <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />
  4.     <title>Simple Tigra Calendar</title>
  5.     
  6.     <link rel="stylesheet" type="text/css" href="tcal.css" />
  7.     <script type="text/javascript" src="tcal.js"></script>
  8. </head>
  9. <body>
  10.     <form action="#">
  11.         
  12.         <div><input type="text" name="date" class="tcal" value="" /></div>
  13.     </form>
  14. </body>
  15. </html>
  1. /*
  2.     Stylesheet for Tigra Calendar v5.0
  3.     Product is Public Domain (Free for any kind of applicaiton, customization and derivative works are allowed)
  4.     URL:

  5.     - all image paths are relative to path of stylesheet
  6.     - the styles below can be moved into the document or in existing stylesheet

  7. */

  8. /* input box in default state */
  9. .tcalInput {
  10.     background: url('img/cal.gif') 100% 50% no-repeat;
  11.     padding-right: 20px;
  12.     cursor: pointer;
  13. }

  14. /* additional properties for input boxe in activated state, above still applies unless in conflict */
  15. .tcalActive {
  16.     background-image: url('img/no_cal.gif');
  17. }
  18. /* container of calendar's pop-up */
  19. #tcal {
  20.     position: absolute;
  21.     visibility: hidden;
  22.     z-index: 100;
  23.     width: 170px;
  24.     background-color: white;
  25.     margin-top: 2px;
  26.     padding: 0 2px 2px 2px;
  27.     border: 1px solid silver;

  28.     -moz-box-shadow: 3px 3px 4px silver;
  29.     -webkit-box-shadow: 3px 3px 4px silver;
  30.     box-shadow: 3px 3px 4px silver;
  31.     -ms-filter: "progid:DXImageTransform.Microsoft.Shadow(Strength=4, Direction=135, Color='silver')";
  32.     filter: progid:DXImageTransform.Microsoft.Shadow(Strength=4, Direction=135, Color='silver');
  33. }

  34. /* table containing navigation and current month */
  35. #tcalControls {
  36.     border-collapse: collapse;
  37.     border: 0;
  38.     width: 100%;
  39. }
  40. #tcalControls td {
  41.     border-collapse: collapse;
  42.     border: 0;
  43.     padding: 0;
  44.     width: 16px;
  45.     background-position: 50% 50%;
  46.     background-repeat: no-repeat;
  47.     cursor: pointer;
  48. }
  49. #tcalControls th {
  50.     border-collapse: collapse;
  51.     border: 0;
  52.     padding: 0;
  53.     line-height: 25px;
  54.     font-size: 10px;
  55.     text-align: center;
  56.     font-family: Tahoma, Geneva, sans-serif;
  57.     font-weight: bold;
  58.     white-space: nowrap;
  59. }
  60. #tcalPrevYear { background-image: url('img/prev_year.gif'); }
  61. #tcalPrevMonth { background-image: url('img/prev_mon.gif'); }
  62. #tcalNextMonth { background-image: url('img/next_mon.gif'); }
  63. #tcalNextYear { background-image: url('img/next_year.gif'); }

  64. /* table containing week days header and calendar grid */
  65. #tcalGrid {
  66.     border-collapse: collapse;
  67.     border: 1px solid silver;
  68.     width: 100%;
  69. }
  70. #tcalGrid th {
  71.     border: 1px solid silver;
  72.     border-collapse: collapse;
  73.     padding: 3px 0;
  74.     text-align: center;
  75.     font-family: Tahoma, Geneva, sans-serif;
  76.     font-size: 10px;
  77.     background-color: gray;
  78.     color: white;
  79. }
  80. #tcalGrid td {
  81.     border: 0;
  82.     border-collapse: collapse;
  83.     padding: 2px 0;
  84.     text-align: center;
  85.     font-family: Tahoma, Geneva, sans-serif;
  86.     width: 14%;
  87.     font-size: 11px;
  88.     cursor: pointer;
  89. }        
  90. #tcalGrid td.tcalOtherMonth { color: silver; }
  91. #tcalGrid td.tcalWeekend { background-color: #ACD6F5; }
  92. #tcalGrid td.tcalToday { border: 1px solid red; }
  93. #tcalGrid td.tcalSelected { background-color: #FFB3BE; }

 tcal.js


点击(此处)折叠或打开

  1. // Tigra Calendar v5.2 (11/20/2011)
  2. //
  3. // License: Public Domain... You're welcome.

  4. // default settins - this structure can be moved in separate file in multilangual applications
  5. var A_TCALCONF = {
  6.     'cssprefix' : 'tcal',
  7.     'months' : ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
  8.     'weekdays' : ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'],
  9.     'longwdays' : ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
  10.     'yearscroll' : true, // show year scroller
  11.     'weekstart' : 0, // first day of week: 0-Su or 1-Mo
  12.     'prevyear' : 'Previous Year',
  13.     'nextyear' : 'Next Year',
  14.     'prevmonth' : 'Previous Month',
  15.     'nextmonth' : 'Next Month',
  16.     'format' : 'm/d/Y' // 'd-m-Y', Y-m-d', 'l, F jS Y'
  17. };

  18. var A_TCALTOKENS = [
  19.      // A full numeric representation of a year, 4 digits
  20.     {'t': 'Y', 'r': '19\\d{2}|20\\d{2}', 'p': function (d_date, n_value) { d_date.setFullYear(Number(n_value)); return d_date; }, 'g': function (d_date) { var n_year = d_date.getFullYear(); return n_year; }},
  21.      // Numeric representation of a month, with leading zeros
  22.     {'t': 'm', 'r': '0?[1-9]|1[0-2]', 'p': function (d_date, n_value) { d_date.setMonth(Number(n_value) - 1); return d_date; }, 'g': function (d_date) { var n_month = d_date.getMonth() + 1; return (n_month < 10 ? '0' : '') + n_month }},
  23.      // A full textual representation of a month, such as January or March
  24.     {'t': 'F', 'r': A_TCALCONF.months.join('|'), 'p': function (d_date, s_value) { for (var m = 0; m < 12; m++) if (A_TCALCONF.months[m] == s_value) { d_date.setMonth(m); return d_date; }}, 'g': function (d_date) { return A_TCALCONF.months[d_date.getMonth()]; }},
  25.      // Day of the month, 2 digits with leading zeros
  26.     {'t': 'd', 'r': '0?[1-9]|[12][0-9]|3[01]', 'p': function (d_date, n_value) { d_date.setDate(Number(n_value)); if (d_date.getDate() != n_value) d_date.setDate(0); return d_date }, 'g': function (d_date) { var n_date = d_date.getDate(); return (n_date < 10 ? '0' : '') + n_date; }},
  27.     // Day of the month without leading zeros
  28.     {'t': 'j', 'r': '0?[1-9]|[12][0-9]|3[01]', 'p': function (d_date, n_value) { d_date.setDate(Number(n_value)); if (d_date.getDate() != n_value) d_date.setDate(0); return d_date }, 'g': function (d_date) { var n_date = d_date.getDate(); return n_date; }},
  29.      // A full textual representation of the day of the week
  30.     {'t': 'l', 'r': A_TCALCONF.longwdays.join('|'), 'p': function (d_date, s_value) { return d_date }, 'g': function (d_date) { return A_TCALCONF.longwdays[d_date.getDay()]; }},
  31.     // English ordinal suffix for the day of the month, 2 characters
  32.     {'t': 'S', 'r': 'st|nd|rd|th', 'p': function (d_date, s_value) { return d_date }, 'g': function (d_date) { n_date = d_date.getDate(); if (n_date % 10 == 1 && n_date != 11) return 'st'; if (n_date % 10 == 2 && n_date != 12) return 'nd'; if (n_date % 10 == 3 && n_date != 13) return 'rd'; return 'th'; }}
  33.     
  34. ];

  35. function f_tcalGetHTML (d_date) {

  36.     var e_input = f_tcalGetInputs(true);
  37.     if (!e_input) return;

  38.     var s_pfx = A_TCALCONF.cssprefix,
  39.         s_format = A_TCALCONF.format;

  40.     // today from config or client date
  41.     var d_today = f_tcalParseDate(A_TCALCONF.today, A_TCALCONF.format);
  42.     if (!d_today)
  43.         d_today = f_tcalResetTime(new Date());

  44.     // selected date from input or config or today
  45.     var d_selected = f_tcalParseDate(e_input.value, s_format);
  46.     if (!d_selected)
  47.         d_selected = f_tcalParseDate(A_TCALCONF.selected, A_TCALCONF.format);
  48.     if (!d_selected)
  49.         d_selected = new Date(d_today);
  50.     
  51.     // show calendar for passed or selected date
  52.     d_date = d_date ? f_tcalResetTime(d_date) : new Date(d_selected);

  53.     var d_firstDay = new Date(d_date);
  54.     d_firstDay.setDate(1);
  55.     d_firstDay.setDate(1 - (7 + d_firstDay.getDay() - A_TCALCONF.weekstart) % 7);

  56.     var a_class, s_html = '+ s_pfx +'Controls">'
  57.         + (A_TCALCONF.yearscroll ? '
  58. ':'')
  59.         + '
  60. '
  61.         + (A_TCALCONF.yearscroll ? '
  62. ':'')
  63.         + '
  64. + s_pfx + 'PrevYear" ' + f_tcalRelDate(d_date, -1, 'y') + ' title="' + A_TCALCONF.prevyear + '"> + s_pfx + 'PrevMonth"' + f_tcalRelDate(d_date, -1) + ' title="' + A_TCALCONF.prevmonth + '">'
  65.         + A_TCALCONF.months[d_date.getMonth()] + ' ' + d_date.getFullYear()
  66.         + '
  67. + s_pfx + 'NextMonth"' + f_tcalRelDate(d_date, 1) + ' title="' + A_TCALCONF.nextmonth + '"> + s_pfx + 'NextYear"' + f_tcalRelDate(d_date, 1, 'y') + ' title="' + A_TCALCONF.nextyear + '">
    + s_pfx +'Grid">';

  68.     // print weekdays titles
  69.     for (var i = 0; i < 7; i++)
  70.         s_html += '
  71. ';
  72.     s_html += '
  73. ';

  74.     // print calendar table
  75.     var n_date, n_month, d_current = new Date(d_firstDay);
  76.     while (d_current.getMonth() == d_date.getMonth() ||
  77.         d_current.getMonth() == d_firstDay.getMonth()) {

  78.         s_html +='
  79. ';
  80.         for (var n_wday = 0; n_wday < 7; n_wday++) {

  81.             a_class = [];
  82.             n_date = d_current.getDate();
  83.             n_month = d_current.getMonth();

  84.             if (d_current.getMonth() != d_date.getMonth())
  85.                 a_class[a_class.length] = s_pfx + 'OtherMonth';
  86.             if (d_current.getDay() == 0 || d_current.getDay() == 6)
  87.                 a_class[a_class.length] = s_pfx + 'Weekend';
  88.             if (d_current.valueOf() == d_today.valueOf())
  89.                 a_class[a_class.length] = s_pfx + 'Today';
  90.             if (d_current.valueOf() == d_selected.valueOf())
  91.                 a_class[a_class.length] = s_pfx + 'Selected';

  92.             s_html += ' + f_tcalRelDate(d_current) + (a_class.length ? ' class="' + a_class.join(' ') + '">' : '>') + n_date + '';
  93.             d_current.setDate(++n_date);
  94.         }
  95.         s_html +='
  96. ';
  97.     }
  98.     s_html +='
  99. ' + A_TCALCONF.weekdays[(A_TCALCONF.weekstart + i) % 7] + '
    '
    ;

  100.     return s_html;
  101. }

  102. function f_tcalRelDate (d_date, d_diff, s_units) {

  103.     var s_units = (s_units == 'y' ? 'FullYear' : 'Month');
  104.     var d_result = new Date(d_date);
  105.     if (d_diff) {
  106.         d_result['set' + s_units](d_date['get' + s_units]() + d_diff);
  107.         if (d_result.getDate() != d_date.getDate())
  108.             d_result.setDate(0);
  109.     }
  110.     return ' onclick="f_tcalUpdate(' + d_result.valueOf() + (d_diff ? ',1' : '') + ')"';
  111. }

  112. function f_tcalResetTime (d_date) {
  113.     d_date.setMilliseconds(0);
  114.     d_date.setSeconds(0);
  115.     d_date.setMinutes(0);
  116.     d_date.setHours(12);
  117.     return d_date;
  118. }

  119. // closes calendar and returns all inputs to default state
  120. function f_tcalCancel () {
  121.     
  122.     var s_pfx = A_TCALCONF.cssprefix;
  123.     var e_cal = document.getElementById(s_pfx);
  124.     if (e_cal)
  125.         e_cal.style.visibility = '';
  126.     var a_inputs = f_tcalGetInputs();
  127.     for (var n = 0; n < a_inputs.length; n++)
  128.         f_tcalRemoveClass(a_inputs[n], s_pfx + 'Active');
  129. }

  130. function f_tcalUpdate (n_date, b_keepOpen) {

  131.     var e_input = f_tcalGetInputs(true);
  132.     if (!e_input) return;
  133.     
  134.     d_date = new Date(n_date);
  135.     var s_pfx = A_TCALCONF.cssprefix;

  136.     if (b_keepOpen) {
  137.         var e_cal = document.getElementById(s_pfx);
  138.         if (!e_cal || e_cal.style.visibility != 'visible') return;
  139.         e_cal.innerHTML = f_tcalGetHTML(d_date, e_input);
  140.     }
  141.     else {
  142.         e_input.value = f_tcalGenerateDate(d_date, A_TCALCONF.format);
  143.         f_tcalCancel();
  144.     }
  145. }

  146. function f_tcalOnClick () {

  147.     // see if already opened
  148.     var s_pfx = A_TCALCONF.cssprefix;
  149.     var s_activeClass = s_pfx + 'Active';
  150.     var b_close = f_tcalHasClass(this, s_activeClass);

  151.     // close all clalendars
  152.     f_tcalCancel();
  153.     if (b_close) return;

  154.     // get position of input
  155.     f_tcalAddClass(this, s_activeClass);
  156.     
  157.     var n_left = f_getPosition (this, 'Left'),
  158.         n_top = f_getPosition (this, 'Top') + this.offsetHeight;

  159.     var e_cal = document.getElementById(s_pfx);
  160.     if (!e_cal) {
  161.         e_cal = document.createElement('div');
  162.         e_cal.onselectstart = function () { return false };
  163.         e_cal.id = s_pfx;
  164.         document.getElementsByTagName("body").item(0).appendChild(e_cal);
  165.     }
  166.     e_cal.innerHTML = f_tcalGetHTML(null);
  167.     e_cal.style.top = n_top + 'px';
  168.     e_cal.style.left = (n_left + this.offsetWidth - e_cal.offsetWidth) + 'px';
  169.     e_cal.style.visibility = 'visible';
  170. }

  171. function f_tcalParseDate (s_date, s_format) {

  172.     if (!s_date) return;

  173.     var s_char, s_regexp = '^', a_tokens = {}, a_options, n_token = 0;
  174.     for (var n = 0; n < s_format.length; n++) {
  175.         s_char = s_format.charAt(n);
  176.         if (A_TCALTOKENS_IDX[s_char]) {
  177.             a_tokens[s_char] = ++n_token;
  178.             s_regexp += '(' + A_TCALTOKENS_IDX[s_char]['r'] + ')';
  179.         }
  180.         else if (s_char == ' ')
  181.             s_regexp += '\\s';
  182.         else
  183.             s_regexp += (s_char.match(/[\w\d]/) ? '' : '\\') + s_char;
  184.     }
  185.     var r_date = new RegExp(s_regexp + '$');
  186.     if (!s_date.match(r_date)) return;
  187.     
  188.     var s_val, d_date = f_tcalResetTime(new Date());
  189.     d_date.setDate(1);

  190.     for (n = 0; n < A_TCALTOKENS.length; n++) {
  191.         s_char = A_TCALTOKENS[n]['t'];
  192.         if (!a_tokens[s_char])
  193.             continue;
  194.         s_val = RegExp['$' + a_tokens[s_char]];
  195.         d_date = A_TCALTOKENS[n]['p'](d_date, s_val);
  196.     }
  197.     
  198.     return d_date;
  199. }

  200. function f_tcalGenerateDate (d_date, s_format) {
  201.     
  202.     var s_char, s_date = '';
  203.     for (var n = 0; n < s_format.length; n++) {
  204.         s_char = s_format.charAt(n);
  205.         s_date += A_TCALTOKENS_IDX[s_char] ? A_TCALTOKENS_IDX[s_char]['g'](d_date) : s_char;
  206.     }
  207.     return s_date;
  208. }

  209. function f_tcalGetInputs (b_active) {

  210.     var a_inputs = document.getElementsByTagName('input'),
  211.         e_input, s_rel, a_result = [];

  212.     for (n = 0; n < a_inputs.length; n++) {

  213.         e_input = a_inputs[n];
  214.         if (!e_input.type || e_input.type != 'text')
  215.             continue;

  216.         if (!f_tcalHasClass(e_input, 'tcal'))
  217.             continue;

  218.         if (b_active && f_tcalHasClass(e_input, A_TCALCONF.cssprefix + 'Active'))
  219.             return e_input;

  220.         a_result[a_result.length] = e_input;
  221.     }
  222.     return b_active ? null : a_result;
  223. }

  224. function f_tcalHasClass (e_elem, s_class) {
  225.     var s_classes = e_elem.className;
  226.     if (!s_classes)
  227.         return false;
  228.     var a_classes = s_classes.split(' ');
  229.     for (var n = 0; n < a_classes.length; n++)
  230.         if (a_classes[n] == s_class)
  231.             return true;
  232.     return false;
  233. }

  234. function f_tcalAddClass (e_elem, s_class) {
  235.     if (f_tcalHasClass (e_elem, s_class))
  236.         return;

  237.     var s_classes = e_elem.className;
  238.     e_elem.className = (s_classes ? s_classes + ' ' : '') + s_class;
  239. }

  240. function f_tcalRemoveClass (e_elem, s_class) {
  241.     var s_classes = e_elem.className;
  242.     if (!s_classes || s_classes.indexOf(s_class) == -1)
  243.         return false;

  244.     var a_classes = s_classes.split(' '),
  245.         a_newClasses = [];

  246.     for (var n = 0; n < a_classes.length; n++) {
  247.         if (a_classes[n] == s_class)
  248.             continue;
  249.         a_newClasses[a_newClasses.length] = a_classes[n];
  250.     }
  251.     e_elem.className = a_newClasses.join(' ');
  252.     return true;
  253. }

  254. function f_getPosition (e_elemRef, s_coord) {
  255.     var n_pos = 0, n_offset,
  256.         e_elem = e_elemRef;

  257.     while (e_elem) {
  258.         n_offset = e_elem["offset" + s_coord];
  259.         n_pos += n_offset;
  260.         e_elem = e_elem.offsetParent;
  261.     }

  262.     e_elem = e_elemRef;
  263.     while (e_elem != document.body) {
  264.         n_offset = e_elem["scroll" + s_coord];
  265.         if (n_offset && e_elem.style.overflow == 'scroll')
  266.             n_pos -= n_offset;
  267.         e_elem = e_elem.parentNode;
  268.     }
  269.     return n_pos;
  270. }

  271. function f_tcalInit () {
  272.     
  273.     if (!document.getElementsByTagName)
  274.         return;

  275.     var e_input, a_inputs = f_tcalGetInputs();
  276.     for (var n = 0; n < a_inputs.length; n++) {
  277.         e_input = a_inputs[n];
  278.         e_input.onclick = f_tcalOnClick;
  279.         f_tcalAddClass(e_input, A_TCALCONF.cssprefix + 'Input');
  280.     }
  281.     
  282.     window.A_TCALTOKENS_IDX = {};
  283.     for (n = 0; n < A_TCALTOKENS.length; n++)
  284.         A_TCALTOKENS_IDX[A_TCALTOKENS[n]['t']] = A_TCALTOKENS[n];
  285. }

  286. function f_tcalAddOnload (f_func) {
  287.     if (document.addEventListener) {
  288.         window.addEventListener('load', f_func, false);
  289.     }
  290.     else if (window.attachEvent) {
  291.         window.attachEvent('onload', f_func);
  292.     }
  293.     else {
  294.         var f_onLoad = window.onload;
  295.         if (typeof window.onload != 'function') {
  296.             window.onload = f_func;
  297.         }
  298.         else {
  299.             window.onload = function() {
  300.                 f_onLoad();
  301.                 f_func();
  302.             }
  303.         }
  304.     }
  305. }

  306. f_tcalAddOnload (f_tcalInit);
 
另外,多款JavaScript 日历控件地址:

阅读(2061) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

cici02082014-10-23 15:44:24

这个还是很好用的,我今天用了,在f_tcalOnClick ()函数里增加了z-index属性,否则挡住了: e_cal.style.zIndex='100000';