js+css 實(shí)現(xiàn)遮罩居中彈出層(隨瀏覽器窗口滾動(dòng)條滾動(dòng))
更新時(shí)間:2013年12月11日 17:06:44 作者:
本文為大家詳細(xì)介紹下使用js實(shí)現(xiàn)遮罩彈出層居中,且隨瀏覽器窗口滾動(dòng)條滾動(dòng),示例代碼如下,感興趣的朋友可以參考下
js+css 實(shí)現(xiàn)遮罩居中彈出層(隨瀏覽器窗口滾動(dòng)條滾動(dòng))
<!doctype html>
<head>
<meta charset="utf-8" />
<title></title>
<style type="text/css">
*{}{margin:0;padding:0;}
html{}{_background:url(about:blank);} /**//* 阻止閃動(dòng) in IE6 , 把空文件換成about:blank , 減少請(qǐng)求 */
body{}{background:#fff; font: 12px/1.5 Tahoma,Geneva, \\5b8b\\4f53, sans-serif; height:100%;}
.wrap{}{height:980px; padding-top:20px;text-align:center;}
p{}{font-size:14px;text-align:center;line-height:24px;}
/**//** 遮罩層 **/
#masklayer{}{
background:#000;
display:none;
filter:alpha(opacity = 50);
opacity:0.5;
top:0;
left:0;
height:100%;
width:100%;
z-index:999;
position:fixed;
_position:absolute;
_left: expression(documentElement.scrollLeft + documentElement.clientWidth - this.offsetWidth);
_top: expression(documentElement.scrollTop + documentElement.clientHeight - this.offsetHeight);
}
/**//** 彈出層 **/
#popup{}{
display:none;
width:300px;
z-index:1000;
left:50%;
top:50%;
position:fixed!important;
margin-left:-150px !important;
_position:absolute;
_top:expression(eval(document.compatMode && document.compatMode=='CSS1Compat')?
documentElement.scrollTop + (document.documentElement.clientHeight - this.offsetHeight)/2: /**//*IE6*/
document.body.scrollTop + (document.body.clientHeight - this.clientHeight)/2); /**//*IE5 IE5.5*/
}
.content{}{background:#f3f3f3;border:1px solid #999;}
.content h3{}{background:#a0a0a0;color:#fff;font-size:14px;height:32px;line-height:32px; padding-left:5px;}
#clickbtn{}{margin-top:20px;}
</style>
</head>
<body>
<div class="wrap">
<p>
我是正文我是正文我是正文我是正文我是正文我是正文我是正文
<br />
我是正文我是正文我是正文我是正文我是正文我是正文我是正文
<br />
我是正文我是正文我是正文我是正文我是正文我是正文我是正文
<br />
我是正文我是正文我是正文我是正文我是正文我是正文我是正文
<br />
我是正文我是正文我是正文我是正文我是正文我是正文我是正文
</p>
<input type="button" id="clickbtn" value="clike me" />
</div>
<div id="masklayer"></div>
<div id="popup">
<div class="content">
<h3>我是彈出層 有沒(méi)有居中?</h3>
<p>居中居中居中居中居中居中</p>
<p>居中居中居中居中居中</p>
<p>居中居中居中居中</p>
<p>居中居中居中</p>
</div>
</div>
<script type="text/javascript">
(function(masklayer){
var clickbtn = document.getElementById('clickbtn');
clickbtn.onclick = function(){
var popup = document.getElementById('popup');
masklayer.style.display='block';
popup.style.display ='block';
var h = popup.clientHeight;
with(popup.style){
marginTop = -h/2+'px';
}
}
})(document.getElementById('masklayer'))
</script>
</body>
</html>
復(fù)制代碼 代碼如下:
<!doctype html>
<head>
<meta charset="utf-8" />
<title></title>
<style type="text/css">
*{}{margin:0;padding:0;}
html{}{_background:url(about:blank);} /**//* 阻止閃動(dòng) in IE6 , 把空文件換成about:blank , 減少請(qǐng)求 */
body{}{background:#fff; font: 12px/1.5 Tahoma,Geneva, \\5b8b\\4f53, sans-serif; height:100%;}
.wrap{}{height:980px; padding-top:20px;text-align:center;}
p{}{font-size:14px;text-align:center;line-height:24px;}
/**//** 遮罩層 **/
#masklayer{}{
background:#000;
display:none;
filter:alpha(opacity = 50);
opacity:0.5;
top:0;
left:0;
height:100%;
width:100%;
z-index:999;
position:fixed;
_position:absolute;
_left: expression(documentElement.scrollLeft + documentElement.clientWidth - this.offsetWidth);
_top: expression(documentElement.scrollTop + documentElement.clientHeight - this.offsetHeight);
}
/**//** 彈出層 **/
#popup{}{
display:none;
width:300px;
z-index:1000;
left:50%;
top:50%;
position:fixed!important;
margin-left:-150px !important;
_position:absolute;
_top:expression(eval(document.compatMode && document.compatMode=='CSS1Compat')?
documentElement.scrollTop + (document.documentElement.clientHeight - this.offsetHeight)/2: /**//*IE6*/
document.body.scrollTop + (document.body.clientHeight - this.clientHeight)/2); /**//*IE5 IE5.5*/
}
.content{}{background:#f3f3f3;border:1px solid #999;}
.content h3{}{background:#a0a0a0;color:#fff;font-size:14px;height:32px;line-height:32px; padding-left:5px;}
#clickbtn{}{margin-top:20px;}
</style>
</head>
<body>
<div class="wrap">
<p>
我是正文我是正文我是正文我是正文我是正文我是正文我是正文
<br />
我是正文我是正文我是正文我是正文我是正文我是正文我是正文
<br />
我是正文我是正文我是正文我是正文我是正文我是正文我是正文
<br />
我是正文我是正文我是正文我是正文我是正文我是正文我是正文
<br />
我是正文我是正文我是正文我是正文我是正文我是正文我是正文
</p>
<input type="button" id="clickbtn" value="clike me" />
</div>
<div id="masklayer"></div>
<div id="popup">
<div class="content">
<h3>我是彈出層 有沒(méi)有居中?</h3>
<p>居中居中居中居中居中居中</p>
<p>居中居中居中居中居中</p>
<p>居中居中居中居中</p>
<p>居中居中居中</p>
</div>
</div>
<script type="text/javascript">
(function(masklayer){
var clickbtn = document.getElementById('clickbtn');
clickbtn.onclick = function(){
var popup = document.getElementById('popup');
masklayer.style.display='block';
popup.style.display ='block';
var h = popup.clientHeight;
with(popup.style){
marginTop = -h/2+'px';
}
}
})(document.getElementById('masklayer'))
</script>
</body>
</html>
相關(guān)文章
JavaScript庫(kù)之vanilla-tilt使用教程(一個(gè)平滑的3D傾斜庫(kù))
vanilla-tilt.js是Javascript中一個(gè)平滑的3D傾斜庫(kù),可以讓網(wǎng)頁(yè)的一些控件變得動(dòng)態(tài)起來(lái),下面這篇文章主要給大家介紹了關(guān)于JavaScript庫(kù)之vanilla-tilt使用的相關(guān)資料,需要的朋友可以參考下2023-02-02
Javascript排序算法之計(jì)數(shù)排序的實(shí)例
計(jì)數(shù)排序是一種高效的線性排序,它通過(guò)計(jì)算一個(gè)集合中元素楚翔的次數(shù)來(lái)確定集合如何排列,計(jì)數(shù)排序不需要進(jìn)行數(shù)據(jù)的比較,所有他的運(yùn)行效率前面介紹的都高2014-04-04
TextArea設(shè)置MaxLength屬性最大輸入值的js代碼
TextArea中限制最大輸入長(zhǎng)度,實(shí)現(xiàn)的方法種種,我們不在一一介紹,今天本文推薦一種簡(jiǎn)單實(shí)用的方法,需要的朋友可以參考下2012-12-12
js獲取元素到文檔區(qū)域document的(橫向、縱向)坐標(biāo)的兩種方法
獲取頁(yè)面中元素到文檔區(qū)域document的橫向、縱向坐標(biāo),在js控制元素運(yùn)動(dòng)的過(guò)程中,對(duì)于頁(yè)面元素坐標(biāo)位置的獲取是經(jīng)常用到的,這里主要總結(jié)下兩種方法2013-05-05
javascript-hashchange事件和歷史狀態(tài)管理實(shí)例分析
這篇文章主要介紹了javascript-hashchange事件和歷史狀態(tài)管理,結(jié)合實(shí)例形式分析了javascript-hashchange基本功能、原理及歷史狀態(tài)管理相關(guān)操作技巧,需要的朋友可以參考下2020-04-04
layui數(shù)據(jù)表格實(shí)現(xiàn)重載數(shù)據(jù)表格功能(搜索功能)
這篇文章主要介紹了layui數(shù)據(jù)表格實(shí)現(xiàn)重載數(shù)據(jù)表格功能,以搜索功能為例進(jìn)行講解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
JS實(shí)現(xiàn)淘寶幻燈片效果的實(shí)現(xiàn)方法
淘寶幻燈片效果:能自動(dòng)播放,鼠標(biāo)指向或者點(diǎn)擊數(shù)字按鈕就能切換圖片。2013-03-03

