jquery 鎖定彈出層實現代碼

來源:互聯網
上載者:User

基於jquery的鎖定彈出層
這個東西也是隨手總結出來的,引用了一些js架構jquery的方法。
div遮蓋其他控制項的方法參考了 對於需要遮蓋flash的,請將flash控制項的WMode變數值設定為Transparent
使用方法: 複製代碼 代碼如下:<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。
*******************************************************
effect/maskDiv.js 複製代碼 代碼如下: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();});
}
};

看評論所言,加了一點用法:

這裡是一個不引入jquery架構的實現同樣效果的程式。

引入檔案
<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>
註:idName是綁定事件的html組件的id,idObj是html控制項變數
配置初始位置(可選步驟)
<script>
var position = {"width":"300px", "height":"400px" , "top":"300px", "left":"720px"};
</script> 顯示遮罩層
qihoo_effect_maskDiv.showMaskDiv(divId, moduleEvent , position);或者qihoo_effect_maskDiv.showMaskDiv(divId, moduleEvent );
隱去遮罩層
qihoo_effect_maskDiv.closeDiv();

js實現的遮蓋層的定義:
經常遇到要彈出一個懸浮層,滑鼠的事件只能在本層上有效,底層會失效。能用的做 法是在懸浮層和底層之間在加一個遮蓋層,遮蓋住整個瀏覽器,這樣就不能點擊底層的任何東西了。 複製代碼 代碼如下: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)

其實說明最主要還是說明一下背景層的CSS定義。首先要使top和left有效,就要設定 position:absolute。filter:"alpha(opacity=0)設定透明度,數值0-100,0表示完全透明,100表示不透明。background設定背景層的顏色。zIndex的值只要比底層的高,比彈出層的低就行了。(zIndex的值越大表示越在上層)。好了,基本就是這樣吧!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.