2018-11-23

                                                      簡單日歷
效果2.gif

html
<!DOCTYPE html>
<html>
<head>
<title>日歷</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="11.css">
</head>
<body onselectstart="return false;">
<div id="container">
<div id="calendar">
<table id="top">
<thead>
<tr id="topMiddle">
<td>
<span id="left"><</span>
<span id="toDay"></span>
<span id="right">></span>
</td>
</tr>
</thead>
</table>
<table id="dayDate">
<thead>
<tr id="week">
<td>日</td>
<td>一</td>
<td>二</td>
<td>三</td>
<td>四</td>
<td>五</td>
<td>六</td>
</tr>
</thead>
<tbody id="dayday"></tbody>
</table>
</div>
<div id="writer">
<div id="btn">toDay</div>
<div id="input">
<input type="text" id="goYear">
<span>年</span>
<input type="text" id="goMonth">
<span>月</span>
<input type="text" id="goDay">
<span>日</span>
<div>
<span id="Go">Go</span>
<span id="clear">clear</span>
</div>
</div>
</div>`
</div>
<script type="text/javascript" src="111.js"></script>
</body>
</html>

css
{
margin: 0;
padding: 0;
}
body{
background-color: #9e9e9e54; /
全屏背景色*/
}

container{

position: relative;  /*相對定位*/
height: 700px;
width: 400px;       /* 設置寬和高 */
margin: 50px auto;      /*屏幕居中顯示*/
box-shadow: 10px 10px #7955481f; /*設置陰影*/

}

calendar{

position: absolute;         /*絕對定位*/
width: 100%;
height: 70%;
background-color: #68EE99;  /*背景色*/

}

top{

position: absolute;
width: 400px;
height: 60px;
border-bottom: 2px solid #00800038; /*設置下方分界線*/

}

topMiddle{

height: 80px;  /*頂部高度*/

}

toDay{

display: inline-block;
width: 230px;
text-align: center;

}

top td span{

font-size: 40px;
cursor: pointer; 
margin-left: 25px;
color: #fff;

}

top td span:hover{

opacity: 0.5;

}

dayDate td:hover{

background-color: #9e9e9e54;

}

dayDate{

position: absolute;
width: 100%;
margin-top: 100px; 

}

dayDate td{

text-align: center;  /*文本水平居中*/
color: #fff;
cursor: pointer;
font-size: 20px;
height: 40px;

}

writer{

position: absolute;
width: 100%;
height: 30%;
margin-top: 490px;
background-color: #fff;

}

Go,

clear,

btn{

width: 50px;
height: 50px;
line-height: 50px;
text-align: center;
margin: 20px auto;
border-style: outset;
cursor: pointer;

}

Go:active,

clear:active,

btn:active{

border-style: inset;

}
input{
height: 30px;
width: 80px;
}

input{

width: 400px;
text-align: center;

}
.hidden{
visibility: hidden;
}

dayday tr{

height: 55px;

}

JavaScript
function byId(id) {
return typeof(id) === "string"?document.getElementById(id):id;
}

var toDay = byId('toDay');
var left = byId('left');
var right = byId('right');
var dayday = byId('dayday');

var date = new Date();
var year = date.getFullYear();
var month = date.getMonth()+1;
var day = date.getDate();
var nowyear = year;
var nowmonth = month;
var nowday = day;
var now = byId('dayday').getElementsByTagName('td');
var btn = byId('btn');
var goYear = byId('goYear');
var goMonth = byId('goMonth');
var goDay = byId('goDay');
var Go = byId('Go');
var clear = byId('clear');
console.log(now)
console.log(year)
console.log(month)
console.log(day)

//跳轉日歷
Go.onclick = function(){
if((goMonth.value>0&&goMonth.value<=12)&&(goDay.value>0&&goDay.value<=31)&&(goYear.value>0)){
month = parseInt(goMonth.value);
day = parseInt(goDay.value);
year = parseInt(goYear.value);
toDay.innerHTML = year+'-'+month+'-'+day;
dayDay();
id();
color();
}
else{
alert('輸入錯誤請重新輸入!');
}

}

//清除輸入
clear.onclick = function(){
goMonth.value='';
goDay.value='';
goYear.value='';
}

//回到今天
btn.onclick = function(){
day = nowday;
month = nowmonth;
year = nowyear;
toDay.innerHTML = nowyear+'-'+nowmonth+'-'+nowday;
dayDay();
id();
color();
}

//給每個日子附ID
function id(){
var k=1;
for(i=0;i<now.length;i++){
if(now[i].innerHTML!=''){
now[i].id= k;
k++;
}
}
}

//添加當前日期顏色
function color(){
for(var i =0;i<now.length;i++){
if(now[i].id ==day){
now[i].style.backgroundColor = "orange";
}
}
}
//添加日期
function dayDay(){
var sum=0;
if((year%4==0&&year%100!=0)||(year%400==0)){
var monthDay = [31,29,31,30,31,30,31,31,30,31,30,31];
}
else{
var monthDay = [31,28,31,30,31,30,31,31,30,31,30,31];
}
for(var i =1;i<year-1;i++){
if((i%4==0&&i%100!=0)||(i%400==0)){
sum+=366;
}
else{
sum+=365;
}
}
for(var i =0;i<month-1;i++){
sum+=monthDay[i]; //加月份的天數(shù)
}
if((year-1)%4==0){
sum+=2;
}
else{
sum+=1;
}
sum = sum%7; //星期幾
console.log(sum)
var tbodyHtml = '<tr>';
var tagClass = 'hidden';
function bigMonth(){
if(sum<6){
for(var i=1;i<=sum+1;i++){
tbodyHtml = tbodyHtml+ "<td class=" + tagClass + "></td>";
}
}
else{
sum=-1;
}
for(var i=1;i<=31;i++){
tbodyHtml =tbodyHtml+'<td>'+i+'</td>'
sum++;
if(sum>=6){
tbodyHtml += '</tr><tr>'
sum=-1;
}
}
}
function smallMonth(){
if(sum<6){
for(var i=1;i<=sum+1;i++){
tbodyHtml = tbodyHtml+ "<td class=" + tagClass + "></td>";
}
}
else{
sum=-1
}
for(var i=1;i<=30;i++){
tbodyHtml =tbodyHtml+'<td>'+i+'</td>'
sum++;
if(sum>=6){
tbodyHtml += '</tr><tr>'
sum=-1;
}
}
}
function yes(){
if(sum<6){
for(var i=1;i<=sum+1;i++){
tbodyHtml = tbodyHtml+ "<td class=" + tagClass + "></td>";
}
}
else{
sum=-1
}
for(var i=1;i<=29;i++){
tbodyHtml =tbodyHtml+'<td>'+i+'</td>'
sum++;
if(sum>=6){
tbodyHtml += '</tr><tr>'
sum=-1;
}
}
}
function no(){
if(sum<6){
for(var i=1;i<=sum+1;i++){
tbodyHtml = tbodyHtml+ "<td class=" + tagClass + "></td>";
}
}
else{
sum=-1
}
for(var i=1;i<=28;i++){
tbodyHtml =tbodyHtml+'<td>'+i+'</td>'
sum++;
if(sum>=6){
tbodyHtml += '</tr><tr>'
sum=-1;
}
}

    }
if((year%4==0&&year%100!=0)||(year%400==0)){
    if((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12)){
        bigMonth();
    }
    if((month==4)||(month==6)||(month==9)||(month==11)){
        smallMonth();
    }
    if(month==2){
        yes();
    }
}
else{
    if((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12)){
        bigMonth();
    }
    if((month==4)||(month==6)||(month==9)||(month==11)){
        smallMonth();
    }
    if(month==2){
        no();
    }
}
dayday.innerHTML = tbodyHtml;

}

+function(){
toDay.innerHTML = nowyear+'-'+nowmonth+'-'+nowday;
dayDay();
id();
color();
}();

left.onclick = function(){
month--;
if(month<=0){
year--;
month = 12;
}
toDay.innerHTML = year+'-'+month+'-'+day;
dayDay();
id();
color();
}
right.onclick = function(){
month++;
if(month>12){
year++;
month = 1;
}
toDay.innerHTML = year+'-'+month+'-'+day;
dayDay();
id();
color();
}

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末向胡,一起剝皮案震驚了整個濱河市职辅,隨后出現(xiàn)的幾起案子走搁,更是在濱河造成了極大的恐慌担败,老刑警劉巖刮便,帶你破解...
    沈念sama閱讀 216,544評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件脯爪,死亡現(xiàn)場離奇詭異启上,居然都是意外死亡碍侦,警方通過查閱死者的電腦和手機粱坤,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,430評論 3 392
  • 文/潘曉璐 我一進店門隶糕,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人站玄,你說我怎么就攤上這事枚驻。” “怎么了株旷?”我有些...
    開封第一講書人閱讀 162,764評論 0 353
  • 文/不壞的土叔 我叫張陵再登,是天一觀的道長。 經(jīng)常有香客問我晾剖,道長锉矢,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,193評論 1 292
  • 正文 為了忘掉前任齿尽,我火速辦了婚禮沽损,結果婚禮上,老公的妹妹穿的比我還像新娘循头。我一直安慰自己绵估,他們只是感情好,可當我...
    茶點故事閱讀 67,216評論 6 388
  • 文/花漫 我一把揭開白布卡骂。 她就那樣靜靜地躺著壹士,像睡著了一般。 火紅的嫁衣襯著肌膚如雪偿警。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,182評論 1 299
  • 那天唯笙,我揣著相機與錄音螟蒸,去河邊找鬼。 笑死崩掘,一個胖子當著我的面吹牛七嫌,可吹牛的內容都是我干的。 我是一名探鬼主播苞慢,決...
    沈念sama閱讀 40,063評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼诵原,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了挽放?” 一聲冷哼從身側響起绍赛,我...
    開封第一講書人閱讀 38,917評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎辑畦,沒想到半個月后吗蚌,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,329評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡纯出,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,543評論 2 332
  • 正文 我和宋清朗相戀三年蚯妇,在試婚紗的時候發(fā)現(xiàn)自己被綠了敷燎。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,722評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡箩言,死狀恐怖硬贯,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情陨收,我是刑警寧澤饭豹,帶...
    沈念sama閱讀 35,425評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站畏吓,受9級特大地震影響墨状,放射性物質發(fā)生泄漏。R本人自食惡果不足惜菲饼,卻給世界環(huán)境...
    茶點故事閱讀 41,019評論 3 326
  • 文/蒙蒙 一肾砂、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧宏悦,春花似錦镐确、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,671評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至砖瞧,卻和暖如春息堂,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背块促。 一陣腳步聲響...
    開封第一講書人閱讀 32,825評論 1 269
  • 我被黑心中介騙來泰國打工荣堰, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人竭翠。 一個月前我還...
    沈念sama閱讀 47,729評論 2 368
  • 正文 我出身青樓振坚,卻偏偏與公主長得像,于是被迫代替她去往敵國和親斋扰。 傳聞我的和親對象是個殘疾皇子渡八,可洞房花燭夜當晚...
    茶點故事閱讀 44,614評論 2 353

推薦閱讀更多精彩內容