JQ日历编写

日历描述:带年份筛选功能,当天日期绿色背景显示,当月显示日期,带上个月补全下个月补全,文字颜色较淡显示。左右按钮能操作本年内的月份切换。

<!DOCTYPE html>
<meta charset="utf-8">
<html>
<head>
    <title></title>
    <style type="text/css">
        *{
            margin:0;
            padding: 0;
        }
        html,body {
            width: 100%;
        }
        ul li {
            list-style: none;
        }
        i {
            font-style: normal;
        }
        .dateBox {
            width: calc(100% - 20);
            display: flex;
            align-items: center;
            flex-direction: column;
            padding: 10px;
            min-width:550px;
        }
        .titleYear {
            width: 60px;
            height:40px;
            background: #000;
            color: #fff;
            line-height: 40px;
            text-align: center;
            border-radius: 4px;
            cursor: pointer;
        }
        .titleMonth {
            width: 100%;
            height: 80px;
            text-align: center;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .titleMonth i {
            font-size: 30px;
            cursor: pointer;
        }
        .titleMonth .monthName {
            display: inline-block;
            width: 60%;
            height: 80px;
            line-height: 80px;
            /*overflow: hidden;*/
        }
        .monthName span {
            width: 100%;
            display: inline-block;
        }
        .titleWeek {
            width: 100%;
            display: flex;
        }
        .titleWeek li {
            flex: 1;
            height: 40px;
            display: flex;
            justify-content: center;
            align-items: center;
            margin-left: 10px;
            border-radius: 4px;
        }
        .titleWeek li:first-child {
            margin-left: 0;
        }
        .dayList {
            width: 100%;
            overflow: hidden;
        }
        .dayList li {
            width: 13%;
            float: left;
            text-align: center;
            height: 50px;
            line-height: 50px;
            background: #fafafa;
            margin-right: 1.5%;
            margin-bottom: 10px;
            border-radius: 6px;
        }
        .dayList li:nth-of-type(7n) {
            margin-right: 0;
        }
        .dayList .nowDateStyle {
            background: #4eb900;
            color: #fff;
        }
        .dayList .previousMonthDay,.nextMonthDay {
            color: #e6e1e1;
        }
        .none {
            display: none;
        }
        .yearBox li {
            width: 32%;
            float: left;
            text-align: center;
            height: 50px;
            line-height: 50px;
            background: #fafafa;
            margin-right: 2%;
            margin-bottom: 10px;
            border-radius: 6px;
        }
        .yearBox li:nth-of-type(3n) {
            margin-right: 0;
        }
    </style>
</head>
<body>
    <div class="dateBox">
        <div class="titleYear">2017</div>
        <div class="titleMonth">
            <i class="leftBtn"><</i>
            <div class="monthName" data-mouthNum='8'>8</div>
            <i class="rightBtn">></i>
        </div>
        <ul class="titleWeek">
            <li>星期日</li>
            <li>星期一</li>
            <li>星期二</li>
            <li>星期三</li>
            <li>星期四</li>
            <li>星期五</li>
            <li>星期六</li>
        </ul>
        <ul class="dayList">
        </ul>
    </div>
    <ul class="yearBox none">
    </ul>
</body>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
var nowDate = new Date(),
    nowYear = nowDate.getFullYear(),
    nowMonth = nowDate.getMonth(),
    nowDay = nowDate.getDate(),
    yearMonthDayRun = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], //闰年每个月的总天数
    yearMonthDay = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],//平年每个月的总天数;
    NameOfMonth = ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"];
    //年份盒子显示,分别显示当前年份前10年,和后十年
    var yearList = '';
    for(var i = 10; i>=0; i--){
        yearList += `<li class="years">${nowYear - i}</li>`
    }
    for(var i = 1; i<= 10; i++){
        yearList += `<li class="years">${nowYear + i}</li>`
    }
    $(".yearBox").html(yearList)
    //如果是闰年则返回true
    function runNian(yearNum) {
        if (yearNum % 4 == 0 && yearNum % 100 != 0 || yearNum % 400 == 0) {
            return true;
        } else {
            return false;
        }
    }
    function dayList(yearStr,monthStr){
        var spanList = "";
        var yearNum = Number(yearStr); //转成数字类型
        var thisYear = new Date().getFullYear();
        nowYear = yearNum;
        var monthNum = Number(monthStr) - 1; //js的月份从0开始,也就是0代表一月,1代表二月
        var firstDayDate = new Date(yearNum, monthNum, 1).getDay(); //获取月份第一天礼拜几
        var lastDayDate = new Date(yearNum, monthNum+1, 1).getDay() == 0 ? 6 : (new Date(yearNum, monthNum+1, 1).getDay()) - 1; //获取月份最后一天礼拜几
        var previousMonth = monthNum - 1;
        //上个月天数计算
        var previousDaysCount;
        if(monthNum === 0){
            previousDaysCount = runNian(yearNum-1) ? yearMonthDayRun[11] : yearMonthDay[11];
        }else{
            previousDaysCount = runNian(yearNum) ? yearMonthDayRun[previousMonth] : yearMonthDay[previousMonth];
        }
        // var previousDaysCount = runNian(yearNum) ? yearMonthDayRun[previousMonth] : yearMonthDay[previousMonth];
        //当前月份天数计算
        var nowDaysCount = runNian(yearNum) ? yearMonthDayRun[monthNum] : yearMonthDay[monthNum];

        //上个月剩几天
        for(var i = previousDaysCount - firstDayDate + 1; i<= previousDaysCount; i++){
            spanList += `<li class="previousMonthDay">${i}</li>`
        }
        for(var i = 1; i <= nowDaysCount; i++){
            if(thisYear === nowYear && nowMonth === monthNum && nowDay === i){
                spanList += `<li class="nowDateStyle">${i}</li>`
            }else{
                spanList += `<li>${i}</li>`
            }

        }
        if(lastDayDate < 6){
            for(var i = 0; i< 6-lastDayDate; i++){
                spanList += `<li class="nextMonthDay">${i+1}</li>`
            }
        }
        $(".dayList").html(spanList);
        $(".monthName").html(NameOfMonth[monthNum]);
    }
    dayList(nowYear, nowMonth + 1);
    var olunaName = $(".monthName");
    function changeTheMonthName(numberMonth) {
        olunaName.attr('data-mouthNum', numberMonth);
        olunaName.html(NameOfMonth[numberMonth - 1]);
    }
    //点击左侧按钮
        $(".leftBtn").click(function(){
        var numberMonth = olunaName.attr('data-mouthNum');
        if(numberMonth > 1){
            numberMonth --;
        }
        dayList(nowYear, numberMonth);
        changeTheMonthName(numberMonth);
    })
    //点击右侧按钮
    $(".rightBtn").click(function(){
        var numberMonth = olunaName.attr('data-mouthNum');
        if(numberMonth < 12){
            numberMonth ++;
        }

        dayList(nowYear, numberMonth);
        changeTheMonthName(numberMonth);
    })
//点击年份
$(".titleYear").click(function(){
    $(".dateBox").addClass("none");
    $(".yearBox").removeClass("none");
})
$(".yearBox").on('click',".years",function(){
    var text = $(this).text();
    $(".titleYear").text(text);
    $(".dateBox").removeClass("none");
    $(".yearBox").addClass("none");
    $(".monthName").attr('data-mouthNum',1)
    dayList(text,1)
    $(".monthName").text(NameOfMonth[0]);
})
</script>
</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 220,137评论 6 511
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,824评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,465评论 0 357
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,131评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,140评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,895评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,535评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,435评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,952评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,081评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,210评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,896评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,552评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,089评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,198评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,531评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,209评论 2 357

推荐阅读更多精彩内容