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

简易万年历

程序员文章站 2022-03-07 19:27:06
...
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>日历</title>
  6. </head>
  7. <style>
  8. body{
  9. width: 100%;
  10. box-sizing: border-box;
  11. }
  12. section{
  13. width: 300px;
  14. height: 320px;
  15. margin: 20px auto;
  16. border: 1px solid #ccc;
  17. }
  18. .header{
  19. display: flex;
  20. justify-content: space-between;
  21. padding: 15px;
  22. }
  23. .header>span:first-child,.header>span:last-child{
  24. line-height: 40px;
  25. }
  26. .container{
  27. width: 100%;
  28. border-spacing: 0;
  29. }
  30. ul,li,p{
  31. padding: 0;
  32. margin: 0;
  33. box-sizing: border-box;
  34. text-align: center;
  35. }
  36. ul{
  37. display: flex;
  38. justify-content: space-around;
  39. }
  40. ul:first-child{
  41. background: #d6d6d6;
  42. padding: 5px;
  43. }
  44. li{
  45. text-align: center;
  46. padding: 5px;
  47. list-style-type: none;
  48. width: 42px;
  49. height: 30px;
  50. }
  51. .active{
  52. color: red;
  53. }
  54. .hover:hover{
  55. border: 1px solid red;
  56. cursor: pointer;
  57. }
  58. #date{
  59. display: flex;
  60. justify-content: start;
  61. flex-wrap: wrap;
  62. }
  63. </style>
  64. <body>
  65. <section>
  66. <div class="header">
  67. <span id="pre">上一月</span>
  68. <span>
  69. <p id="year"></p>
  70. <p id="month"></p>
  71. </span>
  72. <span id="next">下一月</span>
  73. </div>
  74. <div class="container">
  75. <ul>
  76. <li></li>
  77. <li></li>
  78. <li></li>
  79. <li></li>
  80. <li></li>
  81. <li></li>
  82. <li></li>
  83. </ul>
  84. <ul id="date">
  85. </ul>
  86. </div>
  87. </section>
  88. <script>
  89. var date = new Date();
  90. add()
  91. function add(){
  92. var arr = ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"]
  93. var year = date.getFullYear(); //获取系统年份
  94. var month = date.getMonth(); //获取系统月份
  95. var now = date.getDate(); //今天的日期
  96. var xingQi = new Date(year,month,1).getDay(); //当月1号是星期几
  97. var days = getDate(year,month-1); //当月天数
  98. console.log(xingQi);
  99. document.getElementById('year').innerText = year+ '年';
  100. document.getElementById('month').innerText = arr[month];
  101. var dateObj = document.getElementById('date');
  102. function getDate(year,month){
  103. var d = new Date(year,month,0);
  104. return d.getDate();
  105. }
  106. var html =''
  107. for (let i =0;i<xingQi;i++){
  108. html += '<li></li>'
  109. }
  110. for (let i=1;i<=days;i++){
  111. if (i == now){
  112. html += "<li class='active'>" + i + "</li>"
  113. }
  114. else {
  115. html += '<li class="hover">' + i + '</li>';
  116. }
  117. }
  118. document.getElementById('date').innerHTML=html;
  119. }
  120. document.getElementById('pre').onclick=function(){
  121. date.setMonth(date.getMonth()-1);
  122. add();
  123. };
  124. document.getElementById('next').onclick=function(){
  125. date.setMonth(date.getMonth()+1);
  126. add();
  127. }
  128. </script>
  129. </body>
  130. </html>