純js模仿windows系統(tǒng)日歷-創(chuàng)新互聯(lián)

在網(wǎng)上看了幾篇關(guān)于生成日歷的js 教程于是自己也整理了一個想法思路 大家有什么建議歡迎提出

創(chuàng)新互聯(lián)自2013年創(chuàng)立以來,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目成都做網(wǎng)站、網(wǎng)站制作、成都外貿(mào)網(wǎng)站建設(shè)網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元裕華做網(wǎng)站,已為上家服務(wù),為裕華各地企業(yè)和個人服務(wù),聯(lián)系電話:18980820575

首先這個項目里面本人認為的幾個難點:

1、如何定義每一個月的第一天位置

每個月的第一天都不是固定的星期幾,所以第一天的輸出需要動動腦筋把它放到對應(yīng)的星期里面

2、每個月的最后一天有時候因為行數(shù)不夠輸出不了怎么辦?

下面會有答案 ^_^

思路:

1、定義好每一個月份的日期天數(shù)

2、獲取當前的系統(tǒng)日期初始化數(shù)據(jù)

3、輸出日歷

    2.1、先獲取當前月的第一天是星期幾(這一點與日歷的排版至關(guān)重要?。?br />    2.2、獲取當前月的天數(shù)
    2.3、獲取當前月有多少個星期(即要輸出多少行 行數(shù)這里我會預(yù)留多一行)
    2.4、獲取當前年份和月份 用作顯示

下面便是完整的代碼:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>js 日歷</title>
 <style type="text/css">
  *{
  border: 0;
  padding: 0;
  margin: 0;
  font-family: "微軟雅黑";
  }
  a{
  text-decoration: none;
  color: #000;
  }
  li{
  list-style-type: none;
  }
  .calendar_wrap{
  width: 350px;
  margin: 0 auto;
  padding: 0;
  border: 1px solid #000;
  }
  .calendar_list{
  width: 100%;
  margin-top: 10px;
  }
  .calendar_list tr{
  width: 100%;
  }
  .calendar_list tr td{
  text-align: center;
  height: 45px;
  }
  .control_bar{
  word-spacing: -6px;
  }
  .control_bar span,.control_bar b{
  display: inline-block;
  text-align: center;
  word-spacing: 0px;
  }
  .left-bt,.right-bt{
  width: 50px;
  }
  #reduce_bt,#add_bt{
  width: 50%;
  height: 25px;
  border-radius: 50%;
  }
  #reduce_bt:focus{
  outline: none;
  }
  #add_bt:focus{
  outline: none;
  }
  #current_date{
  width: 250px;
  }
  #resetBt{
  display: block;
  text-align: center;
  color: #fff;
  cursor: pointer;
  width: 120px;
  line-height: 40px;
  background-color: #FF7F27;
  margin: 0 auto;
  }
  #date_list tr td:hover{
  background-color: #ccc;
  cursor: default;
  }
 </style>
</head>
<body>
 <div class="calendar_wrap">
 <div class="control_bar">
  <span class="left-bt"><input type="button" id="reduce_bt" value="<"></span>
  <b id="current_date">2017-02</b>
  <span class="right-bt"><input type="button" id="add_bt" value=">"></span>
 </div>
 <table class="calendar_list" cellspacing="0">
  <thead>
   <tr>
   <td>日</td>
   <td>一</td>
   <td>二</td>
   <td>三</td>
   <td>四</td>
   <td>五</td>
   <td>六</td>
   </tr> 
  </thead>
  <tbody id="date_list"></tbody> 
 </table>
 </div>
 <span id="resetBt">回到現(xiàn)在日期</span>
 <script type="text/javascript">
  var dateScreen = document.getElementById('current_date');//獲取顯示當前年份月份的div
  var reduceBt = document.getElementById('reduce_bt');//獲取減少月份的按鈕
  var addBt = document.getElementById('add_bt');//獲取增加月份的按鈕
  var dateList = document.getElementById('date_list');//獲取顯示所有日期部分
  var resetBt = document.getElementById('resetBt');//獲取重設(shè)按鈕
  //定義好每月的日期總數(shù) 總數(shù)按js 獲取月份數(shù)值的下標方式儲存
  var overall_date = [31,28,31,30,31,30,31,31,30,31,30,31];
  var add_date = 1;//定義添加日期數(shù)的初始化
  //初始化日歷
  //獲取現(xiàn)在的日期
  var now_date = new Date();
  var nowFullYear = now_date.getFullYear();
  var nowMonth = now_date.getMonth();
  //執(zhí)行日歷輸出函數(shù)
  printCalendar();
  //-----------------------------------
  //月份減少按鈕點擊事件
  reduceBt.onclick = function(){
  nowMonth = nowMonth - 1;
  if (nowMonth == -1) {
  nowFullYear = nowFullYear - 1;
  nowMonth = 11;
  }
  clearRows();
  printCalendar();
  }
  //增加月份按鈕點擊事件
  addBt.onclick = function(){
  nowMonth+= 1;
  if (nowMonth == 12) {
  nowFullYear+= 1;
  nowMonth = 0;
  } 
  clearRows();
  printCalendar();
  }
  //重設(shè)按鈕點擊事件
  resetBt.onclick = function(){
  var resetDate = new Date();
  nowFullYear = resetDate.getFullYear();
  nowMonth = resetDate.getMonth();
  clearRows();
  printCalendar();
  }
  function printCalendar(){
  var printDate = new cur_date(nowFullYear,nowMonth);//實例cur_date方法
  var printFirstDay = printDate.firstDay;//獲取要輸出月份第一天的星期
  var printTotalDate = printDate.totalDate;//獲取輸出日期的總數(shù)
  var printMonth = printDate.cur_month;//獲取輸出的月份
  (printMonth >= 9)?(printMonth = (printMonth + 1)):(printMonth = ("0" + (printMonth + 1)));
  //調(diào)整月份的顯示格式
  var printYear = printDate.cur_year;//獲取輸出的年份
  var totalRows = Math.ceil((printTotalDate + (printFirstDay - 1)) / 7) + 1;
  //獲取行數(shù)
  //利用天數(shù)除以7天獲得行數(shù)并將它向上去整 但是上限是5
  //而考慮到某些月會有6行所以在總行數(shù)里面加1 以防萬一
  //開始輸出
  //首先顯示出年和月
  dateScreen.innerText = printYear + "-" + printMonth;
   //開始輸出日期
   for (var i = 0; i < totalRows; i++) {
   dateList.insertRow(i);
   for (var j = 0; j < 7; j++) {
   //當天數(shù)總量大于額定總量時先終止內(nèi)部循環(huán)
   if (add_date > printTotalDate) {
   break;
   }
   dateList.rows[i].insertCell(j);
   //改變周日和周六的文字顏色
   if (j == 0) {
   dateList.rows[i].cells[j].style.color = "red";
   dateList.rows[i].cells[j].style.fontWeight = "bold";
   }else if(j == 6){
   dateList.rows[i].cells[j].style.color = "green";
   dateList.rows[i].cells[j].style.fontWeight = "bold";
   }
   if (i == 0 && j >= printFirstDay) {
   //當此時是第一行時而且單元格下標大于等于當前月第一天的星期就開始為單元格填入文本
   dateList.rows[i].cells[j].innerText = add_date;
   add_date++;
   }else if(i > 0){
   //第一行以后的單元格就按循環(huán)添加即可
   dateList.rows[i].cells[j].innerText = add_date;
   add_date++;
   }
   }
   }
  add_date = 1;//輸出完把日期總數(shù)重新賦值為1
  }
  //獲取當前年、月、第一天是星期幾、日期總數(shù)
  function cur_date(curYear,curMonth){
  this.cur_firstDate = new Date(curYear,curMonth,1);//獲取現(xiàn)在日期的第一天
  this.cur_year = curYear;//獲取當前的年
  this.cur_month = curMonth;//獲取當前的月
  this.totalDate = is_leapYear(curYear,curMonth);//獲取總天數(shù)
  this.firstDay = this.cur_firstDate.getDay()//獲取每個月的第一天是星期幾
  }
  //判斷今年是否為閏年
  function is_leapYear(target_year,target_month){
  if ((target_month == 1) && (target_year % 4 == 0) && ((target_year % 100 != 0) || (target_year % 400 != 0))) {
     //當前月是2月且當前年是閏年
     return 29;
  }else{
  //其他月按正常日期總數(shù)輸出
  return overall_date[target_month];
  }
  }
  function clearRows(){
  var rowsNum = dateList.rows.length;
  while(rowsNum > 0){
   dateList.deleteRow(rowsNum - 1);
   rowsNum--;
  }
  }
 </script>
</body>
</html>

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站muchs.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

新聞標題:純js模仿windows系統(tǒng)日歷-創(chuàng)新互聯(lián)
當前地址:http://muchs.cn/article16/hesgg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)響應(yīng)式網(wǎng)站、電子商務(wù)品牌網(wǎng)站設(shè)計、手機網(wǎng)站建設(shè)、網(wǎng)站排名

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化