Home Web Front-end JS Tutorial javascript写的日历类(基于pj)_javascript技巧

javascript写的日历类(基于pj)_javascript技巧

May 16, 2016 pm 06:13 PM

先看看效果:

复制代码 代码如下:






使用很简单;
先创建一个Calendar对象
复制代码 代码如下:

var calendar=new Calendar();

只要调用show()方法即可显示
复制代码 代码如下:

calendar.show()

其实调用的是pj库的show方法
因此在使用之前要先把pj库引进来,在把Calendar.js引进来就可以
Calendar的主要方法有
getDateString([pattern]) format是日期格式,默认是yyyy-mm-dd
show:function(duration) 显示日历
hide:function(duration)隐藏日历
fadeIn:function(duration)淡入日历
fadeOut:function(duration)淡出日历
locateAt:function(obj,offsetX,offsetY)把日历定位到指定的元素正下方,offsetX、offsetY可选,是相对于X、Y轴的偏移量

只定义了两个事件
onChange(fn)//当改变年份或者月份是的事件监听器,以当前Calendar对象为上下文执行fn函数
onSelect(fn)//当选择日期时触发的函数,以当前Calendar对象为上下文执行fn函数

还是把源代码粘贴上来吧(有点多,因为封装了一个table)
复制代码 代码如下:

//date:默认开始日期,不需要可以是null(以当天日期开始),yearListLength:年份下拉列表长度
function Calendar(date,yearListLength){
var currentDate=date||new Date(),_this=this;

this.container=pj("
").appendTo(document.body).setStyle({overflow:'hidden',background: '#99CCFF',border:'1px solid #CCC',fontSize:'12px',height:'160px',width:'180px',position:"absolute",display:'none'});
this.container.get().innerHTML='
             
             
             
             
             
             
';

this.spans=pj("span",this.container.get());
this.as=pj("a",this.container.get()).setStyle({textDecoration:"none",color:"#333"});
this.selects=pj("select",this.container.get());
this.getCurrentDate=function(){return currentDate;};
this.init();
this.initYearList(yearListLength||70);

var change=function(){},select=function(){};
this.onChange=function(fn){//当改变年份或者月份是的事件监听器,以当前Calendar对象为上下文执行fn函数
if(pj.isFunction(fn))change=fn;
};
this.onSelect=function(fn){//当选择日期时触发的函数,以当前Calendar对象为上下文执行fn函数
if(pj.isFunction(fn))select=fn;
};

this.selects[0].onchange=function(){
currentDate.setFullYear(parseInt(this.options[this.selectedIndex].value));
_this.init();
change.apply(_this,arguments);
};//选择年份
this.selects[1].onchange=function(){
currentDate.setMonth(parseInt(this.options[this.selectedIndex].value)-1);
_this.init();
change.apply(_this,arguments);
};//选择月份

this.selects[1].options[currentDate.getMonth()].selected=true;

this.as.addListener({
click:function(){currentDate.setDate(parseInt(this.innerHTML));select.apply(_this,arguments);},
mouseover:function(){if(_this.todate.getDate()+""!=this.innerHTML)this.style.color="#CCC";},
mouseout:function(){if(_this.todate.getDate()+""!=this.innerHTML)this.style.color="#333";}
});

pj("td",this.container.get(0)).addListener({
mouseover:function(){this.style.backgroundColor="#303";},
mouseout:function(){this.style.backgroundColor="#9CF";}
});
}
Calendar.prototype={
init:function(){
var cur=this.getCurrentDate(),
i=new Date(cur.getFullYear(),cur.getMonth(),1).getDay(),//取星期
j=new Date(cur.getFullYear(),cur.getMonth()+1,0).getDate();//取当月最大日数
//alert(i);
this.spans[0].innerHTML=cur.getFullYear();
this.spans[1].innerHTML=cur.getMonth()+1;
var m=0,n=this.as.length-1,isTodate=Calendar.isThisMonth(cur);
while(mfor(var day=1;daythis.as[i].innerHTML=day;
if(isTodate&&day==this.todate.getDate()){
this.todateLink=this.as[i];
pj.setStyle(this.as[i],{color:"#F60",fontWeight:"bold"});
}else if(!isTodate&&day==this.todate.getDate()&&this.todateLink){
pj.setStyle(this.todateLink,{color:"#333",fontWeight:"normal"});
}
}
},
initYearList:function(len){
Calendar.emptySelect(this.selects[0]);

var cur=this.getCurrentDate(),now=this.todate.getFullYear(),max=Math.max(now-cur.getFullYear(),len);
for(var y=0;yvar option=document.createElement("option");
if(cur.getFullYear()==now)option.selected=true;
else option.selected=false;
option.text=now;
option.value=now--;
try{
this.selects[0].add(option,null);
}catch(e){
this.selects[0].add(option);
}
}
},
getDateString:function(format){//format是日期格式,如yyyy-mm-dd
if(!format||!/y{4}.m{2}.d{2}/.test(format))format="yyyy-mm-dd";
format=format.replace(/^yyyy/,this.getCurrentDate().getFullYear());
format=format.replace(/mm/,Calendar.fx(this.getCurrentDate().getMonth()+1));
format=format.replace(/dd/,Calendar.fx(this.getCurrentDate().getDate()));
return format;
},
todate:new Date(),
todateLink:null,
show:function(duration){this.container.show(duration);},
hide:function(duration){this.container.hide(duration);},
fadeIn:function(duration){this.container.fadeIn(duration);},
fadeOut:function(duration){this.container.fadeOut(duration);},
locateAt:function(obj,offsetX,offsetY){
this.container.locate(obj,pj.LEFT_BOTTOM_POSITION,offsetX,offsetY);
}
};
Calendar.emptySelect=function(target){
if(!target.options)return;
while(target.options.length>0)target.remove(0);
};
Calendar.fx=function(dig){return digCalendar.isThisMonth=function(date){
return date.getFullYear()==Calendar.prototype.todate.getFullYear()&&date.getMonth()==Calendar.prototype.todate.getMonth();
};

脚本之家打包
在线演示 http://demo.jb51.net/js/Calendar_pj/index.htm
打包下载 http://www.jb51.net/jiaoben/33760.html
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Replace String Characters in JavaScript Replace String Characters in JavaScript Mar 11, 2025 am 12:07 AM

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

8 Stunning jQuery Page Layout Plugins 8 Stunning jQuery Page Layout Plugins Mar 06, 2025 am 12:48 AM

Leverage jQuery for Effortless Web Page Layouts: 8 Essential Plugins jQuery simplifies web page layout significantly. This article highlights eight powerful jQuery plugins that streamline the process, particularly useful for manual website creation

Build Your Own AJAX Web Applications Build Your Own AJAX Web Applications Mar 09, 2025 am 12:11 AM

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

10 Mobile Cheat Sheets for Mobile Development 10 Mobile Cheat Sheets for Mobile Development Mar 05, 2025 am 12:43 AM

This post compiles helpful cheat sheets, reference guides, quick recipes, and code snippets for Android, Blackberry, and iPhone app development. No developer should be without them! Touch Gesture Reference Guide (PDF) A valuable resource for desig

Improve Your jQuery Knowledge with the Source Viewer Improve Your jQuery Knowledge with the Source Viewer Mar 05, 2025 am 12:54 AM

jQuery is a great JavaScript framework. However, as with any library, sometimes it’s necessary to get under the hood to discover what’s going on. Perhaps it’s because you’re tracing a bug or are just curious about how jQuery achieves a particular UI

10 jQuery Fun and Games Plugins 10 jQuery Fun and Games Plugins Mar 08, 2025 am 12:42 AM

10 fun jQuery game plugins to make your website more attractive and enhance user stickiness! While Flash is still the best software for developing casual web games, jQuery can also create surprising effects, and while not comparable to pure action Flash games, in some cases you can also have unexpected fun in your browser. jQuery tic toe game The "Hello world" of game programming now has a jQuery version. Source code jQuery Crazy Word Composition Game This is a fill-in-the-blank game, and it can produce some weird results due to not knowing the context of the word. Source code jQuery mine sweeping game

How do I create and publish my own JavaScript libraries? How do I create and publish my own JavaScript libraries? Mar 18, 2025 pm 03:12 PM

Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

jQuery Parallax Tutorial - Animated Header Background jQuery Parallax Tutorial - Animated Header Background Mar 08, 2025 am 12:39 AM

This tutorial demonstrates how to create a captivating parallax background effect using jQuery. We'll build a header banner with layered images that create a stunning visual depth. The updated plugin works with jQuery 1.6.4 and later. Download the

See all articles