一个背景变暗的div可拖动提示窗口,兼容IE、Firefox、Opera

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>无标题文档</title>
  6. <script language="javascript">
  7. function alertWin(title, msg, w, h){ 
  8.   var titleheight = "22px"// 提示窗口标题高度 
  9.   var bordercolor = "#666699"// 提示窗口的边框颜色 
  10.   var titlecolor = "#FFFFFF"// 提示窗口的标题颜色 
  11.   var titlebgcolor = "#666699"// 提示窗口的标题背景色
  12.   var bgcolor = "#FFFFFF"// 提示内容的背景色
  13.   
  14.   var iWidth = document.documentElement.clientWidth; 
  15.   var iHeight = document.documentElement.clientHeight; 
  16.   var bgObj = document.createElement("div"); 
  17.   bgObj.style.cssText = "position:absolute;left:0px;top:0px;width:"+iWidth+"px;height:"+Math.max(document.body.clientHeight, iHeight)+"px;filter:Alpha(Opacity=30);opacity:0.3;background-color:#000000;z-index:101;";
  18.   document.body.appendChild(bgObj); 
  19.   
  20.   var msgObj=document.createElement("div");
  21.   msgObj.style.cssText = "position:absolute;font:11px '宋体';top:"+(iHeight-h)/2+"px;left:"+(iWidth-w)/2+"px;width:"+w+"px;height:"+h+"px;text-align:center;border:1px solid "+bordercolor+";background-color:"+bgcolor+";padding:1px;line-height:22px;z-index:102;";
  22.   document.body.appendChild(msgObj);
  23.   
  24.   var table = document.createElement("table");
  25.   msgObj.appendChild(table);
  26.   table.style.cssText = "margin:0px;border:0px;padding:0px;";
  27.   table.cellSpacing = 0;
  28.   var tr = table.insertRow(-1);
  29.   var titleBar = tr.insertCell(-1);
  30.   titleBar.style.cssText = "width:100%;height:"+titleheight+"px;text-align:left;padding:3px;margin:0px;font:bold 13px '宋体';color:"+titlecolor+";border:1px solid " + bordercolor + ";cursor:move;background-color:" + titlebgcolor;
  31.   titleBar.style.paddingLeft = "10px";
  32.   titleBar.innerHTML = title;
  33.   var moveX = 0;
  34.   var moveY = 0;
  35.   var moveTop = 0;
  36.   var moveLeft = 0;
  37.   var moveable = false;
  38.   var docMouseMoveEvent = document.onmousemove;
  39.   var docMouseUpEvent = document.onmouseup;
  40.   titleBar.onmousedown = function() {
  41.     var evt = getEvent();
  42.     moveable = true
  43.     moveX = evt.clientX;
  44.     moveY = evt.clientY;
  45.     moveTop = parseInt(msgObj.style.top);
  46.     moveLeft = parseInt(msgObj.style.left);
  47.     
  48.     document.onmousemove = function() {
  49.       if (moveable) {
  50.         var evt = getEvent();
  51.         var x = moveLeft + evt.clientX - moveX;
  52.         var y = moveTop + evt.clientY - moveY;
  53.         if ( x > 0 &&( x + w < iWidth) && y > 0 && (y + h < iHeight) ) {
  54.           msgObj.style.left = x + "px";
  55.           msgObj.style.top = y + "px";
  56.         }
  57.       }  
  58.     };
  59.     document.onmouseup = function () { 
  60.       if (moveable) { 
  61.         document.onmousemove = docMouseMoveEvent;
  62.         document.onmouseup = docMouseUpEvent;
  63.         moveable = false
  64.         moveX = 0;
  65.         moveY = 0;
  66.         moveTop = 0;
  67.         moveLeft = 0;
  68.       } 
  69.     };
  70.   }
  71.   
  72.   var closeBtn = tr.insertCell(-1);
  73.   closeBtn.style.cssText = "cursor:pointer; padding:2px;background-color:" + titlebgcolor;
  74.   closeBtn.innerHTML = "<span style='font-size:15pt; color:"+titlecolor+";'>×</span>";
  75.   closeBtn.onclick = function(){ 
  76.     document.body.removeChild(bgObj); 
  77.     document.body.removeChild(msgObj); 
  78.   } 
  79.   var msgBox = table.insertRow(-1).insertCell(-1);
  80.   msgBox.style.cssText = "font:10pt '宋体';";
  81.   msgBox.colSpan  = 2;
  82.   msgBox.innerHTML = msg;
  83.   
  84.     // 获得事件Event对象,用于兼容IE和FireFox
  85.     function getEvent() {
  86.       return window.event || arguments.callee.caller.arguments[0];
  87.     }
  88. </script>
  89. </head>
  90. <body>
  91. <input type="button" value="点这里" οnclick="alertWin('标题','这里是内容',300,200);" />
  92. </body>
  93. </html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值