博客列表 >JS,CSS HTML 简单开心日历

JS,CSS HTML 简单开心日历

中天行者
中天行者原创
2021年11月23日 02:22:00475浏览

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <style>
  8. html,body,table,tr,th,td{
  9. margin: 0;
  10. padding: 0;
  11. }
  12. html,body{
  13. width: 100%;
  14. height: 100%;
  15. }
  16. body{
  17. position: relative;
  18. }
  19. .table{
  20. width: 500px;
  21. height: 500px;
  22. position: absolute;
  23. top:calc(50% - 250px);
  24. left:calc(50% - 250px);
  25. box-shadow: -1px 20px 20px 5px #dbdbdb;
  26. }
  27. .title,.yearMonth{
  28. display: flex;
  29. justify-content: space-between;
  30. align-items: center;
  31. }
  32. .yearMonth{
  33. flex-direction: column;
  34. }
  35. .body,table{
  36. width: 100%;
  37. border-spacing:0;
  38. }
  39. .body>table .t_title{
  40. background-color: #dbdbdb;
  41. }
  42. th{
  43. padding: 10px;
  44. border-bottom: #888888 1px solid;
  45. border-right: #888888 1px solid;
  46. border-radius: 5px ;
  47. }
  48. td{
  49. text-align: center;
  50. cursor: pointer;
  51. padding: 15px;
  52. border-bottom: grey 1px solid;
  53. border-right: grey 1px solid;
  54. border-radius: 0 0 5px 5px;
  55. transition: all 1s;
  56. font-size: 20px;
  57. }
  58. td:hover{
  59. background-color: darkred;
  60. color: white;
  61. box-shadow: 10px 10px 10px #888888;
  62. }
  63. .proMonth,.nextMonth{
  64. transition: all 1s;
  65. cursor: pointer;
  66. padding: 10px;
  67. margin: 5px;
  68. font-weight: bold;
  69. }
  70. .proMonth:hover{
  71. background-color: darkred;
  72. color: white;
  73. border-radius: 5px;
  74. }
  75. .nextMonth:hover{
  76. background-color: darkred;
  77. color: white;
  78. border-radius: 5px;
  79. }
  80. .footer{
  81. display: flex;
  82. justify-content: end;
  83. flex-direction: column;
  84. align-items: center;
  85. font-size: 30px;
  86. font-weight: bold;
  87. overflow: hidden;
  88. padding: 10px;
  89. }
  90. i{
  91. background: none;
  92. transition: all 0.3s ease;
  93. cursor: pointer;
  94. font-style:normal
  95. }
  96. i:hover{
  97. font-size: 40px;
  98. color: brown;
  99. text-shadow: -10px 5px 10px #888888;
  100. }
  101. i::after{
  102. display: block;
  103. /*内容为空*/
  104. content: "";
  105. /*线高度*/
  106. height: 2px;
  107. background: brown ;
  108. transition: all 0.5s ease;
  109. /*水平划线0*/
  110. transform: scaleX(0);
  111. box-shadow: -10px 5px 10px #888888;
  112. }
  113. i:hover::after{
  114. transform: scaleX(1.5);
  115. }
  116. .nowDay{
  117. color: red;
  118. background-color: antiquewhite;
  119. }
  120. </style>
  121. <body>
  122. <div class="table">
  123. <div class="title">
  124. <div class="proMonth" id="proMonth">上一月</div>
  125. <div class="yearMonth" id="yearMonth">
  126. <div class="year" id="year">2021</div>
  127. <div class="month" id="month">12月</div>
  128. </div>
  129. <div class="nextMonth" id="nextMonth">下一月</div>
  130. </div>
  131. <div class="body">
  132. <table>
  133. <tr class="t_title" id="t_title">
  134. </tr>
  135. <tbody id="t_body">
  136. </tbody>
  137. </table>
  138. </div>
  139. <div class="footer">
  140. <i>
  141. 开心日历
  142. </i>
  143. </div>
  144. </div>
  145. <script>
  146. var date =new Date();
  147. var nowY = year =date.getFullYear();//年
  148. var nowM = month =date.getMonth();//月
  149. var day =date.getDate();//日
  150. var t_title= document.getElementById("t_title");
  151. var proMonth= document.getElementById("proMonth");
  152. var nextMonth= document.getElementById("nextMonth");
  153. var monthElement= document.getElementById("month");
  154. var yearElement= document.getElementById("year");
  155. var changeNum = ['一', '二', '三', '四', '五', '六', '七', '八', '九','十','十一','十二'];
  156. monthElement.innerText=changeNum[month]+'月';
  157. yearElement.innerText=year+'年';
  158. var t_body= document.getElementById("t_body");
  159. var week=['星期日','星期一','星期二','星期三','星期四','星期五','星期六'];
  160. var str=""
  161. week.forEach(function (v) {
  162. str +="<th>"+v+"</th>";
  163. });
  164. month +=1;
  165. nowM +=1;
  166. //追加星期抬头
  167. t_title.innerHTML=str;
  168. var weekDay =week[date.getDay()];
  169. var string = "<tr>";
  170. var lastDay= new Date(year, month, 0).getDate();//最后一天--总计天数
  171. var firstDayOfWeek =new Date(month+"/1/"+year).getDay(); //每月第一天周几
  172. myInnerHtml();
  173. // 上一月
  174. proMonth.onclick=function () {
  175. string = "<tr>";
  176. if(month-1===0){
  177. year -=1;
  178. month =12;
  179. yearElement.innerText=year+'年';
  180. }else{
  181. month -=1;
  182. }
  183. myInnerHtml()
  184. }
  185. // 下一月
  186. nextMonth.onclick=function () {
  187. string = "<tr>";
  188. if(month+1===13){
  189. year +=1;
  190. month =1;
  191. yearElement.innerText=year+'年';
  192. }else{
  193. month +=1;
  194. }
  195. myInnerHtml()
  196. }
  197. // 填充tbody的天数
  198. function myInnerHtml() {
  199. monthElement.innerText=changeNum[month-1]+'月';
  200. lastDay= new Date(year, month, 0).getDate();////最后一天--总计天数
  201. firstDayOfWeek =new Date(month+"/1/"+year).getDay();//每月第一天周几
  202. for(var i =1;i<=firstDayOfWeek;i++){
  203. string += "<td></td>"
  204. }
  205. for(var i =1;i<=lastDay;i++){
  206. var std="<td>";
  207. var etd="</td>";
  208. if(i===day && nowY === year && nowM === month ){
  209. std="<td class='nowDay'>"
  210. }
  211. if((i+firstDayOfWeek)%7==0){
  212. string += std+i+ etd +"</tr><tr>";
  213. }else {
  214. string += std+i+ etd ;
  215. }
  216. }
  217. t_body.innerHTML=string
  218. }
  219. </script>
  220. </body>
  221. </html>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议