基于jquery的模態(tài)div層彈出效果
更新時間:2010年08月21日 11:59:42 作者:
由于項目原因,需要實現(xiàn) 點擊按鈕后,顯示友好的 loading 信息,并且為模態(tài)的。
經(jīng)過幾多周折,終于搞出來了,效果圖如下:

具體實現(xiàn):
(1) 編寫 jquery.divbox.js 的代碼:
jQuery.fn.extend(
{
OpenDiv: function()
{
var sWidth, sHeight;
sWidth = window.screen.availWidth;
if (window.screen.availHeight > document.body.scrollHeight)
{
sHeight = window.screen.availHeight;
} else
{
sHeight = document.body.scrollHeight + 20;
}
var maskObj = document.createElement("div");
maskObj.setAttribute('id', 'BigDiv');
maskObj.style.position = "absolute";
maskObj.style.top = "0";
maskObj.style.left = "0";
maskObj.style.background = "#111";
maskObj.style.filter = "Alpha(opacity=70);";
maskObj.style.opacity = "0.7";
maskObj.style.width = sWidth + "px";
maskObj.style.height = sHeight + "px";
maskObj.style.zIndex = "10000";
$("body").attr("scroll", "no");
document.body.appendChild(maskObj);
$("#BigDiv").data("divbox_selectlist", $("select:visible"));
$("select:visible").hide();
$("#BigDiv").attr("divbox_scrolltop", $.ScrollPosition().Top);
$("#BigDiv").attr("divbox_scrollleft", $.ScrollPosition().Left);
$("#BigDiv").attr("htmloverflow", $("html").css("overflow"));
$("html").css("overflow", "hidden");
window.scrollTo($("#BigDiv").attr("divbox_scrollleft"), $("#BigDiv").attr("divbox_scrolltop"));
var MyDiv_w = this.width();
var MyDiv_h = this.height();
MyDiv_w = parseInt(MyDiv_w);
MyDiv_h = parseInt(MyDiv_h);
var width = $.PageSize().Width;
var height = $.PageSize().Height;
var left = $.ScrollPosition().Left;
var top = $.ScrollPosition().Top;
var Div_topposition = top + (height / 2) - (MyDiv_h / 2);
var Div_leftposition = left + (width / 2) - (MyDiv_w / 2);
this.css("position", "absolute");
this.css("z-index", "10001");
this.css("background", "#fff");
this.css("left", Div_leftposition + "px");
this.css("top", Div_topposition + "px");
if ($.browser.mozilla)
{
this.show();
return;
}
this.fadeIn("fast");
}
, CloseDiv: function()
{
if ($.browser.mozilla)
{
this.hide();
} else
{
this.fadeOut("fast");
} $("html").css("overflow", $("#BigDiv").attr("htmloverflow"));
window.scrollTo($("#BigDiv").attr("divbox_scrollleft"), $("#BigDiv").attr("divbox_scrolltop"));
$("#BigDiv").data("divbox_selectlist").show();
$("#BigDiv").remove();
}
});
$.extend(
{
PageSize:function ()
{
var width=0;
var height=0;
width=window.innerWidth!=null?window.innerWidth:document.documentElement&&document.documentElement.clientWidth?document.documentElement.clientWidth:document.body!=null?document.body.clientWidth:null;
height=window.innerHeight!=null?window.innerHeight:document.documentElement&&document.documentElement.clientHeight?document.documentElement.clientHeight:document.body!=null?document.body.clientHeight:null;
return {Width:width,Height:height};
}
,ScrollPosition:function ()
{
var top=0,left=0;
if($.browser.mozilla)
{
top=window.pageYOffset;
left=window.pageXOffset;
}
else if($.browser.msie)
{
top=document.documentElement.scrollTop;
left=document.documentElement.scrollLeft;
}
else if(document.body)
{
top=document.body.scrollTop;
left=document.body.scrollLeft;
}
return {Top:top,Left:left};
}
});
(2) 網(wǎng)頁中需引用兩個js, jquery.divbox.js 和 jquery.js
(3) 測試頁面代碼:
<html>
<head>
<title>測試</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery.divbox.js"></script>
<style>
#divSCA
{
position: absolute;
width: 700px;
height: 500px;
font-size: 12px;
background: #fff;
border: 0px solid #000;
z-index: 10001;
display: none;
}
</style>
<script type="text/javascript">
function openDiv() {
$("#divSCA").OpenDiv();
}
function closeDiv() {
$("#divSCA").CloseDiv();
}
</script>
</head>
<body>
<div id="divSCA">
<font size="50">這是模態(tài)DIV,點擊關(guān)閉</font>
<input type="button" value="關(guān)閉" onclick="closeDiv()">
</div>
<input type="button" value="彈出" onclick="openDiv()"/>
測試能否覆蓋 select
<select>
<option>測試1</option>
<option>測試2</option>
<option>測試3</option>
</select>
</body>
</html>
(4) 測試代碼及源文件下載地址:
點擊下載

具體實現(xiàn):
(1) 編寫 jquery.divbox.js 的代碼:
復(fù)制代碼 代碼如下:
jQuery.fn.extend(
{
OpenDiv: function()
{
var sWidth, sHeight;
sWidth = window.screen.availWidth;
if (window.screen.availHeight > document.body.scrollHeight)
{
sHeight = window.screen.availHeight;
} else
{
sHeight = document.body.scrollHeight + 20;
}
var maskObj = document.createElement("div");
maskObj.setAttribute('id', 'BigDiv');
maskObj.style.position = "absolute";
maskObj.style.top = "0";
maskObj.style.left = "0";
maskObj.style.background = "#111";
maskObj.style.filter = "Alpha(opacity=70);";
maskObj.style.opacity = "0.7";
maskObj.style.width = sWidth + "px";
maskObj.style.height = sHeight + "px";
maskObj.style.zIndex = "10000";
$("body").attr("scroll", "no");
document.body.appendChild(maskObj);
$("#BigDiv").data("divbox_selectlist", $("select:visible"));
$("select:visible").hide();
$("#BigDiv").attr("divbox_scrolltop", $.ScrollPosition().Top);
$("#BigDiv").attr("divbox_scrollleft", $.ScrollPosition().Left);
$("#BigDiv").attr("htmloverflow", $("html").css("overflow"));
$("html").css("overflow", "hidden");
window.scrollTo($("#BigDiv").attr("divbox_scrollleft"), $("#BigDiv").attr("divbox_scrolltop"));
var MyDiv_w = this.width();
var MyDiv_h = this.height();
MyDiv_w = parseInt(MyDiv_w);
MyDiv_h = parseInt(MyDiv_h);
var width = $.PageSize().Width;
var height = $.PageSize().Height;
var left = $.ScrollPosition().Left;
var top = $.ScrollPosition().Top;
var Div_topposition = top + (height / 2) - (MyDiv_h / 2);
var Div_leftposition = left + (width / 2) - (MyDiv_w / 2);
this.css("position", "absolute");
this.css("z-index", "10001");
this.css("background", "#fff");
this.css("left", Div_leftposition + "px");
this.css("top", Div_topposition + "px");
if ($.browser.mozilla)
{
this.show();
return;
}
this.fadeIn("fast");
}
, CloseDiv: function()
{
if ($.browser.mozilla)
{
this.hide();
} else
{
this.fadeOut("fast");
} $("html").css("overflow", $("#BigDiv").attr("htmloverflow"));
window.scrollTo($("#BigDiv").attr("divbox_scrollleft"), $("#BigDiv").attr("divbox_scrolltop"));
$("#BigDiv").data("divbox_selectlist").show();
$("#BigDiv").remove();
}
});
$.extend(
{
PageSize:function ()
{
var width=0;
var height=0;
width=window.innerWidth!=null?window.innerWidth:document.documentElement&&document.documentElement.clientWidth?document.documentElement.clientWidth:document.body!=null?document.body.clientWidth:null;
height=window.innerHeight!=null?window.innerHeight:document.documentElement&&document.documentElement.clientHeight?document.documentElement.clientHeight:document.body!=null?document.body.clientHeight:null;
return {Width:width,Height:height};
}
,ScrollPosition:function ()
{
var top=0,left=0;
if($.browser.mozilla)
{
top=window.pageYOffset;
left=window.pageXOffset;
}
else if($.browser.msie)
{
top=document.documentElement.scrollTop;
left=document.documentElement.scrollLeft;
}
else if(document.body)
{
top=document.body.scrollTop;
left=document.body.scrollLeft;
}
return {Top:top,Left:left};
}
});
(2) 網(wǎng)頁中需引用兩個js, jquery.divbox.js 和 jquery.js
(3) 測試頁面代碼:
復(fù)制代碼 代碼如下:
<html>
<head>
<title>測試</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery.divbox.js"></script>
<style>
#divSCA
{
position: absolute;
width: 700px;
height: 500px;
font-size: 12px;
background: #fff;
border: 0px solid #000;
z-index: 10001;
display: none;
}
</style>
<script type="text/javascript">
function openDiv() {
$("#divSCA").OpenDiv();
}
function closeDiv() {
$("#divSCA").CloseDiv();
}
</script>
</head>
<body>
<div id="divSCA">
<font size="50">這是模態(tài)DIV,點擊關(guān)閉</font>
<input type="button" value="關(guān)閉" onclick="closeDiv()">
</div>
<input type="button" value="彈出" onclick="openDiv()"/>
測試能否覆蓋 select
<select>
<option>測試1</option>
<option>測試2</option>
<option>測試3</option>
</select>
</body>
</html>
(4) 測試代碼及源文件下載地址:
點擊下載
您可能感興趣的文章:
- Jquery代碼實現(xiàn)圖片輪播效果(一)
- 基于JQuery的實現(xiàn)圖片輪播效果(焦點圖)
- 用jquery實現(xiàn)下拉菜單效果的代碼
- jQuery簡單tab切換效果實現(xiàn)方法
- jquery animate 動畫效果使用說明
- jQuery動畫效果animate和scrollTop結(jié)合使用實例
- jQuery的鏈?zhǔn)秸{(diào)用淺析
- 淺析jQuery的鏈?zhǔn)秸{(diào)用之each函數(shù)
- 模仿jQuery each函數(shù)的鏈?zhǔn)秸{(diào)用
- Python實現(xiàn)類似jQuery使用中的鏈?zhǔn)秸{(diào)用的示例
- JQuery特殊效果和鏈?zhǔn)秸{(diào)用操作示例
相關(guān)文章
分享有關(guān)jQuery中animate、slide、fade等動畫的連續(xù)觸發(fā)、滯后反復(fù)執(zhí)行的bug
這篇文章主要介紹了分享有關(guān)jQuery中animate、slide、fade等動畫的連續(xù)觸發(fā)、滯后反復(fù)執(zhí)行的bug的相關(guān)資料,需要的朋友可以參考下2016-01-01jquery 鍵盤事件 keypress() keydown() keyup()用法總結(jié)
在本篇文章里小編給各位整理的是關(guān)于jquery 鍵盤事件 keypress() keydown() keyup()用法總結(jié)以及相關(guān)實例,需要的朋友們學(xué)習(xí)下。2019-10-10JQuery使用$.ajax和checkbox實現(xiàn)下次不在通知功能
這篇文章主要介紹了JQuery使用$.ajax和checkbox實現(xiàn)下次不在通知功能,本文給出HTML代碼、JS代碼、和后端JAVA代碼完整示例,需要的朋友可以參考下2015-04-04