基于jquery实现日历效果
更新时间:2021年3月14日 15:00 点击:2159
本文实例为大家分享了jquery实现日历效果的具体代码,供大家参考,具体内容如下
/** * 2021/3/6 * Calendar */ /* get y Year m Month before days */ function getBDays( y, m ) { return (new Date(y, m, 1).getDay()); } /* get y Year m Month total days */ function getTDays( y, m ) { return (new Date(y, m + 1, -1).getDate() + 1); } /* get y Year m Month last days */ function getBMDays( y, m ) { return (new Date(y, m, -1).getDate() + 1); } function Calendar( nowDate ) { // year, month, day this.year = nowDate.getFullYear(); this.month = nowDate.getMonth(); this.day = nowDate.getDate(); // before days this.beforeDays = getBDays(this.year, this.month); // current month days this.totalDays = getTDays(this.year, this.month); // last month days this.lastDays = getBMDays(this.year, this.month); // save now date this.nowY = nowDate.getFullYear(); this.nowM = nowDate.getMonth(); } Calendar.prototype.initCalendar = function() { // get calendar id let calDiv = $("#Calendar").append("<table></table>"); // get calendar table let calTable = $("#Calendar > table"); // add calendar table tr for ( let n = 0; n < 8; n++ ) { calTable.append('<tr></tr>'); } // get calendar table tr : header let calHeadTr = $("#Calendar > table > tr:first"); // add calendar table tr th for ( let n = 0; n < 3; n++ ) { calHeadTr.append('<th></th>'); } // select index > 0 tr let calBodyTr = $("#Calendar > table > tr:gt(0)"); // add calendar table tr td for ( let n = 0; n < 7; n++ ) { calBodyTr.append('<td></td>'); } } Calendar.prototype.insertDate = function( calName ) { // get calendar table tr td : header let calHeadTh = $("#Calendar > table > tr:first > th"); // modify header content $(calHeadTh[0]).html("<a><</a>"); $(calHeadTh[1]).html(`<a>${this.year} 年 ${this.month + 1} 月</a>`); $(calHeadTh[2]).html("<a>></a>"); // add style to header $(calHeadTh[1]).attr({ "colspan" : 5, "title" : calName }); // weekday arrays const calWeekArr = ['日', '一', '二', '三', '四', '五', '六']; // get calendar table tr td : weekdays let calWeekTd = $("#Calendar > table > tr:eq(1) > td"); for ( let n = 0; n < 7; n++ ) { $(calWeekTd[n]).html(`<a>${calWeekArr[n]}</a>`); } // get calendar table tr td : body let calBodyTd = $("#Calendar > table > tr:gt(1) > td"); // insert before days for (let n = this.beforeDays - 1, lastDays = this.lastDays; n >= 0; n--, lastDays--) { $(calBodyTd[n]).html(`<a>${lastDays}</a>`); $(calBodyTd[n]).attr("class", "other-day"); } // insert current days for (let n = this.beforeDays, i = 1; i <= this.totalDays; i++, n++) { $(calBodyTd[n]).html(`<a>${i}</a>`); if (i == this.day && (new Date(this.year, this.month, 1).getMonth() == this.nowM) && (new Date(this.year, this.month, 1).getFullYear() == this.nowY)) { $(calBodyTd[n]).attr("class", "now-day"); } else { $(calBodyTd[n]).removeAttr("class", "now-day"); } } // insert after days for (let n = this.beforeDays + this.totalDays, i = 1; n < calBodyTd.length; n++, i++) { $(calBodyTd[n]).html(`<a>${i}</a>`); $(calBodyTd[n]).attr("class", "other-day"); } } Calendar.prototype.update = function( newDate ) { // year, month, day this.year = newDate.getFullYear(); this.month = newDate.getMonth(); this.day = newDate.getDate(); // before days this.beforeDays = getBDays(this.year, this.month); // current month days this.totalDays = getTDays(this.year, this.month); // last month days this.lastDays = getBMDays(this.year, this.month); } function initDate() { // create Date object let now = new Date(); let cal = new Calendar( now ); // init and insert cal.initCalendar(); cal.insertDate( 'MyDate' ); // add click event to th:first $("#Calendar > table > tr:first > th:first").click(function(){ now.setMonth( now.getMonth() - 1 ); cal.update( now ); cal.insertDate( 'MyDate' ); }); // add click event to th:last $("#Calendar > table > tr:first > th:last").click(function(){ now.setMonth( now.getMonth() + 1 ); cal.update( now ); cal.insertDate( 'MyDate' ); }); } initDate();
html
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Document</title> <link href="css/dateCal.css" rel="stylesheet" media="screen"> </head> <body> <div id="Calendar"></div> <script src="js/jquery.js"></script> <script src="js/dateCal.js"></script> </body> </html>
CSS:
#Calendar { width: 200px; padding-bottom: 5px; box-shadow: 0 1px 3px #ccc; border: 1px solid #EDEDED; } #Calendar table { width: inherit; text-align: center; user-select: none; font-family: "Comic Sans MS"; border-collapse: collapse; border-spacing: 0px; } #Calendar table tr th { background: #f8f8f8; font-size: 12px; } #Calendar table tr:nth-child(2) { background: #f8f8f8; } #Calendar table tr td { font-size: 10px; } #Calendar table tr td.now-day { color: red; } #Calendar table tr td.other-day { color: lightgray; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。
上一篇: js实现瀑布流布局(无限加载)
下一篇: 微信小程序自定义tabbar组件
相关文章
- 这篇文章主要介绍了jquery实现加载更多"转圈圈"效果,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-11-10
- 最近接了一个项目,其中有需求要用到jquery分页控件,上网也找到了需要分页控件,各种写法各种用法,都是很复杂,最终决定自己动手写一个jquery分页控件,全当是练练手了。写的不好,还请见谅,本分页控件在chrome测试过,其他的兼容性...2015-10-30
- 有时我们在页面上需要选择数值范围,如购物时选取价格区间,购买主机时自主选取CPU,内存大小配置等,使用直观的滑块条直接选取想要的数值大小即可,无需手动输入数值,操作简单又方便。HTML首先载入jQuery库文件以及jRange相关...2015-03-15
- 本文实例讲述了jQuery实现非常实用漂亮的select下拉菜单选择效果。分享给大家供大家参考,具体如下:先来看如下运行效果截图:在线演示地址如下:http://demo.jb51.net/js/2015/js-select-chose-style-menu-codes/具体代码如...2015-11-08
- 本文实例讲述了jquery实现的伪分页效果代码。分享给大家供大家参考,具体如下:这里介绍的jquery伪分页效果,在火狐下表现完美,IE全系列下有些问题,引入了jQuery1.7.2插件,代码里有丰富的注释,相信对学习jQuery有不小的帮助,期...2015-10-30
- 拜读一个开源框架,最想学到的就是设计的思想和实现的技巧。废话不多说,jquery这么多年了分析都写烂了,老早以前就拜读过,不过这几年都是做移动端,一直御用zepto, 最近抽出点时间把jquery又给扫一遍我也不会照本宣科的翻译...2014-05-31
- JQuery使我们在开发Ajax应用程序的时候提高了效率,减少了许多兼容性问题,我们在Ajax项目中,遇到ajax异步获取数据出错怎么办,我们可以通过捕捉error事件来获取出错的信息。在没给大家介绍正文之前先给分享Jquery中AJAX参...2015-11-24
- 当页面打开时我们需要执行一些操作,这个时候如果我们选择使用jquery的话,需要重写他的3中方法,自我感觉没什么区 别,看个人喜好了,第二种感觉比较简单明了: 第一种: 复制代码 代码如下: <script type="text/javas...2014-06-07
jquery中常用的SET和GET$(”#msg”).html循环介绍
复制代码 代码如下: $(”#msg”).html(); //返回id为msg的元素节点的html内容。 $(”#msg”).html(”new content“); //将“new content” 作为html串写入id为msg的元素节点内容中,页面显示粗体的new content $(”...2013-10-13- 这篇文章主要为大家详细介绍了jQuery实现广告显示和隐藏动画,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-07-05
- jquery中jquery.offset().top / left用于获取div距离窗口的距离,jquery.position().top / left 用于获取距离父级div的距离(必须是绝对定位的div)。 (1)先介绍jquery.offset().top / left css: 复制代码 代码如下: *{ mar...2013-10-13
- 有时候我们为了省事就操作键盘组合键去代替使用鼠标,我们今天就使用JQuery实现Ctrl+Enter提交表单。我们发帖时,在内容输入框中输入完内容后,可以点击“提交”按钮来发表内容。可是,如果你够“懒”,你可以不用动鼠标,只需按...2015-10-23
jQuery Mobile开发中日期插件Mobiscroll使用说明
这篇文章主要介绍了jQuery Mobile开发中日期插件Mobiscroll使用说明,需要的朋友可以参考下...2016-03-03- 直接为大家介绍制作过程,希望大家可以喜欢。HTML结构该页面切换特效的HTML结构使用一个<main>元素来作为页面的包裹元素,div.cd-cover-layer用于制作页面切换时的遮罩层,div.cd-loading-bar是进行ajax加载时的loading进...2015-10-30
jQuery 1.9使用$.support替代$.browser的使用方法
jQuery 从 1.9 版开始,移除了 $.browser 和 $.browser.version , 取而代之的是 $.support 。 在更新的 2.0 版本中,将不再支持 IE 6/7/8。 以后,如果用户需要支持 IE 6/7/8,只能使用 jQuery 1.9。 如果要全面支持 IE,并混合...2014-05-31- 本文实例讲述了jQuery实现鼠标滑过链接控制图片的滑动展开与隐藏效果。分享给大家供大家参考,具体如下:这里演示jQuery实现鼠标移动到链接上,滑动展开/隐藏图片效果,鼠标放在“上一页”“下一页”上,立即浮现出所对应的图...2015-10-30
jQuery+PHP发布的内容进行无刷新分页(Fckeditor)
这篇文章将使用jQuery,并结合PHP,将Fckeditor发布的内容进行分页,并且实现无刷新切换页面。 本文假设你是WEB开发人员,掌握了jQuery和PHP相关知识,并且熟知Fckeditor的配置和使用。...2015-10-23- 在DOM操作中,常常需要动态创建HTML内容,使文档在浏览器里的呈现效果发生变化,并且达到各种各样的人机交互目的....2016-04-27
- 这篇文章主要为大家详细介绍了jQuery实现下拉菜单滑动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-08-09
- jQuery实现带玻璃流光质感的手风琴特效是一款基于jQuery+CSS3实现的带玻璃流光质感的手风琴特效,分享给大家,具体如下效果图:具体代码如下:html代码: <section class="strips"> <article class="strips__strip"> <di...2015-11-24