初始化數據時,為每個可高亮顯示的鏈接創建了lightbox對象。該類的代碼具體實現如下:
var lightbox = Class.create();
lightbox.prototype = {
yPos : 0,
xPos : 0,
//構造方法,ctrl為創建該對象的元素
initialize: function(ctrl) {
//將該元素的鏈接賦值給this.content
this.content = ctrl.href;
//為該元素添加onclick事件activate方法
Event.observe(ctrl, 'click', this.activate.bindAsEventListener(this), false);
ctrl.onclick = function(){return false;};
},
//當單擊鏈接時
activate: function(){
if (browser == 'Internet Explorer'){//判斷為IE浏覽器
this.getScroll();
this.prepareIE('100%', 'hidden');
this.setScroll(0,0);
this.hideSelects('hidden');//隱藏所有的<select>標記
}
//調用該類中的displayLightbox方法
this.displayLightbox("block");
},
prepareIE: function(height, overflow){
bod = document.getElementsByTagName('body')[0];
bod.style.height = height;
bod.style.overflow = overflow;
htm = document.getElementsByTagName('html')[0];
htm.style.height = height;
htm.style.overflow = overflow;
},
hideSelects: function(visibility){
selects = document.getElementsByTagName('select');
for(i = 0; i < selects.length; i++) {
selects[i].style.visibility = visibility;
}
},
getScroll: function(){
if (self.pageYOffset) {
this.yPos = self.pageYOffset;
} else if (document.documentElement && document.documentElement.scrollTop){
this.yPos = document.documentElement.scrollTop;
} else if (document.body) {
this.yPos = document.body.scrollTop;
}
},
setScroll: function(x, y){
window.scrollTo(x, y);
},
displayLightbox: function(display){
//將覆蓋層顯示
$('overlay').style.display = display;
//將高亮層顯示
$('lightbox').style.display = display;
//如果不是隱藏狀態,則調用該類中的loadInfo方法
if(display != 'none') this.loadInfo();
}
//該方法發送Ajax請求
loadInfo: function() {
//當請求完成後調用本類中processInfo方法
var myAjax = new Ajax.Request(
this.content,
{method: 'get', parameters: "", onComplete: this.processInfo.bindAsEvent Listener (this)}
);
},
// 將返回的文本信息顯示到高亮層上
processInfo: function(response){
//獲得返回的文本數據
var result = response.responseText;
//顯示到高亮層
info = "<div id='lbContent'>" + result + "</div>";
//在info元素前插入一個元素
new Insertion.Before($('lbLoadMessage'), info)
//改變該元素的class name的值
$('lightbox').className = "done";
//調用本類中actions方法
this.actions();
var ctrl=$('lightbox');
//為高亮層添加事件處理方法reset
Event.observe(ctrl, 'click', this.reset.bindAsEventListener(this), false);
ctrl.onclick = function(){return false;};
},
//恢復初始狀態
reset:function(){
//隱藏覆蓋層
$('overlay').style.display="none";
//清空返回數據
$('lbContent').innerHTML="";
//隱藏高亮層
$('lightbox').style.display="none";
},
// Search through new links within the lightbox, and attach click event
actions: function(){
lbActions = document.getElementsByClassName('lbAction');
for(i = 0; i < lbActions.length; i++) {
Event.observe(lbActions[i], 'click', this[lbActions[i].rel].bindAs EventListener(this), false);
lbActions[i].onclick = function(){return false;};
}
}
}
提示:由於該對象比較復雜,讀者可以仔細參閱代碼的注釋部分。