lhgcalendar.js
上传用户:liubo1823
上传日期:2022-02-07
资源大小:9k
文件大小:6k
源码类别:

JavaScript

开发平台:

HTML/CSS

  1. /*
  2.  *@Generator -> LiHuiGang - Email:lhg133@126.com - QQ:463214570
  3.  *@Copyright (c) 2009 LiHuiGang Compostion - Blog:http://www.cnblogs.com/lhgstudio
  4.  */
  5. var ie = /*@cc_on!@*/false, co = null, io = '';
  6. var config = { dir : '', skin : 'default' }
  7. var $ = function( id )
  8. {
  9.     return 'string' == typeof(id) ? document.getElementById(id) : id;
  10. }
  11. String.prototype.trim = function()
  12. {
  13. return this.replace( /(^s*)|(s*$)/g, '');
  14. }
  15. function getet()
  16. {
  17.     if(ie) return window.event;
  18. var func = getet.caller;
  19. while( func != null )
  20. {
  21.     var arg = func.arguments[0];
  22. if( arg && (arg + '').indexOf('Event') >= 0 ) return arg;
  23. func = func.caller;
  24. }
  25. return null;
  26. }
  27. function getpos( o )
  28. {
  29.     var l, t;
  30. if( o.getBoundingClientRect )
  31. {
  32.     var el = o.getBoundingClientRect();
  33. var st = Math.max( document.documentElement.scrollTop, document.body.scrollTop );
  34. var sl = Math.max( document.documentElement.scrollLeft, document.body.scrollLeft );
  35. l = sl + el.left; t = st + el.top;
  36. }
  37. else
  38. {
  39.     l = o.offsetLeft; t = o.offsetTop;
  40. while( o = o.offsetParent )
  41. {
  42.     l += o.offsetLeft; t += o.offsetTop;
  43. }
  44. }
  45. return { x:l, y:t };
  46. }
  47. var calendar = function( s )
  48. {
  49.     var d = new Date();
  50. if( s && s.indexOf('-') >= 0 )
  51. {
  52.     var a = s.trim().split('-');
  53. d = new Date( a[0], a[1]-1, a[2] );
  54. }
  55. this.y = d.getFullYear();
  56. this.m = d.getMonth() + 1;
  57. this.d = d.getDate();
  58. }
  59. calendar.prototype =
  60. {
  61.     draw : function()
  62. {
  63. var a = [], h = '', w = ['日','一','二','三','四','五','六'];
  64. var fd = new Date(this.y, this.m - 1, 1).getDay();
  65. var md = new Date(this.y, this.m, 0).getDate();
  66. for( var i = 1; i <= fd; i++ ) a.push(0);
  67. for( var i = 1; i <= md; i++ ) a.push(i);
  68. h += '<div id="containchild"><table cellspacing="0" cellpadding="0" id="calendar">' +
  69.      '<caption><div id="py" onclick="co.py();"><img height="1"/></div><div id="pm" onclick="co.pm();"><img height="1"/></div><span>' + this.y + ' 年 ' + this.m + ' 月</span><div id="nm" onclick="co.nm();"><img height="1"/></div><div id="ny" onclick="co.ny();"><img height="1"/></div></caption>' +
  70.      '<thead><tr>';
  71. for( var i = 0; i < 7; i++ )
  72. h += '<td>' + w[i] + '</td>';
  73. h += '</tr></thead><tbody>';
  74. while( a.length )
  75. {
  76.     h += '<tr>';
  77. for( var i = 1; i <= 7; i++ )
  78. {
  79. if( a.length )
  80. {
  81.     var d = a.shift();
  82. if(d)
  83. {
  84.     h += '<td onmouseover="lhgcalendar_over(this)" onmouseout="lhgcalendar_out(this)"';
  85. if( d == this.d ) h += ' class="tday" '; else h += ' class="tdday" '; 
  86. h += 'onclick="co.setdate(this);">' + d + '</td>';
  87. }else h += '<td>&nbsp;</td>';
  88. }
  89. }
  90. h += '</tr>';
  91. }
  92. h += '</tbody></table></div>';
  93. return h;
  94. },
  95. crte : function()
  96. {
  97.     this.hide();
  98. var contain = document.createElement('div');
  99. var s = contain.style;
  100. s.visibility = 'hidden';
  101. s.position = 'absolute';
  102. s.top = '0px'; s.zIndex = 65530;
  103. contain.innerHTML = this.draw();
  104. contain.id = 'container';
  105. document.body.appendChild( contain );
  106. if(ie)
  107. {
  108. var ifm = document.createElement('iframe');
  109. var s = ifm.style;
  110. ifm.frameBorder = 0;
  111. ifm.height = (contain.clientHeight - 3) + 'px';
  112. s.visibility = 'inherit'; s.zIndex = -1;
  113. s.filter = 'alpha(opacity=0)';
  114. s.position = 'absolute'; s.top = '0px'; s.left = '0px';
  115. s.width = $('container').offsetWidth;
  116. contain.insertAdjacentElement( 'afterBegin', ifm );
  117. }
  118. },
  119. hide : function()
  120. {
  121. if($('container')){ document.body.removeChild($('container')); }
  122. var co = null;
  123. },
  124. py : function()
  125. {
  126.     this.predraw( new Date(this.y - 1, this.m - 1, 1) );
  127. },
  128. ny : function()
  129. {
  130.     this.predraw( new Date(this.y + 1, this.m - 1, 1) );
  131. },
  132. pm : function()
  133. {
  134.     this.predraw( new Date(this.y, this.m - 2, 1) );
  135. },
  136. nm : function()
  137. {
  138.     this.predraw( new Date(this.y, this.m, 1) );
  139. },
  140. predraw : function( d )
  141. {
  142.     this.y = d.getFullYear(); this.m = d.getMonth() + 1;
  143. $('containchild').innerHTML = this.draw();
  144. },
  145. setdate : function( o )
  146. {
  147.     if( $(io) ) $(io).value = this.y + '-' + this.m + '-' + o.innerHTML;
  148. this.hide();
  149. }
  150. };
  151. function lhgcalendar( id )
  152. {
  153. var evt = getet();
  154. var e = evt.srcElement || evt.target;
  155. var d = id ? $(id).value : e.value;
  156. io = id ? id : e.id;
  157. var c = new calendar( d ); co = c; c.crte();
  158. var pos = getpos(e);
  159. var l, t, w = e.offsetWidth, h = e.offsetHeight;
  160. if( config.dir == 'right' )
  161. {
  162.     l = pos.x + w; t = pos.y;
  163. l = ie ? l : l+2; t = ie ? t-2 : t;
  164. }
  165. else
  166. {
  167.     l = pos.x; t = pos.y + h;
  168. l = ie ? l-2 : l; t = ie ? t-1 : t+1;
  169. }
  170. var o = $('container');
  171. o.style.top = t + 'px';
  172. o.style.left = l + 'px';
  173. o.style.visibility = 'visible';
  174. if(ie)
  175. {
  176.     document.attachEvent( 'onclick', c.hide );
  177. o.attachEvent( 'onclick', function(e){e.cancelBubble = true;} );
  178. }
  179. else
  180. {
  181.     document.addEventListener( 'click', c.hide, false );
  182. o.addEventListener( 'click', function(e){e.cancelBubble = true;}, false );
  183. }
  184. evt.cancelBubble = true;
  185. }
  186. function lhgcalendar_over( o )
  187. {
  188.     if( o.className != 'tday' ) o.className = 'over';
  189. }
  190. function lhgcalendar_out( o )
  191. {
  192.     if( o.className != 'tday' ) o.className = 'tdday';
  193. }
  194. (function()
  195. {
  196. var l = document.createElement( 'link' );
  197. l.type = 'text/css';
  198. l.href = 'skin/' + config.skin + '.css';
  199. l.rel = 'stylesheet';
  200. document.getElementsByTagName('head')[0].appendChild(l);
  201. })();