做彈跳視窗時最麻煩的往往不是視窗本身,而是「怎麼讓使用者只能跟這個視窗互動」。做法是在彈出層底下鋪一層半透明遮罩,把整片瀏覽器蓋住,底層元素自然點不到。
<script src="jquery.js"></script>
<script src="effect/maskDiv.js"></script>
<script>
// 事件配置模組
var moduleEvent = [
{ "idName": "close", "eventName": "click", "doMethod": "CLOSE_DIV" },
{ "idObj": window, "eventName": "resize", "doMethod": "RESIZE_WINDOW" }
];
qihoo_effect_maskDiv.showMaskDiv("your div id", moduleEvent);
// 注意:該 div 的 display 請先設為 none
</script>
若要遮蓋 Flash 物件,記得把 Flash 的 WMode 設成 Transparent,否則不論 z-index 多高,Flash 都會浮在最上層。
var qihoo_effect_maskDiv = {
_module : {},
_css : {},
_instance: "",
_event : {
"CLOSE_WINDOW" : function() { window.close(); },
"CLOSE_DIV" : function() { qihoo_effect_maskDiv.closeDiv(); },
"RESIZE_WINDOW": function() { qihoo_effect_maskDiv.resizeWindow(); }
},
_isIe : eval("false;/*@cc_on@if(@\x5fwin32)isMSIE=true@end@*/"),
init : function () {
if (this._instance) { return this._instance; }
this.appendBackGroundDiv();
this._instance = this;
return this._instance;
},
appendBackGroundDiv : function () {
this._module.backGroundDiv = $("<div id='qihoo_effect_maskDiv_module_backGroundDiv'></div>");
this._module.backGroundDiv.appendTo("body");
this._css.mask = {
"background-color": "#000", "position": "absolute",
"-moz-opacity": "0.75", "filter": "alpha(opacity=75)", "z-index": 5,
"width" : document.body.clientWidth + 100,
"height": document.body.clientHeight + 300,
"top" : "0px", "left": "0px", "display": "none"
};
this._module.backGroundDiv.css(this._css.mask);
this._module.coverIframe = $("<iframe src='javascript:false' id='qihoo_effect_maskDiv_module_iframe' frameborder='1'></iframe>");
this._css.normalIframe = {
'position': 'absolute', 'left': '-1000px', 'top': '-1000px', 'z-index': 7
};
this._module.coverIframe.css(this._css.normalIframe);
this._module.coverIframe.appendTo("body");
},
showMaskDiv : function (showDivId, moduleEvent, position) {
instance = this;
if (this._isIe) {
if (document.readyState != "complete") {
setTimeout(function () {
instance.showMaskDiv(showDivId, moduleEvent, position);
}, 100);
return false;
}
}
if ("" == this._instance) { this.init(); }
this._module.showDiv = $("#" + showDivId);
if (typeof position == 'undefined') {
this._css.coverIframe = {
'position': 'absolute',
'top' : parseInt(screen.height / 4 + document.documentElement.scrollTop) + "px",
'left': parseInt(screen.width / 4 + document.documentElement.scrollLeft) + "px",
'zIndex': 7
};
this._css.coverd = {
"zIndex": 10, "position": "absolute",
"width": "400px", "height": "240px",
"top" : parseInt(screen.height / 4 + document.documentElement.scrollTop) + "px",
"left": parseInt(screen.width / 4 + document.documentElement.scrollLeft) + "px"
};
} else {
this._css.coverIframe = {
'position': 'absolute', "height": position.height, "width": position.width,
"top": position.top, "left": position.left, 'zIndex': 7
};
this._css.coverd = {
"zIndex": 10, "position": "absolute",
"height": position.height, "width": position.width,
"top": position.top, "left": position.left
};
}
for (var i in moduleEvent) {
if (typeof moduleEvent[i].idName != "undefined") {
$("#" + moduleEvent[i].idName).bind(moduleEvent[i].eventName, this._event[moduleEvent[i].doMethod]);
}
if (typeof moduleEvent[i].idObj != "undefined") {
$(moduleEvent[i].idObj).bind(moduleEvent[i].eventName, this._event[moduleEvent[i].doMethod]);
}
}
this._module.backGroundDiv.show();
showDivFront = this._module.showDiv;
cssParam = this._css;
coveredIframe = this._module.coverIframe;
this._module.backGroundDiv.animate({ opacity: 0.75 }, "normal", function () {
coveredIframe.css(cssParam.coverIframe);
showDivFront.show();
});
this._module.showDiv.css(this._css.coverd);
},
resizeWindow : function () {
this._css.mask = {
"background-color": "#000", "position": "absolute",
"-moz-opacity": "0.75", "opacity": "0.75",
"filter": "alpha(opacity=75)", "zIndex": 5,
"width" : document.body.clientWidth + 100,
"height": document.body.clientHeight + 300,
"top" : "0px", "left": "0px"
};
this._module.backGroundDiv.css(this._css.mask);
},
closeDiv : function () {
this._module.coverIframe.css(this._css.normalIframe);
this._module.showDiv.hide();
hidebBackGroundDiv = this._module.backGroundDiv;
this._module.backGroundDiv.animate({ opacity: 0 }, "normal", function () {
hidebBackGroundDiv.hide();
});
}
};
把事件綁定與位置設定拆成兩段,程式碼可讀性會好很多:
<script src="/js/jquery.js"></script>
<script src="/js/maskDiv.js"></script>
<script>
var moduleEvent = [
{ "idName": "closeForm", "eventName": "click", "doMethod": "CLOSE_DIV" },
{ "idObj" : window, "eventName": "resize", "doMethod": "RESIZE_WINDOW" }
];
</script>
<!-- 初始位置為選填步驟 -->
<script>
var position = { "width": "300px", "height": "400px", "top": "300px", "left": "720px" };
</script>
<!-- 顯示遮罩層 -->
qihoo_effect_maskDiv.showMaskDiv(divId, moduleEvent, position);
<!-- 或省略 position -->
qihoo_effect_maskDiv.showMaskDiv(divId, moduleEvent);
<!-- 隱藏遮罩層 -->
qihoo_effect_maskDiv.closeDiv();
其中 idName 是 HTML 元素的 id,idObj 是元素物件(例如 window),兩者擇一即可。
var w = Math.max(document.documentElement.scrollWidth, document.documentElement.clientWidth) + "px";
var h = Math.max(document.documentElement.scrollHeight, document.documentElement.clientHeight) + "px";
var gb = $("<div/>").attr("id", "gb")
.css({ top: "0", left: "0", zIndex: "2", position: "absolute",
filter: "alpha(opacity=0)", background: "#fff" })
.css("width", w).css("height", h);
四個關鍵點:
position: absolute 搭配 top:0; left:0,定位才會生效。filter: alpha(opacity=0) 控制透明度,範圍 0–100,0 全透明、100 全不透明;Firefox 等瀏覽器則用 opacity。background 決定遮罩顏色,通常黑色搭配 70% 左右的透明度最常見。z-index 要大於底層、小於彈出層,數字越大越上層。把這四點調對,鎖定式彈出層就成型了;剩下的都是動畫與事件細節。