html+css+js 实现日历控件

talk is cheap show me the code

html+css

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <style>
    *{
      padding: 0%;
      margin: 0%;
    }

    html, body{
      background:#f2f2f2;
    }

    .calendar{
      margin: 45px;
      width: 450px;
      height: 350px;
      background: white;
      box-shadow: 0px 1px 1px rgba(0,0,0,.1);
    }

    .title{
      height: 70px;
      border-bottom: 1px solid rgba(0,0,0,.1);
      position: relative;
      text-align: center;
    }

    #calendar-title{
      font-size: 25px;
      text-transform: uppercase;
      font-family: Arial, Helvetica, sans-serif;
      padding: 14px 0 0 0;
    }

    #calendar-year{
      font-size: 15px;
      font-family: Arial, Helvetica, sans-serif;
      font-weight: normal;
    }

    #pre{
      position: absolute;
      top: 0px;
      left: 0px;

      /*没规定大小时,图片显示 0X0*/
      width: 60px;
      height: 70px;

    }

    #next{
      position: absolute;
      top: 0px;
      right: 0px;
      width: 60px;
      height: 70px;
    }

    .body-list ul{
      font-size: 14px;
      font-family: Arial, Helvetica, sans-serif;
      font-weight: bold;
      width: 100%;
      box-sizing: border-box;

    }

    .body-list ul li{
      list-style: none;
      /*
      display:inline-block;
      width: 13.3%;
      */

      /*100/7 = 14.28%*/
      display: block;
      width: 14.28%;
      float: left;

      /*规定行高,垂直居中*/
      height: 36px;
      line-height: 36px;
      box-sizing: border-box;
      text-align: center;

    }


    .green{
      color:#6ac13c;
    }

    .lightgrey{ /*浅灰色显示过去的日期*/
      color:#a8a8a8;
    }
    .darkgrey{ /*深灰色显示将来的日期*/
      color:#565656;
    }

    /*日期当天用绿色背景绿色文字加以显示*/
    .greenbox{
      border: 1px solid #6ac13c;
      background: #e9f8df;
    }
    .icon {
      width: 1em; height: 2em;
      vertical-align: -0.15em;
      fill: currentColor;
      overflow: hidden;
    }

    svg{
      font-size: 20px !important;
      color: #808080;
    }
  </style>
</head>
<body>
<div class="calendar">
  <div class="title">
    <h1 class="green" id="calendar-title">Month</h1>
    <h2 class="green" id="calendar-year">Year</h2>
    <a href="" id="pre">
      <svg class="icon" aria-hidden="true">
        <use xlink:href="#icon-zuoyou"></use>
      </svg>
    </a>
    <a href="" id="next">
      <svg class="icon" aria-hidden="true">
        <use xlink:href="#icon-zuoyou1"></use>
      </svg>
    </a>
  </div>

  <div class="body">
    <div class="lightgrey body-list">
      <ul>
        <li>SUN</li>
        <li>MON</li>
        <li>TUE</li>
        <li>WED</li>
        <li>THU</li>
        <li>FRI</li>
        <li>SAT</li>

      </ul>

    </div>

    <div class="darkgrey body-list">
      <ul id="days">

      </ul>
    </div>
  </div>
</div>
<script src="js/calendar.js"></script>
<script src="js/iconfont.js"></script>
</body>
</html>

js

const month_olypic = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];//闰年每个月份的天数
const month_normal = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
const month_name = ["January", "Febrary", "March", "April", "May", "June", "July", "Auguest", "September", "October", "November", "December"];
//获取以上各个部分的id
const holder = document.getElementById("days");
const prev = document.getElementById("prev");
const next = document.getElementById("next");
const ctitle = document.getElementById("calendar-title");
const cyear = document.getElementById("calendar-year");
//获取当天的年月日
const my_date = new Date();
let my_year = my_date.getFullYear();//获取年份
let my_month = my_date.getMonth(); //获取月份,一月份的下标为0
const my_day = my_date.getDate();//获取当前日期

//根据年月获取当月第一天是周几
function dayStart(month,year){
  const tmpDate = new Date(year, month, 1);
  return (tmpDate.getDay());
}
//根据年份判断某月有多少天(11,2018),表示2018年12月
function daysMonth(month, year){
  const tmp1 = year % 4;
  const tmp2 = year % 100;
  const tmp3 = year % 400;

  if((tmp1 == 0 && tmp2 != 0) || (tmp3 == 0)){
    return (month_olypic[month]);//闰年
  }else{
    return (month_normal[month]);//非闰年
  }
}
//js实现str插入li+class,不要忘了用innerhtml进行插入
function refreshDate(){
  let i;
  let str = "";
  //计算当月的天数和每月第一天都是周几,day_month和day_year都从上面获得
  const totalDay = daysMonth(my_month, my_year);
  const firstDay = dayStart(my_month, my_year);
  //添加每个月的空白部分
  for(let i = 0; i < firstDay; i++){
    str += "<li>"+"</li>";
  }

  //从一号开始添加知道totalDay,并为pre,next和当天添加样式
  let myclass;
  for(let i = 1; i <= totalDay; i++){
    //三种情况年份小,年分相等月份小,年月相等,天数小
    //点击pre和next之后,my_month和my_year会发生变化,将其与现在的直接获取的再进行比较
    //i与my_day进行比较,pre和next变化时,my_day是不变的
    console.log(my_year+" "+my_month+" "+my_day);
    console.log(my_date.getFullYear()+" "+my_date.getMonth()+" "+my_date.getDay());
    if((my_year < my_date.getFullYear())||(my_year == my_date.getFullYear() && my_month < my_date.getMonth()) || (my_year == my_date.getFullYear() && my_month == my_date.getMonth() && i < my_day)){
      myclass = " class='lightgrey'";
    }else if(my_year == my_date.getFullYear() && my_month == my_date.getMonth() && i == my_day){
      myclass = "class = 'green greenbox'";
    }else{
      myclass = "class = 'darkgrey'";
    }
    str += "<li "+myclass+">"+i+"</li>";
  }
  holder.innerHTML = str;
  ctitle.innerHTML = month_name[my_month];
  cyear.innerHTML = my_year;
}
//调用refreshDate()函数,日历才会出现
refreshDate();
//实现onclick向前或向后移动
pre.onclick = function(e){
  e.preventDefault();
  my_month--;
  if(my_month < 0){
    my_year--;
    my_month = 11; //即12月份
  }
  refreshDate();
}

next.onclick = function(e){
  e.preventDefault();
  my_month++;
  if(my_month > 11){
    my_month = 0;
    my_year++;
  }
  refreshDate();
}

另外引用阿里巴巴矢量图标库(采用symbol引入)
引入方式参考链接
最终效果如下:
在这里插入图片描述
该文章参考出处:https://blog.csdn.net/qq_22317389/article/details/80349497

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值