以前用过几个日历控件,包括一些在网上下载的、个人开发的js日历,后来也使用了梅花雪的日历,感觉功能强大,界面美观,之后一直使用,不过最近的一个项目中在求日历支持firefox等其它浏览器,这时发现梅花雪不能满足要求,于是google了一把,找到了一个支持多浏览器的日历---jscalendar。
下载地址: http://www.dynarch.com/projects/calendar/
jscalendar功能比较强大,同时界面也不错。
jscalendar显示位置的问题----在使用过程中发现,当要输入的文本框在页面下方时, 日历显示的时候可能会把输入框挡住,我想的效果是若输入框在页面下方时,日历自动显示在输入框的上方。
查看calendar-setup.js和calendar.js文件,知道日历默认显示在输入框的右下方,且当日历显示位置超过页面滚动高度时则将日历显示位置上调,但只是使日历的显示位置不超过页面高度,这时可能会把输入框挡住。控制日历显示的函数是showAtElement,于是修改calendar.js文件:
/** Shows the calendar near a given element. */
Calendar.prototype.showAtElement = function (el, opts) {
var self = this;
var p = Calendar.getAbsolutePos(el);
if (!opts || typeof opts != "string") {
this.showAt(p.x, p.y + el.offsetHeight);
return true;
}
function fixPosition(box) {
if (box.x < 0)
box.x = 0;
if (box.y < 0)
box.y = 0;
var cp = document.createElement("div");
var s = cp.style;
s.position = "absolute";
s.right = s.bottom = s.width = s.height = "0px";
document.body.appendChild(cp);
var br = Calendar.getAbsolutePos(cp);
document.body.removeChild(cp);
if (Calendar.is_ie) {
br.y += document.body.scrollTop;
br.x += document.body.scrollLeft;
} else {
br.y += window.scrollY;
br.x += window.scrollX;
}
var tmp = box.x + box.width - br.x;
if (tmp > 0) {
box.x -= tmp;
}
tmp = box.y + box.height - br.y;
if (tmp > 0) {
//box.y -= tmp;
box.y -= box.height + box.elHeight;
}
};
this.element.style.display = "block";
Calendar.continuation_for_the_fucking_khtml_browser = function() {
var w = self.element.offsetWidth;
var h = self.element.offsetHeight;
self.element.style.display = "none";
var valign = opts.substr(0, 1);
var halign = "l";
if (opts.length > 1) {
halign = opts.substr(1, 1);
}
// vertical alignment
switch (valign) {
case "T": p.y -= h; break;
case "B": p.y += el.offsetHeight; break;
case "C": p.y += (el.offsetHeight - h) / 2; break;
case "t": p.y += el.offsetHeight - h; break;
case "b": break; // already there
}
// horizontal alignment
switch (halign) {
case "L": p.x -= w; break;
case "R": p.x += el.offsetWidth; break;
case "C": p.x += (el.offsetWidth - w) / 2; break;
case "l": p.x += el.offsetWidth - w; break;
case "r": break; // already there
}
p.width = w;
p.elWidth = el.offsetWidth;
//p.height = h + 40;
p.height = h;
p.elHeight = el.offsetHeight;
self.monthsCombo.style.display = "none";
fixPosition(p);
self.showAt(p.x, p.y);
};
if (Calendar.is_khtml)
setTimeout("Calendar.continuation_for_the_fucking_khtml_browser()", 10);
else
Calendar.continuation_for_the_fucking_khtml_browser();
};
下载地址: http://www.dynarch.com/projects/calendar/
jscalendar功能比较强大,同时界面也不错。
jscalendar显示位置的问题----在使用过程中发现,当要输入的文本框在页面下方时, 日历显示的时候可能会把输入框挡住,我想的效果是若输入框在页面下方时,日历自动显示在输入框的上方。
查看calendar-setup.js和calendar.js文件,知道日历默认显示在输入框的右下方,且当日历显示位置超过页面滚动高度时则将日历显示位置上调,但只是使日历的显示位置不超过页面高度,这时可能会把输入框挡住。控制日历显示的函数是showAtElement,于是修改calendar.js文件:
/** Shows the calendar near a given element. */
Calendar.prototype.showAtElement = function (el, opts) {
var self = this;
var p = Calendar.getAbsolutePos(el);
if (!opts || typeof opts != "string") {
this.showAt(p.x, p.y + el.offsetHeight);
return true;
}
function fixPosition(box) {
if (box.x < 0)
box.x = 0;
if (box.y < 0)
box.y = 0;
var cp = document.createElement("div");
var s = cp.style;
s.position = "absolute";
s.right = s.bottom = s.width = s.height = "0px";
document.body.appendChild(cp);
var br = Calendar.getAbsolutePos(cp);
document.body.removeChild(cp);
if (Calendar.is_ie) {
br.y += document.body.scrollTop;
br.x += document.body.scrollLeft;
} else {
br.y += window.scrollY;
br.x += window.scrollX;
}
var tmp = box.x + box.width - br.x;
if (tmp > 0) {
box.x -= tmp;
}
tmp = box.y + box.height - br.y;
if (tmp > 0) {
//box.y -= tmp;
box.y -= box.height + box.elHeight;
}
};
this.element.style.display = "block";
Calendar.continuation_for_the_fucking_khtml_browser = function() {
var w = self.element.offsetWidth;
var h = self.element.offsetHeight;
self.element.style.display = "none";
var valign = opts.substr(0, 1);
var halign = "l";
if (opts.length > 1) {
halign = opts.substr(1, 1);
}
// vertical alignment
switch (valign) {
case "T": p.y -= h; break;
case "B": p.y += el.offsetHeight; break;
case "C": p.y += (el.offsetHeight - h) / 2; break;
case "t": p.y += el.offsetHeight - h; break;
case "b": break; // already there
}
// horizontal alignment
switch (halign) {
case "L": p.x -= w; break;
case "R": p.x += el.offsetWidth; break;
case "C": p.x += (el.offsetWidth - w) / 2; break;
case "l": p.x += el.offsetWidth - w; break;
case "r": break; // already there
}
p.width = w;
p.elWidth = el.offsetWidth;
//p.height = h + 40;
p.height = h;
p.elHeight = el.offsetHeight;
self.monthsCombo.style.display = "none";
fixPosition(p);
self.showAt(p.x, p.y);
};
if (Calendar.is_khtml)
setTimeout("Calendar.continuation_for_the_fucking_khtml_browser()", 10);
else
Calendar.continuation_for_the_fucking_khtml_browser();
};