Native JavaScript implements a calendar

Irving the procedural ape 2020-11-11 11:51:38
native javascript implements calendar


Design objectives : Don't rely on other libraries 、 Compatible with some older browsers 、 Configurable and extensible .

The test page :

1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title> Test calendar control </title> 6 <style> 7 #div1{ 8 width: 256px; 9 height: 202px; 10 box-sizing: border-box; 11 background-color: #fff; 12 box-shadow: 1px 1px 3px #999; 13 position:absolute; 14 left:0px; 15 } 16 #div2 { 17 width: 236px; 18 height: 166px; 19 box-sizing: border-box; 20 background-color: #fff; 21 box-shadow: 1px 1px 3px #999; 22 position:absolute; 23 left:300px; 24 } 25 .select_date{ 26 padding-left: 10px;box-sizing:border-box;width: 100px;height: 30px;background-color: #fff;font-size: 12px;float: left;line-height: 30px; 27 } 28 .span_rq{ 29 width: 60px;display: inline-block; 30 } 31 </style> 32 <script src="DatePicker.js"></script> 33 </head> 34 <body> 35 <div id="div_all_base" style="background-color: beige"> 36 <div id="div1"> 37 38 </div> 39 <div id="div2"> 40 41 </div> 42 <div style="width: 600px;height: 30px;left:600px;position:absolute;float:left" id="div3"> 43 <div style="float: left;line-height: 30px;margin-left: 10px;font-size: 12px;"> from </div> 44 <div class="select_date"><span class="span_rq"> Start date </span><img src="image/rili2.jpg" style="margin-left: 10px;width: 20px;margin-top: 6px;cursor: pointer" alt="" onclick="var divs=div_rilis.getElementsByClassName('div_rili');divs[0].style.display='block';divs[1].style.display='none'"></div> 45 <div style="position: absolute;width:256px;height: 202px;top:50px;display: none" class="div_rili"></div> 46 <div style="float: left;line-height: 30px;margin-left: 10px;font-size: 12px;"> To </div> 47 <div class="select_date"><span class="span_rq"> End date </span><img src="image/rili2.jpg" style="margin-left: 10px;width: 20px;margin-top: 6px;cursor: pointer" alt="" onclick="var divs=div_rilis.getElementsByClassName('div_rili');divs[1].style.display='block';divs[0].style.display='none'"></div> 48 <div style="position: absolute;width:256px;height: 202px;top:50px;left:120px;display: none" class="div_rili"></div> 49 </div> 50 </div> 51 </body> 52 <script> 53 var div_rilis=document.getElementById("div3"); 54 55 var apiHost=""; 56 var data_enabledate={list_enabledate:[{CBRQ:20201111},{CBRQ:20201112},{CBRQ:20201113},{CBRQ:20201114},{CBRQ:20201115}],type:"ok"}; 57 // use AJAX Query the optional date from the back end , You need to return the format of the data with data_enabledate identical 58 //var datepicker1=new DatePicker("datepicker1",{pickcallback:loadPage,bannerBackColor:"rgb(0,112,192)",searchEnableUrl:apiHost+"/public/searchEnableDate"});// 59 // Directly with front-end data , Set click callback 、banner Color 、 Activation date 60 var datepicker1=new DatePicker("datepicker1",{pickcallback:loadPage,bannerBackColor:"rgb(0,112,192)",searchEnableUrl:data_enabledate}); 61 var div_rili1=document.getElementById("div1"); 62 div_rili1.appendChild(datepicker1.div); 63 datepicker1.reloadDateList(); 64 65 // Set more configuration items , The pressed button changes color , The program can be within a certain range according to the periphery div Adjust the size of each button , In addition, the font size is set , And allow clicking on inactive dates 66 var datepicker2=new DatePicker("datepicker2",{ 67 pickcallback:function(str_date,that){ 68 datepicker2.pick0(str_date); 69 loadPage(str_date); 70 },bannerBackColor:"rgb(0,112,192)",width:236,height:166,shadowSize:0,paddingSize:0, 71 searchEnableUrl:data_enabledate,contentFontSize:9,flag_canPickUnEnabled:true, 72 }); 73 var div_rili2=document.getElementById("div2"); 74 div_rili2.appendChild(datepicker2.div); 75 datepicker2.reloadDateList(); 76 77 function loadPage(str_date){ 78 79 alert(" The date of the click is "+str_date); 80 } 81 82 // Common start and end dates , Here you set the... For each calendar title, The activation button is not distinguished , And compare the value of the calendar with span Of innerHTML Connect ( Use arr_valuelink Attributes can be associated with input or textview Of value) 83 var divs=div_rilis.getElementsByClassName('div_rili'); 84 var spans=div_rilis.getElementsByClassName("span_rq"); 85 var datepicker1b=new DatePicker("datepicker1b",{pickcallback:function(value,that){ 86 var divs=div_rilis.getElementsByClassName('div_rili'); 87 divs[0].style.display='none'; 88 divs[1].style.display='none'; 89 DatePicker.linkValue(value,that) 90 },bannerBackColor:"rgb(0,112,192)",arr_innerlink:[spans[0]],title:" Start date "});// And react It's different here spans stay onload It doesn't regenerate after , So it can be directly related to dom object !! 91 var datepicker2b=new DatePicker("datepicker2b",{pickcallback:function(value,that){ 92 var divs=div_rilis.getElementsByClassName('div_rili'); 93 divs[0].style.display='none'; 94 divs[1].style.display='none'; 95 DatePicker.linkValue(value,that) 96 },bannerBackColor:"rgb(0,112,192)",arr_innerlink:[spans[1]],title:" End date "}); 97 divs[0].appendChild(datepicker1b.div); 98 divs[1].appendChild(datepicker2b.div); 99 datepicker1b.reloadDateList();100 datepicker2b.reloadDateList();101 </script>102 </html>

Calendar button image :

Code implementation :( It is suggested to look from the back to the front )

1 DatePicker=function(id,obj_p) 2 { 3 // If you already have this div Then the establishment fails 4 var div=document.getElementById("id"); 5 if(div) 6 { 7 console.log(" Components id repeat , Please use other components id"); 8 return; 9 } 10 div=document.createElement("div");// The outermost container 11 this.id=id; 12 this.div=div; 13 div.id=id; 14 var _this=this; 15 16 this.width=obj_p.width||256; 17 this.height=obj_p.height||202; 18 this.shadowSize=obj_p.shadowSize||4; 19 this.shadowColor=obj_p.shadowColor||"rgb(203,203,203)"; 20 this.backColor=obj_p.backColor||"white"; 21 //this.boxShadow=obj_p.boxShadow||"inset 0 0 4px 4px rgb(203,203,203)"; 22 div.style.border="0px"; 23 div.style.width=this.width+"px"; 24 div.style.height=this.height+"px"; 25 div.style.boxShadow="inset 0 0 "+this.shadowSize+"px "+this.shadowSize+"px "+this.shadowColor; 26 div.style.position="absolute"; 27 div.style.backgroundColor=this.backColor; 28 29 this.title=obj_p.title||" Date selection "; 30 //this.noTitle=obj.noTitle||false; 31 //this.noBanner=obj.noBanner||false; 32 this.h1=obj_p.h1||24;// The top row of spaces 33 this.h2=obj_p.h2||24; 34 //this.h3=obj_p.h3||208; 35 this.titleFontSize=obj_p.titleFontSize||12; 36 this.contentFontSize=obj_p.contentFontSize||10; 37 var span=document.createElement("span")// The title bar of the calendar 38 span.innerText=this.title; 39 span.style.height=this.h1+"px"; 40 span.style.fontSize=this.titleFontSize+"px"; 41 span.style.fontWeight="bold"; 42 span.style.lineHeight=this.h1+"px"; 43 span.style.display="inline-block"; 44 span.style.left=this.shadowSize+"px"; 45 span.style.paddingLeft=this.shadowSize+"px"; 46 span.style.position="absolute"; 47 span.style.zIndex="2"; 48 div.appendChild(span); 49 50 this.bannerBackColor=obj_p.bannerBackColor||"rgb(169,16,10)"; 51 this.bannerFontColor=obj_p.bannerFontColor||"white"; 52 this.date=obj_p.date||(new Date());// Default date 53 this.year=this.date.getFullYear(); 54 this.month=this.date.getMonth(); 55 this.day31=this.date.getDate(); 56 this.day7=this.date.getDay(); 57 this.arr_day7=obj_p.arr_day7||[""," One "," Two "," 3、 ... and "," Four "," 5、 ... and "," 6、 ... and "," Japan "]; 58 this.arr_bannerp=obj_p.arr_bannerp||([// Button to adjust the month and year 59 {id:"btn1",width:10,marginLeft:20,text:"《",onclick:function(){_this.year-=10;_this.changeYear()}} 60 ,{id:"btn2",width:10,marginLeft:5,text:"〈",onclick:function(){_this.year-=1;_this.changeYear()}} 61 ,{id:"btn3",width:40,marginLeft:5,text:this.year,onclick:function(){}} 62 ,{id:"btn4",width:10,marginLeft:5,text:"〉",onclick:function(){_this.year+=1;_this.changeYear()}} 63 ,{id:"btn5",width:10,marginLeft:5,text:"》",onclick:function(){_this.year+=10;_this.changeYear()}} 64 ,{id:"btn6",width:10,marginLeft:50,text:"〈",onclick:function(){_this.month-=1;if(_this.month<0){_this.month=11;_this.year-=1;}_this.changeMonth();_this.changeYear()}} 65 ,{id:"btn7",width:20,marginLeft:5,text:this.month+1,onclick:function(){}} 66 ,{id:"btn8",width:10,marginLeft:5,text:"〉",onclick:function(){_this.month+=1;if(_this.month>11){_this.month=0;_this.year+=1;}_this.changeMonth();_this.changeYear()}} 67 ]) 68 69 var div_banner=document.createElement("div"); 70 div_banner.style.height=this.h2+"px"; 71 //div_banner.style.width="100%"; 72 div_banner.style.left=this.shadowSize+"px"; 73 div_banner.style.right=this.shadowSize+"px"; 74 div_banner.style.top=this.h1+"px"; 75 div_banner.style.position="absolute"; 76 div_banner.style.backgroundColor=this.bannerBackColor; 77 div_banner.style.lineHeight=this.h2+"px"; 78 div_banner.style.zIndex="2"; 79 //div_banner.style.fontSize=this.contentFontSize+2+"px"; 80 //div_banner.style.color=this.bannerFontColor; 81 //div_banner.style.fontWeight="bold"; 82 83 //this.changeYearBack=obj_p. 84 85 var len=this.arr_bannerp.length; 86 var sum_left=0; 87 for(var i=0;i<len;i++)//banner On the button to adjust the month and year 88 { 89 var bannerp=this.arr_bannerp[i]; 90 var btn=document.createElement("button"); 91 btn.id=bannerp.id; 92 btn.style.position="absolute"; 93 btn.style.backgroundColor=this.bannerBackColor; 94 btn.style.border="0px"; 95 btn.style.padding="0px"; 96 btn.style.textAlign="center"; 97 btn.style.color=this.bannerFontColor;//button This property does not automatically inherit 98 btn.style.fontWeight="bold"; 99 btn.style.fontSize=this.contentFontSize+2+"px";100 btn.style.height="100%";101 btn.style.lineHeight=this.h2+"px";102 btn.style.width=bannerp.width+"px";103 btn.style.left=sum_left+bannerp.marginLeft+"px";104 btn.innerText=bannerp.text;105 btn.onclick=bannerp.onclick;106 sum_left+=(bannerp.width+bannerp.marginLeft);107 this[btn.id]=btn;108 div_banner.appendChild(btn);109 }110 div.appendChild(div_banner);111 this.paddingSize=obj_p.paddingSize||6;112 113 this.gridBackColor=obj_p.gridBackColor||"#eeeeee";// Cell background color 114 this.gridColor=obj_p.gridColor||"#000000";// The text color of this month's cells 115 this.gridColor0=obj_p.gridColor0||"#888888";// Associated with the text color of non current month cells 116 this.hoverBorderColor=obj_p.hoverBorderColor||"#888888";// The color of the border after moving the mouse into an optional cell 117 this.enableBackColor=obj_p.enableBackColor||"rgb(207,232,252)";// The background color of the cell that can be selected 118 this.enableBackColorPicked=obj_p.enableBackColorPicked||"rgb(2.........
版权声明
本文为[Irving the procedural ape]所创,转载请带上原文链接,感谢

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云