js實(shí)現(xiàn)簡單日歷效果
更新時(shí)間:2021年05月18日 08:35:26 作者:睡個(gè)好覺_
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)簡單日歷效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了js實(shí)現(xiàn)簡單日歷效果的具體代碼,供大家參考,具體內(nèi)容如下

## css模塊
<style type="text/css">
*{
margin: 0;
padding: 0;
}
.date{
width: 300px;
height: 220px;
border: 1px solid #000;
margin: 100px auto;
}
.title{
width: 200px;
display: flex;
font-size: 12px;
margin: auto;
text-align: center;
justify-content: space-around;
align-items: center;
}
.year{
margin: 0 40px;
display: flex;
flex-direction: column;
}
#week{
border-top: 1px solid #000;
border-bottom: 1px solid #000;
margin: auto;
list-style-type: none;
display: flex;
}
#week li{
display: inline-block;
text-align: center;
flex:1;
}
#ul{
list-style-type: none;
margin-top: 5px;
}
#ul li {
display: inline-block;
width: 40px;
height: 21px;
text-align: center;
border: 1px solid #fff;
}
.current{
color:red;
}
#ul li:hover{
border: 1px solid red;
}
#prev,#next{
cursor: pointer;
}
</style>
## html
<div class="date">
<div class="title">
<span id="prev"><上一月</span>
<div class="year">
<span id="year">2021</span>
<span id="month">5月</span>
</div>
<span id="next">下一月></span>
</div>
<!-- 用ul做日歷 -->
<ul id="week">
<li>日</li>
<li>一</li>
<li>二</li>
<li>三</li>
<li>四</li>
<li>五</li>
<li>六</li>
</ul>
<ul id="ul">
</ul>
</div>
## js代碼
<script type="text/javascript">
// date對象,方便切換月份,所以設(shè)置為全局對向
let date = new Date();
// 點(diǎn)擊切換月份的事件
document.getElementById('prev').addEventListener('click',function(){
date.setMonth(date.getMonth()-1);
add();
})
document.getElementById('next').addEventListener('click',function(){
date.setMonth(date.getMonth()+1);
add();
})
add();
//制作日歷的函數(shù)
function add(){
// 當(dāng)前年
let cYear = date.getFullYear();
// 當(dāng)前月
let cMonth = date.getMonth()+1;
// 獲取到當(dāng)前日期
let cDay = date.getDate();
// 寫入年月
document.getElementById('year').innerHTML = cYear;
document.getElementById('month').innerHTML = cMonth+'月';
let days = new Date(cYear,cMonth,-1);
// 當(dāng)前月份的天數(shù)
let n = days.getDate()+1;
// 每個(gè)月的第一天是星期幾
let week = new Date(cYear,cMonth-1,1).getDay();
let html = '';
// 寫入dom
for(let i=0;i<week;i++){
html+=`<li></li>`
}
for(let i=1;i<=n;i++){
if(i==cDay){
html+=`<li class="current">${i}</li>`
}else{
html+=`<li>${i}</li>`
}
}
// 一次性插入
document.getElementById('ul').innerHTML = html
}
</script>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
微信小程序movable-view的可移動(dòng)范圍示例詳解
這篇文章主要介紹了微信小程序movable-view的可移動(dòng)范圍?,movable-view不管怎么移動(dòng)都要完全包含住movable-area,也就是說movable-area不能超出movable-view的區(qū)域范圍,反之亦然,本文通過示例介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
js方法數(shù)據(jù)驗(yàn)證的簡單實(shí)例
下面小編就為大家?guī)硪黄猨s方法數(shù)據(jù)驗(yàn)證的簡單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-09-09
純前端使用插件pdfjs實(shí)現(xiàn)將pdf轉(zhuǎn)為圖片的步驟
這篇文章主要介紹了純前端使用插件pdfjs實(shí)現(xiàn)將pdf轉(zhuǎn)為圖片的步驟,在實(shí)現(xiàn)過程中遇到了跨域問題,后臺(tái)設(shè)置跨域但前端配置無效,最終采用后臺(tái)返回PDF的base64格式,通過PDF.js將base64轉(zhuǎn)換為二進(jìn)制數(shù)據(jù)加載PDF,需要的朋友可以參考下2025-01-01
JS判斷鼠標(biāo)進(jìn)入容器的方向與window.open新窗口被攔截的問題
這篇文章主要給大家介紹了利用Javascript判斷鼠標(biāo)進(jìn)入容器方向的方法,以及window.open新窗口被攔截的問題分析,文中給出了詳細(xì)圖文介紹和示例代碼,相信對大家的理解和學(xué)習(xí)具有一定的參考借鑒價(jià)值,下面來一起看看吧。2016-12-12
微信小程序swiper實(shí)現(xiàn)文字縱向輪播提示效果
這篇文章主要介紹了微信小程序swiper實(shí)現(xiàn)文字縱向輪播提示效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01

