JS实现日历

7.2--

以前写特效都用jquery,js使用还不熟的很,最近又在看《javascript权威指南》,正好公司的项目有个日历签到的功能,就先用js写个日历控件试试,目前还只实现了基本的功能,先贴代码:

html

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <title></title>
 5     <meta charset="utf-8">
 6     <link rel="stylesheet" href="css/data-plug-in.css"/>
 7 </head>
 8 <body>
 9     <div class="plug-in-date">
10             <span class="plug-in-date-reduce" id="plug-in-date-reduce"><</span>
11             <span class="plug-in-date-time"><span id="plug-in-date-year">2014</span>年<span id="plug-in-date-month">7</span>月</span>
12             <span class="plug-in-date-add" id="plug-in-date-add">></span>
13             <table class="plug-in-date-table" id="plug-in-date-table">
14             </table>
15     </div>
16     <script src="js/date-plug-in.js"></script>
17 </body>
18 </html>

CSS

.plug-in-date{width: 200px;height: 210px;position:absolute;background: #f2f2f2;font-size:13px;font-family:"微软雅黑","黑体","宋体";border-radius:10px;-moz-border-radius: 10px;z-index: 9999;}
.plug-in-date-add,.plug-in-date-reduce{width:20px;height:20px;position: absolute;background:#1B1B1D;line-height:20px;text-align:center;color:#f2f2f2;cursor:pointer;z-index: 99;border-radius:3px;-moz-border-radius: 3px;-webkit-transition:background .3s;}
.plug-in-date-reduce:hover,.plug-in-date-add:hover{background-color: #3F3A3F;}
.plug-in-date-reduce{float: left;left: 10px;top: 10px;}
.plug-in-date-add{float: right;right: 10px;top: 10px;}
.plug-in-date-time{width: 100px;height: 20px;position:absolute;top:10px;left:50px;background:#C1C9D0;line-height:20px;text-align: center;color:#4A4A4A;z-index: 9;border-radius:5px;-moz-border-radius: 5px;}
.plug-in-date-table{width: 182px;position: absolute;top: 35px;left:9px;border-collapse: collapse;}
.plug-in-date-table td,.plug-in-date-table th{width: 20px;height:20px;line-height:20px;text-align:center;border-radius: 10px;}
.plug-in-date-table th{color: #18191B;}
.plug-in-date-table td.plug-in-date-not-cur-month{color:#B3B2B0; }
.plug-in-date-table td:hover{background:#C1C9D0;cursor: pointer;}
.plug-in-date-table td.plug-in-date-cur-time{background:#C1C9D0;}

JS

/*
 *JavaScript日期显示
 *vesion:1.0.0
 *作者:陶z or Xiaoz or TaoShiFu
 *Blog地址:http://www.cnblogs.com/xiaozweb
 *创建日期:2014-7-1
 *最后修改日期:2014-7-2
 */
var dateBtnReduce,dateBtnAdd; //操作按钮
var dateYearE; //年
var dateMonthE; //月
var dateContent;//显示区
dateBtnReduce = document.getElementById("plug-in-date-reduce");
dateBtnAdd = document.getElementById("plug-in-date-add");
dateYearE  = document.getElementById("plug-in-date-year");
dateMonthE = document.getElementById("plug-in-date-month");
dateContent = document.getElementById("plug-in-date-table");
var datePlugIn = new DatePlugIn(); //日期操作
//后退
dateBtnReduce.onclick = function(){
    var year,month;
    year = parseInt(dateYearE.innerText);
    month = parseInt(dateMonthE.innerText);
    month = datePlugIn.reduceMonth(month);
    if(month==12) year = datePlugIn.reduceYear(year);
    dateMonthE.innerText = month;
    dateYearE.innerText = year;
    dateContent.innerHTML = new DrawDate(year,month,datePlugIn).draw();
}
//前进
dateBtnAdd.onclick = function(){
    var year,month;
    year = parseInt(dateYearE.innerText);
    month = parseInt(dateMonthE.innerText);
    month = datePlugIn.addMonth(month);
    if(month==1) year = datePlugIn.addYear(year);
    dateMonthE.innerText = month;
    dateYearE.innerText = year;
    dateContent.innerHTML = new DrawDate(year,month,datePlugIn).draw();
}
var date = new Date();
dateContent.innerHTML = (new DrawDate(date.getFullYear(),date.getMonth()+1,datePlugIn)).draw();
//图形绘制类
function DrawDate(year,month,datePlugIn){
    this.year = year;
    this.month = month;
    //绘制
    this.draw = function(){
        var htmlContent;
        var week;
        var monthNumber1,monthNumber2; //关联月天数统计
        var count1=0,count2=0,count3=0; //绘制控制
        //计算当月1号为周几
        week = datePlugIn.getWeekDayByYearAndMonthAndDay(this.year,this.month,1);
        count1 = week;
        //计算关联三个月的天数
        monthNumber1 = datePlugIn.getDayNumberByYearAndMonth((datePlugIn.reduceMonth(month)==12?datePlugIn.reduceYear(this.year):this.year),datePlugIn.reduceMonth(month));
        monthNumber2 = datePlugIn.getDayNumberByYearAndMonth(this.year,this.month);
        htmlContent =  "<tr>"+
            "<th>日</th>"+
            "<th>一</th>"+
            "<th>二</th>"+
            "<th>三</th>"+
            "<th>四</th>"+
            "<th>五</th>"+
            "<th>六</th>"+
            "</tr>";
        for(var i = 0;i<6;i++){
            htmlContent += "<tr>";
            for(var j =0 ;j<7;j++){
                    if(i==0&&j<week){
                        htmlContent += "<td class=‘plug-in-date-not-cur-month‘>"+(monthNumber1-count1+1)+"</td>";
                        count1--;
                        continue;
                    }
                    if(count2<monthNumber2){
                        htmlContent += "<td>"+(count2+1)+"</td>"
                        count2++;
                    }else{
                        htmlContent += "<td class=‘plug-in-date-not-cur-month‘>"+(count3+1)+"</td>"
                        count3++;
                    }
            }
            htmlContent +="</tr>";
        }
        return htmlContent;
    }
}
//日期操作类
function DatePlugIn(){
    var MIN_YEAR = 2000;//最小年份
    var MAX_YEAR = 2015;//最大年份
    //年份减少
    this.addYear = function(year){
        year++;
        if(year>MAX_YEAR)return MIN_YEAR;
        else return year;
    }
    //年份增加
    this.reduceYear = function(year){
        year--;
        if(year<MIN_YEAR)return MAX_YEAR;
        else return year;
    }
    //月份减少
    this.reduceMonth = function(month){
        month--;
        if(month<1)return 12;
        else return month;
    }
    //月份增加
    this.addMonth = function(month){
        month++;
        if(month>12) return 1;
        else return month;
    }
    //根据年份月份得到天数
    this.getDayNumberByYearAndMonth = function(year,month){
        var LEAP_YEAR = 0;
        var dayNumber = 0;
        if(year%4==0&&year%100!=0||year%400==0) LEAP_YEAR = 1;
        switch (month){
            case 1:case 3:case 5: case 7:case 8:case 10:case 12: dayNumber = 31;break;
            case 2:  dayNumber = 28+LEAP_YEAR;break;
            case 4:case 6:case 9:case 11: dayNumber = 30;break;
        }
        return dayNumber;
    }
    //根据年月日得到周几
    this.getWeekDayByYearAndMonthAndDay = function(year,month,day){
        var myDate=new Date();
        myDate.setFullYear(year,month-1,day);
        return myDate.getDay();
    }
}

贴在这里先,慢慢优化。

JS实现日历,布布扣,bubuko.com

时间: 2024-12-12 04:28:49

JS实现日历的相关文章

js简易日历

js简易日历中设计的知识点:选项卡切换   数组    innerHTML  连接符 与选项卡的区别:div的个数不同 连接符中需要注意的:(优先级) "abc"+12+3+"def"               结果:abc123def "abc"+(12+3)+"def"            结果:abc15def html代码: <body> <div class="contain"

js 面向对象日历实现原理详解

对于前端开发来说,日历空间在网站里应用的很多,比如:填写表单时,是选取一下事件了--等等.下面就来分析一下怎么用js来写一个自己万年历. 在没有开始之前,我们是先弄明白是什么原理,要通过几个步骤来实现. 第一,我们的知道某一个月的某第一天是星期几. 第二,我们得知道某一个月有一共有几天, 只要有了这两部就可以循环出来了,下面看一下代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http

js自定义日历

<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>选择日期</title> <script src="~/Scripts/jquery.1.9.1.min.js"></script> <style class=&qu

js实现日历的简单算法

最近有用到日历可需要编辑文本的日历,为了绑定数据的方便,所以用js写了一套日历,其实思想也是很简单. 实现步骤如下: 1:首先取得处理月的总天数 JS不提供此参数,我们需要计算.考虑到闰年问题会影响二月份的天数,我们先编写一个判断闰年的自编函数: function is_leap(year) {   return (year%100==0?res=(year%400==0?1:0):res=(year%4==0?1:0));} 2:接着定义一个包含十二个月在内的月份总天数的数组: m_days=

js中日历的代码

Html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Con

jquery.jCal.js显示日历插件

描述:日历插件jCal用于需要输入日期的表单文本框. 兼容浏览器:IE浏览器/Firefox/Google Chrome 官方链接: http://www.overset.com/2008/05/1 … cker-jquery-plugin/ JS下载: http://www.ijquery.cn/js/jquery.jCal.min.js 预览:  http://www.ijquery.cn/demo/jCal 打包下载: http://www.ijquery.cn/demo/jCal/jCa

原生js简易日历效果实现

这里我们将用原生js实现简易的日历,原理和之前的原生js选项卡差不多,不过也有些区别: 首先html代码: <div class="container"> <div class="container_cont"> <ul id="cont_ul"> <li class="active"> <span>1</span> <p>jan</p&

js 简单日历

源地址:https://jingyan.baidu.com/article/546ae185fa4f721149f28cbf.htm 文件:index.htm <!DOCTYPE html> <head> <meta charset="utf8"> <title>日历例子</title> <script src="calendar.js"></script> </head>

13、 js处理日历控件(去掉 readonly )

前言:有的日期控件可以输入日期,有的不可以手动输入,对于不可以输入的日历控件,需要先用 js 去掉 readonly 属性,然后直接输入日期文本内容 1.日历控件,以12306为例  2.手动去掉readonly,双击readonly,删除掉,出发日期中就可以手动输入内容了,如下图:  3.js去掉readonly.再输入日期 用 js 去掉元素属性基本思路:先定位到元素,然后用 removeAttribute("readonly")方法删除属性 出发日元素 id 为:train_da