原生js實(shí)現(xiàn)日歷效果
本文實(shí)例為大家分享了js實(shí)現(xiàn)日歷效果的具體代碼,供大家參考,具體內(nèi)容如下
html代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>日歷插件</title>
<link rel="stylesheet" href="./css/reset.css" >
<link rel="stylesheet" href="./css/index.css" >
</head>
<body>
<div class="show">
<button id="pre">上月</button>
<span id="showtime">2019-01</span>
<button id="next">下月</button>
</div>
<div class="box" id="box">
</div>
</body>
<script>
// 獲取本月第一天
function getMonthDay(date){
date=new Date(date.valueOf())
date.setDate(1);
return date
}
// 獲取本月最后一天
function getMonthLastDay(date){
date=new Date(date.valueOf())
date.setMonth(date.getMonth()+1);
date.setDate(0);
return date
}
//獲取本月的時(shí)間對(duì)象集合
function getMonth(date){
var arr=[]
// 獲取本月第一天
var _date=getMonthDay(date)
// // 獲取本月最后一天
var dataLast=getMonthLastDay(date).getDate()
arr.push(new Date(_date.valueOf()))
// 處理本月第一天 到本月最后一天
for(var i =1;i<dataLast;i++){
_date.setDate(_date.getDate()+1)
arr.push(new Date(_date.valueOf()))
}
// 向前補(bǔ)全,重置為本月一號(hào)
_date=getMonthDay(date)
var forln=_date.getDay()
for(var i=0;i<forln;i++){
_date.setDate(_date.getDate()-1)
arr.unshift(new Date(_date.valueOf()))
}
// 向后補(bǔ)全,重置為本月最后一天
_date=getMonthLastDay(date)
forln=_date.getDay()
for(var i=forln;i<6;i++){
_date.setDate(_date.getDate()+1)
arr.push(new Date(_date.valueOf()))
}
return arr
}
// 將集合渲染到頁(yè)面
function renderTable(monthDateArr,date){
document.getElementById("showtime").innerHTML=`${date.getFullYear()}-${date.getMonth()+1}`
var table=document.createElement('table')
var trTh=document.createElement('tr')
trTh.innerHTML=`
<th>周日</th>
<th>周一</th>
<th>周二</th>
<th>周三</th>
<th>周四</th>
<th>周五</th>
<th>周六</th>
`
table.appendChild(trTh)
var tr=document.createElement('tr')
for(let i in monthDateArr){
var td=document.createElement('td')
td.innerHTML=monthDateArr[i].getDate()
tr.appendChild(td)
if((i*1+1)%7===0){
table.appendChild(tr)
tr=document.createElement('tr')
}else if(i == monthDateArr.length-1){
table.appendChild(tr)
}
}
document.getElementById("box").innerHTML=table.outerHTML
}
var date=new Date()
document.getElementById('pre').onclick=function(){
date.setDate(1)
date.setMonth(date.getMonth()-1)
renderTable(getMonth(date),date)
}
document.getElementById('next').onclick=function(){
date.setDate(1)
date.setMonth(date.getMonth()+1)
rrenderTable(getMonth(date),date)
}
renderTable(getMonth(date),date)
</script>
</html>
css代碼
.box{
width: 700px;
margin: 0 auto;
box-sizing: border-box;
padding: 0 1px;
}
table{
width: 100%;
}
th,td{
width:100px;
text-align: center;
}
th{
height: 30px;
line-height: 30px;
background: #e0e5e5;
}
td{
height: 70px;
line-height: 70px;
background: #f3f5f5;
}
.show{
display: flex;
align-items: center;
justify-content: center;
}
更多精彩內(nèi)容請(qǐng)點(diǎn)擊專(zhuān)題《javascript日歷插件使用方法匯總》進(jìn)行學(xué)習(xí)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 如何通過(guò)JS實(shí)現(xiàn)日歷簡(jiǎn)單算法
- 原生JS實(shí)現(xiàn)相鄰月份日歷
- JavaScript實(shí)現(xiàn)簡(jiǎn)單日歷效果
- 基于javascript實(shí)現(xiàn)日歷功能原理及代碼實(shí)例
- JS實(shí)現(xiàn)簡(jiǎn)單日歷特效
- js實(shí)現(xiàn)簡(jiǎn)單的日歷顯示效果函數(shù)示例
- 原生JavaScript實(shí)現(xiàn)日歷功能代碼實(shí)例(無(wú)引用Jq)
- JS實(shí)現(xiàn)帶陰歷的日歷功能詳解
- javascript實(shí)現(xiàn)考勤日歷功能
- 原生JS實(shí)現(xiàn)日歷組件的示例代碼
- js實(shí)現(xiàn)日歷
相關(guān)文章
ES6新特性八:async函數(shù)用法實(shí)例詳解
這篇文章主要介紹了ES6新特性八:async函數(shù)用法,結(jié)合實(shí)例形式分析了async函數(shù)的功能、原理、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-04-04
javascript回車(chē)完美實(shí)現(xiàn)tab切換功能
這篇文章主要介紹了javascript通過(guò)回車(chē)實(shí)現(xiàn)tab切換功能,需要的朋友可以參考下2014-03-03
javascript在事件監(jiān)聽(tīng)方面的兼容性小結(jié)
javascript 在事件監(jiān)聽(tīng)方面的兼容性總結(jié),注意是由于多個(gè)瀏覽器的不一致,導(dǎo)致大家在js書(shū)寫(xiě)時(shí)需要考慮多個(gè)瀏覽器的兼容性。2010-04-04
js實(shí)現(xiàn)分享到隨頁(yè)面滾動(dòng)而滑動(dòng)效果的方法
這篇文章主要介紹了js實(shí)現(xiàn)分享到隨頁(yè)面滾動(dòng)而滑動(dòng)效果的方法,實(shí)例分析了javascript操作頁(yè)面元素滾動(dòng)效果的方法,需要的朋友可以參考下2015-04-04
JavaScript組合拼接字符串的效率對(duì)比測(cè)試
這篇文章主要介紹了JavaScript組合拼接字符串的效率對(duì)比測(cè)試,本文測(cè)試了IE6、Firefox、Mozilla、Netscape、Opera等瀏覽器,需要的朋友可以參考下2014-11-11

