一个简单的DIV弹出屏幕居中并且背景变灰

 
  
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" lang ="zh-CN" >
< head >
< title > 点击背景变暗,div弹出框 </ title >
< style >
body
{ margin : 0 ; padding : 0 ; }
</ style >
< script >
function getPosition() {
var top = document.documentElement.scrollTop;
var left = document.documentElement.scrollLeft;
var height = document.documentElement.clientHeight;
var width = document.documentElement.clientWidth;
return {top:top,left:left,height:height,width:width};
}
function showPop(){
var width = 300 ; // 弹出框的宽度
var height = 160 ; // 弹出框的高度
var obj = document.getElementById( " pop " );

obj.style.display
= " block " ;
obj.style.position
= " absolute " ;
obj.style.zindex
= " 100 " ;
obj.style.width
= width + " px " ;
obj.style.height
= height + " px " ;

document.getElementById(
" sad " ).style.backgroundColor = " #ccc " ; // div层变暗
document.getElementById( " sad " ).style.opacity = " 0.5 " ; // div层透明度为50%,在FF下
//
document.getElementById("sad").filters.alpha.opacity="50";//div层透明度为50%,在IE下

var Position = getPosition();
leftadd
= (Position.width - width) / 2;
topadd = (Position.height - height) / 2;
obj.style.top = (Position.top + topadd) + " px " ;
obj.style.left
= (Position.left + leftadd) + " px " ;

window.onscroll
= function (){
var Position = getPosition();
obj.style.top
= (Position.top + topadd) + " px " ;
obj.style.left
= (Position.left + leftadd) + " px " ;
};
}

function hidePop(){
document.getElementById(
" pop " ).style.display = " none " ;
document.getElementById(
" sad " ).style.backgroundColor = "" ;
}
</ script >
</ head >
< body >

< div id ="sad" >
< div id ="pop" style ="border:2px solid #ff0000;display:none;background-color:#FC0;" > testtest < br />< br />< br />< br />< a href ="javascript:hidePop();" > hide </ a ></ div >
< br >< br >< br >< br >< br >< br >< br >
< a href ="javascript:showPop();" > show </ a >
< br >< br >< br >< br >< br >< br >< br >< br >< br >< br >< br >< br >
< br >< br >< br >< br >< br >< br >< br >< br >< br >< br >< br >< br >< br >< br >< br >< br >< br >< br >< br >
< br >< br >< br >< br >< br >< br >< br >< br >< br >< br >< br >< br >< br >< br >< br >< br >< br >< br >< br >
< br >< br >< br >< br >< br >< br >< br >< br >< br >< br >< br >< br >< br >< br >< br >< br >< br >< br >< br >
</ div >
</ body >
</ html >

转载于:https://www.cnblogs.com/songxiii/archive/2011/03/09/1978052.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值