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. [front end -- JavaScript] knowledge point (IV) -- memory leakage in the project (I)
  2. This mechanism in JS
  3. Vue 3.0 source code learning 1 --- rendering process of components
  4. Learning the realization of canvas and simple drawing
  5. gin里获取http请求过来的参数
  6. vue3的新特性
  7. Get the parameters from HTTP request in gin
  8. New features of vue3
  9. vue-cli 引入腾讯地图(最新 api,rocketmq原理面试
  10. Vue 学习笔记(3,免费Java高级工程师学习资源
  11. Vue 学习笔记(2,Java编程视频教程
  12. Vue cli introduces Tencent maps (the latest API, rocketmq)
  13. Vue learning notes (3, free Java senior engineer learning resources)
  14. Vue learning notes (2, Java programming video tutorial)
  15. 【Vue】—props属性
  16. 【Vue】—创建组件
  17. [Vue] - props attribute
  18. [Vue] - create component
  19. 浅谈vue响应式原理及发布订阅模式和观察者模式
  20. On Vue responsive principle, publish subscribe mode and observer mode
  21. 浅谈vue响应式原理及发布订阅模式和观察者模式
  22. On Vue responsive principle, publish subscribe mode and observer mode
  23. Xiaobai can understand it. It only takes 4 steps to solve the problem of Vue keep alive cache component
  24. Publish, subscribe and observer of design patterns
  25. Summary of common content added in ES6 + (II)
  26. No.8 Vue element admin learning (III) vuex learning and login method analysis
  27. Write a mini webpack project construction tool
  28. Shopping cart (front-end static page preparation)
  29. Introduction to the fluent platform
  30. Webpack5 cache
  31. The difference between drop-down box select option and datalist
  32. CSS review (III)
  33. Node.js学习笔记【七】
  34. Node.js learning notes [VII]
  35. Vue Router根据后台数据加载不同的组件(思考-&gt;实现-&gt;不止于实现)
  36. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  37. 【JQuery框架,Java编程教程视频下载
  38. [jQuery framework, Java programming tutorial video download
  39. Vue Router根据后台数据加载不同的组件(思考-&gt;实现-&gt;不止于实现)
  40. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  41. 【Vue,阿里P8大佬亲自教你
  42. 【Vue基础知识总结 5,字节跳动算法工程师面试经验
  43. [Vue, Ali P8 teaches you personally
  44. [Vue basic knowledge summary 5. Interview experience of byte beating Algorithm Engineer
  45. 【问题记录】- 谷歌浏览器 Html生成PDF
  46. [problem record] - PDF generated by Google browser HTML
  47. 【问题记录】- 谷歌浏览器 Html生成PDF
  48. [problem record] - PDF generated by Google browser HTML
  49. 【JavaScript】查漏补缺 —数组中reduce()方法
  50. [JavaScript] leak checking and defect filling - reduce() method in array
  51. 【重识 HTML (3),350道Java面试真题分享
  52. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  53. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  54. [re recognize HTML (3) and share 350 real Java interview questions
  55. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  56. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  57. 【重识 HTML ,nginx面试题阿里
  58. 【重识 HTML (4),ELK原来这么简单
  59. [re recognize HTML, nginx interview questions]
  60. [re recognize HTML (4). Elk is so simple