CSS+Javascript+Html日历控件

最近,因需要用HTML+JAVASCRIPT+CSS实现了一个日历控件,效果如下:

单击上月、下月进行日历切换。当前日期在日历中变颜色标注显示。还是老老套路、老方法,分HML+CSS+JAVASCRIPT三部分代码。

一、html代码

html 复制代码
<h1>学习计划</h1>	
	<div class="month">      
	  <ul>
	    <li class="prev"><上月</li>
	    <li class="next">下月></li>
	    <li style="text-align:center"><span id="monthbox">10月</span><br>
	      <span style="font-size:18px" id="yearbox">2023年</span>
	    </li>
	  </ul>
	</div>
	
	<ul class="weekdays">
	  <li>星期一</li>
	  <li>星期二</li>
	  <li>星期三</li>
	  <li>星期四</li>
	  <li>星期五</li>
	  <li>星期六</li>
	  <li>星期日</li>
	</ul>	
	<ul class="days"></ul>	

这段代码主要包含三个部分,一是头部显示年月,上月、下月切换按钮;二是显示星期一到日;三是日期容器,存在日期。

二、CSS代码

css 复制代码
* {box-sizing:border-box;}
		ul {list-style-type: none;}
		body {font-family: Verdana,sans-serif;}		
		.month {padding: 70px 25px;width: 100%;background: #1abc9c;}		
		.month ul {margin: 0;padding: 0;}		
		.month ul li {color: white;font-size: 20px;text-transform: uppercase;letter-spacing: 3px;}		
		.month .prev {float: left;padding-top: 10px;cursor: pointer;}		
		.month .next {float: right;padding-top: 10px;cursor: pointer;}		
		.weekdays {margin: 0;padding: 10px 0;background-color: #ddd;}		
		.weekdays li {display: inline-block;width: 13.6%;color: #666;text-align: center;}		
		.days {padding: 10px 0;background: #eee;margin: 0;}		
		.days li {list-style-type: none;display: inline-block;width: 13.6%;text-align: center;margin-bottom: 5px;font-size:12px;color: #777;}		
		.days li .active {padding: 5px;background: #1abc9c;color: white !important}		
		/* Add media queries for smaller screens */
		@media screen and (max-width:720px) {.weekdays li, .days li {width: 13.1%;}}		
		@media screen and (max-width: 420px) {.weekdays li, .days li {width: 12.5%;} .days li .active {padding: 2px;}}
		@media screen and (max-width: 290px) {.weekdays li, .days li {width: 12.2%;}}

这段代码主要定义了日历的样式,一个主要的方法简述如下:

  1. box-sizing:border-box;就是将border和padding数值包含在width和height之内,这样的好处就是修改border和padding数值盒子的大小不变。
  2. @media screen and (max-width:720px) 。表示当浏览器的可视区域小于720px时候,执行。

三、Javascript代码

javascript 复制代码
<script type="text/javascript">
	     var currentDate=new Date();
		function showDateList(){
			let year = currentDate.getFullYear();
			let month = currentDate.getMonth()+1;
			let date = currentDate.getDate();
			let firstWeekDay = new Date(year,month-1,1).getDay();
			let monthDays = new Date(year,month,0).getDate();
			let str="";
			let daylength = monthDays+firstWeekDay-1;
			let startDay = firstWeekDay-1
			if(firstWeekDay==0) {
				daylength =monthDays+6;
				startDay=6;
			}
			for (var i = 0; i <daylength ; i++) {
				if(i<startDay)
				{
					str +="<li></li>"
				}
				else
				{
					let today = new Date();
					let todate =(i-startDay+1);
					console.log(date)
					if(year == today.getFullYear() && month == today.getMonth()+1 &&todate== today.getDate())
					{
						str +="<li><span class='active'>"+todate+"</span></li>";
					}
					else{
						str +="<li>"+todate+"</li>";
					}					
				}
			}
			document.querySelector("#monthbox").innerHTML=month+"月";
			document.querySelector("#yearbox").innerHTML=year+"年";
			document.querySelector(".days").innerHTML=str;
		}
		showDateList();
		document.querySelector(".next").onclick= function(){
			currentDate.setMonth(currentDate.getMonth() + 1);
			showDateList();
		}
		document.querySelector(".prev").onclick= function(){
			currentDate.setMonth(currentDate.getMonth() - 1);
			showDateList();
		}
	</script>	

此段代码实现了当月日历情况,单击上月、下月进行月份切换。

这样我们的日历就成型了,完整代码如下,请参考:

html 复制代码
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <title></title>
	<style>
		* {box-sizing:border-box;}
		ul {list-style-type: none;}
		body {font-family: Verdana,sans-serif;}
		
		.month {padding: 70px 25px;width: 100%;background: #1abc9c;}		
		.month ul {margin: 0;padding: 0;}		
		.month ul li {color: white;font-size: 20px;text-transform: uppercase;letter-spacing: 3px;}		
		.month .prev {float: left;padding-top: 10px;cursor: pointer;}		
		.month .next {float: right;padding-top: 10px;cursor: pointer;}		
		.weekdays {margin: 0;padding: 10px 0;background-color: #ddd;}		
		.weekdays li {display: inline-block;width: 13.6%;color: #666;text-align: center;}		
		.days {padding: 10px 0;background: #eee;margin: 0;}		
		.days li {list-style-type: none;display: inline-block;width: 13.6%;text-align: center;margin-bottom: 5px;font-size:12px;color: #777;}		
		.days li .active {padding: 5px;background: #1abc9c;color: white !important}		
		/* Add media queries for smaller screens */
		@media screen and (max-width:720px) {.weekdays li, .days li {width: 13.1%;}}		
		@media screen and (max-width: 420px) {.weekdays li, .days li {width: 12.5%;} .days li .active {padding: 2px;}}
		@media screen and (max-width: 290px) {.weekdays li, .days li {width: 12.2%;}}
	</style>
    
</head>
<body>
	
	<h1>学习计划</h1>	
	<div class="month">      
	  <ul>
	    <li class="prev"><上月</li>
	    <li class="next">下月></li>
	    <li style="text-align:center"><span id="monthbox">10月</span><br>
	      <span style="font-size:18px" id="yearbox">2023年</span>
	    </li>
	  </ul>
	</div>
	
	<ul class="weekdays">
	  <li>星期一</li>
	  <li>星期二</li>
	  <li>星期三</li>
	  <li>星期四</li>
	  <li>星期五</li>
	  <li>星期六</li>
	  <li>星期日</li>
	</ul>	
	<ul class="days"></ul>	
	<script type="text/javascript">
	     var currentDate=new Date();
		function showDateList(){
			let year = currentDate.getFullYear();
			let month = currentDate.getMonth()+1;
			let date = currentDate.getDate();
			let firstWeekDay = new Date(year,month-1,1).getDay();
			let monthDays = new Date(year,month,0).getDate();
			let str="";
			let daylength = monthDays+firstWeekDay-1;
			let startDay = firstWeekDay-1
			if(firstWeekDay==0) {
				daylength =monthDays+6;
				startDay=6;
			}
			for (var i = 0; i <daylength ; i++) {
				if(i<startDay)
				{
					str +="<li></li>"
				}
				else
				{
					let today = new Date();
					let todate =(i-startDay+1);
					console.log(date)
					if(year == today.getFullYear() && month == today.getMonth()+1 &&todate== today.getDate())
					{
						str +="<li><span class='active'>"+todate+"</span></li>";
					}
					else{
						str +="<li>"+todate+"</li>";
					}
					
				}
			}
			document.querySelector("#monthbox").innerHTML=month+"月";
			document.querySelector("#yearbox").innerHTML=year+"年";
			document.querySelector(".days").innerHTML=str;
		}
		showDateList();
		document.querySelector(".next").onclick= function(){
			currentDate.setMonth(currentDate.getMonth() + 1);
			showDateList();
		}
		document.querySelector(".prev").onclick= function(){
			currentDate.setMonth(currentDate.getMonth() - 1);
			showDateList();
		}
	</script>	
</body>
</html>
相关推荐
ThomasChan1231 小时前
Typescript 多个泛型参数详细解读
前端·javascript·vue.js·typescript·vue·reactjs·js
zzlyx991 小时前
.NET 9 微软官方推荐使用 Scalar 替代传统的 Swagger
javascript·microsoft·.net
Bunury1 小时前
组件封装-List
javascript·数据结构·list
我命由我123452 小时前
NPM 与 Node.js 版本兼容问题:npm warn cli npm does not support Node.js
前端·javascript·前端框架·npm·node.js·html5·js
浪浪山小白兔2 小时前
HTML5 语义元素详解
前端·html·html5
Orange3015112 小时前
【自己动手开发Webpack插件:开启前端构建工具的个性化定制之旅】
前端·javascript·webpack·typescript·node.js
五行星辰3 小时前
用 Java 发送 HTML 内容并带附件的电子邮件
java·html
Jacob程序员4 小时前
leaflet绘制室内平面图
android·开发语言·javascript
eguid_14 小时前
JavaScript图像处理,常用图像边缘检测算法简单介绍说明
javascript·图像处理·算法·计算机视觉
wanfeng_094 小时前
视频m3u8形式播放 -- python and html
python·html·video·hls·m3u8