[Java] js日历选择器特效 datepicker →→→→→进入此内容的聊天室

来自 , 2020-10-17, 写在 Java, 查看 214 次.
URL http://www.code666.cn/view/8065d07d
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>DatePicker</title>
  6. <meta name="author" content="hongru.chen" />
  7. <style>
  8. .date-picker-wp {
  9. display: none;
  10. position: absolute;
  11. background: #f1f1f1;
  12. left: 40px;
  13. top: 40px;
  14. border-top: 4px solid #3879d9;
  15. }
  16. .date-picker-wp table {
  17. border: 1px solid #ddd;
  18. }
  19. .date-picker-wp td {
  20. background: #fafafa;
  21. width: 22px;
  22. height: 18px;
  23. border: 1px solid #ccc;
  24. font-size: 12px;
  25. text-align: center;
  26. }
  27. .date-picker-wp td.noborder {
  28. border: none;
  29. background: none;
  30. }
  31. .date-picker-wp td a {
  32. color: #1c93c4;
  33. text-decoration: none;
  34. }
  35. .strong {font-weight: bold}
  36. .hand {cursor: pointer; color: #3879d9}
  37. </style>
  38.   <script type="text/javascript">
  39. var DatePicker = function () {
  40. var $ = function (i) {return document.getElementById(i)},
  41. addEvent = function (o, e, f) {o.addEventListener ? o.addEventListener(e, f, false) : o.attachEvent('on'+e, function(){f.call(o)})},
  42. getPos = function (el) {
  43. for (var pos = {x:0, y:0}; el; el = el.offsetParent) {
  44. pos.x += el.offsetLeft;
  45. pos.y += el.offsetTop;
  46. }
  47. return pos;
  48. }
  49. var init = function (n, config) {
  50. window[n] = this;
  51. Date.prototype._fd = function () {var d = new Date(this); d.setDate(1); return d.getDay()};
  52. Date.prototype._fc = function () {var d1 = new Date(this), d2 = new Date(this); d1.setDate(1); d2.setDate(1); d2.setMonth(d2.getMonth()+1); return (d2-d1)/86400000;};
  53. this.n = n;
  54. this.config = config;
  55. this.D = new Date;
  56. this.el = $(config.inputId);
  57. this.el.title = this.n+'DatePicker';
  58. this.update();
  59. this.bind();
  60. }
  61. init.prototype = {
  62. update : function (y, m) {
  63. var con = [], week = ['Su','Mo','Tu','We','Th','Fr','Sa'], D = this.D, _this = this;
  64. fn = function (a, b) {return '<td title="'+_this.n+'DatePicker" class="noborder hand" onclick="'+_this.n+'.update('+a+')">'+b+'</td>'},
  65. _html = '<table cellpadding=0 cellspacing=2>';
  66. y && D.setYear(D.getFullYear() + y);
  67. m && D.setMonth(D.getMonth() + m);
  68. var year = D.getFullYear(), month = D.getMonth() + 1, date = D.getDate();
  69. for (var i=0; i<week.length; i++) con.push('<td title="'+this.n+'DatePicker" class="noborder">'+week[i]+'</td>');
  70. for (var i=0; i<D._fd(); i++ ) con.push('<td title="'+this.n+'DatePicker" class="noborder"> </td>');
  71. for (var i=0; i<D._fc(); i++ ) con.push('<td class="hand" onclick="'+this.n+'.fillInput('+year+', '+month+', '+(i+1)+')">'+(i+1)+'</td>');
  72. var toend = con.length%7;
  73. if (toend != 0) for (var i=0; i<7-toend; i++) con.push('<td class="noborder"> </td>');
  74. _html += '<tr>'+fn("-1, null", "<<")+fn("null, -1", "<")+'<td title="'+this.n+'DatePicker" colspan=3 class="strong">'+year+'/'+month+'/'+date+'</td>'+fn("null, 1", ">")+fn("1, null", ">>")+'</tr>';
  75. for (var i=0; i<con.length; i++) _html += (i==0 ? '<tr>' : i%7==0 ? '</tr><tr>' : '') + con[i] + (i == con.length-1 ? '</tr>' : '');
  76. !!this.box ? this.box.innerHTML = _html : this.createBox(_html);
  77. },
  78. fillInput : function (y, m, d) {
  79. var s = this.config.seprator || '/';
  80. this.el.value = y + s + m + s + d;
  81. this.box.style.display = 'none';
  82. },
  83. show : function () {
  84. var s = this.box.style, is = this.mask.style;
  85. s['left'] = is['left'] = getPos(this.el).x + 'px';
  86. s['top'] = is['top'] = getPos(this.el).y + this.el.offsetHeight + 'px';
  87. s['display'] = is['display'] = 'block';
  88. is['width'] = this.box.offsetWidth - 2 + 'px';
  89. is['height'] = this.box.offsetHeight - 2 + 'px';
  90. },
  91. hide : function () {
  92. this.box.style.display = 'none';
  93. this.mask.style.display = 'none';
  94. },
  95. bind : function () {
  96. var _this = this;
  97. addEvent(document, 'click', function (e) {
  98. e = e || window.event;
  99. var t = e.target || e.srcElement;
  100. if (t.title != _this.n+'DatePicker') {_this.hide()} else {_this.show()}
  101. })
  102. },
  103. createBox : function (html) {
  104. var box = this.box = document.createElement('div'), mask = this.mask = document.createElement('iframe');
  105. box.className = this.config.className || 'datepicker';
  106. mask.src = 'javascript:false';
  107. mask.frameBorder = 0;
  108. box.style.cssText = 'position:absolute;display:none;z-index:9999';
  109. mask.style.cssText = 'position:absolute;display:none;z-index:9998';
  110. box.title = this.n+'DatePicker';
  111. box.innerHTML = html;
  112. document.body.appendChild(box);
  113. document.body.appendChild(mask);
  114. return box;
  115. }
  116. }
  117. return init;
  118. }();
  119. onload = function () {
  120. new DatePicker('_DatePicker_demo', {
  121. inputId: 'date-input',
  122. className: 'date-picker-wp',
  123. seprator: '-'
  124. });
  125. new DatePicker('_demo2', {inputId: 'demo2', className: 'date-picker-wp'})
  126. }
  127. </script></head>
  128. <body>
  129. <input type="text" id="date-input" />
  130. <input type="text" id="demo2" /><br/>
  131. <select><option>岑安</option></select>
  132. </body>
  133. </html>

回复 "js日历选择器特效 datepicker"

这儿你可以回复上面这条便签

captcha