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

js做的一个日历

程序员文章站 2022-03-03 22:44:49
...

js做的一个日历

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>日历</title>
  6. <style>
  7. section {
  8. margin: 60px auto;
  9. width: 300px;
  10. height: 300px;
  11. border: 1px solid black;
  12. /*padding: 10px;*/
  13. font-size: 16px;
  14. }
  15. .padding-1{
  16. padding: 3px;
  17. }
  18. .padding-2{
  19. padding: 10px;
  20. }
  21. .d-flex {
  22. display: flex;
  23. }
  24. .just-betww {
  25. justify-content: space-between;
  26. }
  27. .just-round {
  28. justify-content: space-around;
  29. }
  30. .align-items-centen {
  31. align-items: center;
  32. }
  33. .text-center {
  34. text-align: center;
  35. }
  36. .background-ccc {
  37. background: #cccccc;
  38. }
  39. .border-1{
  40. border: 1px solid black;
  41. }
  42. .curos {
  43. cursor:pointer ;
  44. }
  45. .mix{
  46. width: 26px;
  47. height: 26px;
  48. margin: 5px 4px 4px 7px;
  49. border: 1px solid rgba(255,0,0,0);
  50. }
  51. .mix:hover{
  52. border: 1px solid red;
  53. }
  54. .flex-wrap {
  55. flex-wrap: wrap;
  56. }
  57. .visiti-hid{
  58. visibility: hidden;
  59. }
  60. .visiti-vi{
  61. visibility: visible;
  62. }
  63. .dis-none {
  64. display: none;
  65. }
  66. .day-bg{
  67. background: peru;
  68. }
  69. </style>
  70. </head>
  71. <body>
  72. <section>
  73. <div class="d-flex just-betww padding-1">
  74. <div class="curos" onclick=aboveMoth()>上一月</div>
  75. <div><span id="myYear">2021</span></div>
  76. <div class="curos" onclick=nextMoth()>下一月</div>
  77. </div>
  78. <div class="text-center"><span id="myMoth">九月</span></div>
  79. <div class="background-ccc text-center d-flex just-round">
  80. <span></span>
  81. <span></span>
  82. <span></span>
  83. <span></span>
  84. <span></span>
  85. <span></span>
  86. <span></span>
  87. </div>
  88. <div class="padding-1 d-flex flex-wrap just-round text-center">
  89. <div class="mix visiti-hid curos" id="0"><span></span></div>
  90. <div class="mix visiti-hid curos" id="1"><span></span></div>
  91. <div class="mix visiti-hid curos" id="2"><span></span></div>
  92. <div class="mix visiti-hid curos" id="3"><span></span></div>
  93. <div class="mix visiti-hid curos" id="4"><span></span></div>
  94. <div class="mix visiti-hid curos" id="5"><span></span></div>
  95. <div class="mix visiti-hid curos" id="6"><span></span></div>
  96. <div class="mix visiti-hid curos" id="7"><span></span></div>
  97. <div class="mix visiti-hid curos" id="8"><span></span></div>
  98. <div class="mix visiti-hid curos" id="9"><span></span></div>
  99. <div class="mix visiti-hid curos" id="10"><span></span></div>
  100. <div class="mix visiti-hid curos" id="11"><span></span></div>
  101. <div class="mix visiti-hid curos" id="12"><span></span></div>
  102. <div class="mix visiti-hid curos" id="13"><span></span></div>
  103. <div class="mix visiti-hid curos" id="14"><span></span></div>
  104. <div class="mix visiti-hid curos" id="15"><span></span></div>
  105. <div class="mix visiti-hid curos" id="16"><span></span></div>
  106. <div class="mix visiti-hid curos" id="17"><span></span></div>
  107. <div class="mix visiti-hid curos" id="18"><span></span></div>
  108. <div class="mix visiti-hid curos" id="19"><span></span></div>
  109. <div class="mix visiti-hid curos" id="20"><span></span></div>
  110. <div class="mix visiti-hid curos" id="21"><span></span></div>
  111. <div class="mix visiti-hid curos" id="22"><span></span></div>
  112. <div class="mix visiti-hid curos" id="23"><span></span></div>
  113. <div class="mix visiti-hid curos" id="24"><span></span></div>
  114. <div class="mix visiti-hid curos" id="25"><span></span></div>
  115. <div class="mix visiti-hid curos" id="26"><span></span></div>
  116. <div class="mix visiti-hid curos" id="27"><span></span></div>
  117. <div class="mix visiti-hid curos" id="28"><span></span></div>
  118. <div class="mix visiti-hid curos" id="29"><span></span></div>
  119. <div class="mix visiti-hid curos" id="30"><span></span></div>
  120. <div class="mix visiti-hid curos" id="31"><span></span></div>
  121. <div class="mix visiti-hid curos" id="32"><span></span></div>
  122. <div class="mix visiti-hid curos" id="33"><span></span></div>
  123. <div class="mix visiti-hid curos" id="34"><span></span></div>
  124. <div class="mix visiti-hid curos" id="35"><span></span></div>
  125. <div class="mix visiti-hid curos" id="36"><span></span></div>
  126. <div class="mix visiti-hid curos" id="37"><span></span></div>
  127. <div class="mix visiti-hid curos" id="38"><span></span></div>
  128. <div class="mix visiti-hid curos" id="39"><span></span></div>
  129. <div class="mix visiti-hid curos" id="40"><span></span></div>
  130. <div class="mix visiti-hid curos" id="41"><span></span></div>
  131. </div>
  132. </section>
  133. <script>
  134. var myMoth=['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月'];
  135. var myDate = new Date();
  136. var year =myDate.getFullYear();
  137. var moth = myDate.getMonth();
  138. var myDay = myDate.getDate();
  139. // console.log(myDay);
  140. // console.log(moth);
  141. giveFirst();
  142. function giveFirst(){
  143. var firstDate = new Date();
  144. var year =firstDate.getFullYear();
  145. var moth = firstDate.getMonth();
  146. // console.log(firstDate);
  147. document.getElementById('myYear').innerHTML=year;
  148. document.getElementById('myMoth').innerHTML=myMoth[moth];
  149. firstDate.setDate(1);
  150. var firstWeek=firstDate.getDay();
  151. firstDate.setMonth(firstDate.getMonth()+1);
  152. var last=new Date(firstDate-3600000*24);
  153. var lastDay=last.getDate();
  154. var weekDay=firstWeek;
  155. var myD= document.getElementById(''+(myDay+firstWeek-1));
  156. myD.style.background="peru";
  157. for (var i=1;i<=lastDay;i++){
  158. document.getElementById(''+weekDay).firstElementChild.innerHTML=i;
  159. var myEle= document.getElementById(''+weekDay);
  160. myEle.style.visibility="visible";
  161. weekDay++;
  162. }
  163. }
  164. function table(year,moth){
  165. var dateTemp= new Date(year,moth,0);
  166. var lastDay = dateTemp.getDate();
  167. // dateTemp.setDate(myDay);
  168. // myDay= dateTemp.getDate();
  169. // console.log(myDay);
  170. var myD= document.getElementById(''+myDay);
  171. myD.style.background="peru";
  172. dateTemp.setDate(1);
  173. var firstWeek=dateTemp.getDay();
  174. // var flag=0;
  175. div1= document.getElementsByClassName('visiti-hid');
  176. for (var j=0;j<42;j++)
  177. {
  178. div1[j].style.visibility="hidden";
  179. div1[j].style.background = "none";
  180. // flag=0;
  181. }
  182. var weekDay=firstWeek;
  183. var myD= document.getElementById(''+(myDay+firstWeek-1));
  184. myD.style.background="peru";
  185. for (var i=1;i<=lastDay;i++){
  186. document.getElementById(''+weekDay).firstElementChild.innerHTML=i;
  187. var myEle= document.getElementById(''+weekDay);
  188. myEle.style.visibility="visible";
  189. weekDay++;
  190. // console.log(lastDay);
  191. }
  192. }
  193. function aboveMoth()
  194. {
  195. myDate.setMonth(myDate.getMonth()-1);
  196. moth=myDate.getMonth();
  197. year =myDate.getFullYear();
  198. document.getElementById('myMoth').innerHTML=myMoth[moth];
  199. document.getElementById('myYear').innerHTML=year;
  200. mothTemp=moth+1;
  201. table(year,mothTemp);
  202. // console.log('上一月');
  203. // console.log(moth);
  204. // console.log(year);
  205. }
  206. function nextMoth()
  207. {
  208. myDate.setMonth(myDate.getMonth()+1);
  209. moth=myDate.getMonth();
  210. year =myDate.getFullYear();
  211. document.getElementById('myMoth').innerHTML=myMoth[moth];
  212. document.getElementById('myYear').innerHTML=year;
  213. mothTemp=moth+1;
  214. table(year,mothTemp);
  215. // console.log(moth);
  216. // console.log('下一月')
  217. }
  218. // document.getElementById(''+firstWeek).innerHTML = '1';
  219. </script>
  220. </body>
  221. </html>