写日历的一些总结

近来项目中有一块日历的部分,找了各种网上的资料没有特别合适的  所有自己写了一下,前前后后折腾了很久

先说一下需求,日历页面中显示当前月份以及当前月份的前三个月,当前日期之前都可以点击,之后日期显示灰色不可点击状态;效果图如下:

html代码:<div class="table-cont"><table class="week"><thead><tr><th>一</th><th>二</th><th>三</th><th>四</th><th>五</th><th>六</th><th>日</th></tr></thead></table><div class="month month-first"></div><div class="day day-first"></div><div class="month month-second"></div><div class="day day-second"></div><div class="month month-third"></div><div class="day day-third"></div><div class="month month-fourth"></div><div class="day day-fourth"></div></div>

js代码

获取当前日期
function getNowDate() {
var today = new Date();
return {
year: today.getFullYear(),
month: today.getMonth() + 1,
day: today.getDate()
}
}
//判断是否为闰年 -> 1 闰年 0 不是
function isLeap(year) {
return year % 4 == 0 ? (year % 100 != 0 ? 1 : (year % 400 == 0 ? 1 : 0)) : 0;
}
//获取指定年月的值 year -> 年份 month -> 真实月份
function getDays(year, month) {
month--;
var first_day = new Date(year,month,1),
next_month_first_day = new Date(year, month + 1, 1),
dayOfWeek = first_day.getDay(),
last_day = (new Date(next_month_first_day.getTime()-1000*60*60*24)).getDate();
return {
dayOfWeek: dayOfWeek,
last_day: last_day,
year: year,
month: month
}
}
//根据天数形成 html_str
function template(info) {
var $html = "";
for(var i=1,j=1; i < info.last_day + info.dayOfWeek; i++) {
if( i == 1 && (i-1) % 7 == 0) {
$html += "<tr>";
}
if(i < info.dayOfWeek) {
$html += "<td></td>";
} else {
if(info.status == -1) {
$html += "<td><a class=‘before‘ href=‘javascript:void(0)‘ >"+ (j++) +"</a></td>";
onclick();
} else if (info.status == 1) {
$html += "<td><a class=‘after‘ href=‘javascript:void(0)‘ >"+ (j++) +"</a></td>";
} else {
$html += "<td><a href=‘javascript:void(0)‘ class=‘" + (j < now_date.day ? "before" : (j == now_date.day ? "active" : "after")) +"‘ >"+ (j++) +"</a></td>";
onclick();
}
}
if(i != 0 && i % 7 == 0) {
$html += "</tr>";
}
}
return $html;
}
//获取前一个月的信息
function getPre(now) {
var count_month = now.month - 1;
var new_year = now.year;
if(count_month <= 0) {
new_year = now.year -1;
count_month = 12;
}
return {
year: new_year,
month: count_month
}
}

//真正的逻辑从这里开始
var now_date = getNowDate(),
pre_1_date = getPre(now_date),
pre_2_date = getPre(pre_1_date);

function handle_date(para_date,title_class,table_class,status) {
//title
$(title_class).html(para_date.year + "年" + para_date.month + "月");
//table
var $table = $("<table></table>");
$(table_class).append($table);
var now_days = getDays(para_date.year, para_date.month);
now_days.status = status;
$table.append(template(now_days));
}
handle_date(now_date, ".month-third",".day-third",0);
handle_date(pre_1_date, ".month-second",".day-second",-1);
handle_date(pre_2_date, ".month-first",".day-first",-1);

// $("a.before").add("a.active").on("click", function() {
// console.log("绑定点击事件!");
// });
function onclick(){
$(‘.before,.active‘).click(function(){
$(this).parents($(‘.day‘)).siblings().find(‘a‘).removeClass(‘active‘);
$(this).addClass(‘active‘);
})
}

时间: 2024-07-28 23:12:49

写日历的一些总结的相关文章

javascript动手写日历组件(2)——优化UI和添加交互(by vczero)

一.优化UI 继上一篇,http://www.cnblogs.com/vczero/p/js_ui_1.html.开始优化UI,主要优化的部分有: (1)增加星期行.(2)字体设置.(3)日期垂直居中.(4)将单元格->底部线条.(5)修改文本的颜色对比.(6)将内部调用的函数加前缀_,如_addHeader()._addWeekday(). 修改的后基本效果如下图: 整个代码做了小修小改: 1 var Calendar = function(div){ 2 this.div = documen

javascript动手写日历组件(1)——构建日历逻辑 (by vczero)

一.分析日历的组成部分和交互要素 (1)组成部分:选择年月部分.星期显示.包含本月(或者有前月和下一个月部分日子) (2)根据选择的年和月份,动态绘制日历面板. (3)一个日历 7(天) * 5(周) = 35格表格. (4)一个月份是统一的一个面板:一个月的头一天一定在日历面板的第一行,根据该天的“星期几”确定位置. (5)第一格子是星期一,最后一个格子是星期日,为5周的日历面板. 二.确定逻辑设计 日历上面的日历,8月1号建军节为什么会出现在这一格?因为一个月的天数是小于5周(35天)的,因

javascript动手写日历组件(3)——内存和性能优化(by vczero)

一.列表 javascript动手写日历组件的文章列表,主要是通过原生的JavaScript写的一个简约的日历组件: (1)javascript动手写日历组件(1)——构建日历逻辑:http://www.cnblogs.com/vczero/p/js_ui_1.html (2)javascript动手写日历组件(2)——优化UI和添加交互:http://www.cnblogs.com/vczero/p/js_ui_2.html (3)javascript动手写日历组件(2)——内存和性能优化:h

用一门非常小众的语言REBOL写日历(万年历)打印的程序

先上效果图由于课程设置,选修了一门非常非常小众的REBOL语言的程序设计概论,就自己观点来看,这门语言小众,语法晦涩,教材也不太好,书中作者试图一直把我们以门外汉这样灌输知识给我们,我觉得这点作者做的不是特别好,比如说流行语言中的变量,用书中作者的话就变成了单字...赋值就是设字...这看来非常的不专业,而且某种程度上貌似在拐弯,这是我对这本教材不能接受的地方.当然,对于初学者来说,这无可厚非.欸,怪我怪我,来了这么渣的专业,全机电学大物3,就我们班学大物2(2比3难),你说一堆搞机器人的学的比

vue之手把手教你写日历组件

---恢复内容开始--- 1.日历组件 1.分析功能:日历基本功能,点击事件改变日期,样式的改变 1.结构分析:html 1.分为上下两个部分 2.上面分为左按钮,中间内容展示,右按钮 下面分为周几展示和日期展示 3.基本结构页面html书写 <template>    <div class="calender2">        <div class="date-header">            <div class

js手写日历插件

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>日历</title> <style> table {

写日历的一些总结(二)

点击切换前一天的日期  只能切换到当前月份的前三个月  例如(今天是2017年3月24日,点击左边按钮只能切换到2016年12月1日左按钮不能再点击,点击右边按钮只能切换到当前日期的前一天右按钮不能再点击)具体js代码如下: var today = new Date();  获取当前日期 var year = today.getFullYear(); var month = today.getMonth()+1; var day = today.getDate(); var m = month;

javascript写日历代码

<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <LINK REL=stylesheet HREF="../../images/ntstyles.css" TYPE="text/css"> <title>Canlender--JavaScript<

IOS日历JTCalendar第三方使用

关于JTCalendar是一个很好的写日历的第三方,我们可已从http://www.code4app.com/ios/有农历的日历/5225b2f96803fa484f000000网址的到实例代码 下面我就介绍一下一些控制日历的代码 控制点击日历日期使选中日期变色的事件 [_calendarManager setDate:_todayDate]; 控制小点标记的事件 if([self haveEventForDay:dayView.date]){ dayView.dotView.hidden =