简单图层效果

来源:互联网 发布:linux socket编程教程 编辑:程序博客网 时间:2024/04/30 09:58
<!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">
 <head>
 <style type="text/css">
 body {
margin:0;
padding:0;
 }
 </style>
 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
 <title>弹层效果</title>
     <script language="javascript">
         function msgbox(){ //背景图层的生成块
             var msg = document.createElement("div");
             msg.id = "layer";
             //msg.innerHTML = "<a href='#'> oh my gold is very good!</a>";
             with (msg.style){
                 width = "100%";
                 height = document.documentElement.clientHeight + "px";
                 background = "#E9E9E9";
                 position = "absolute";
                 left = "0";
                 top = "0";  
                 filter = 'Alpha(opacity=70)';//设置透明度
                 opacity = '0.7';//不透明度
             }
             document.body.appendChild(msg);
 
//以下是图片图层的运用
var message = document.createElement("div");
message.id = "messgebox";
message.innerHTML = "<img src='baidu_sylogo1.gif'  onclick='Hlose()'/>";//不能用小写的close()否则将不识别  
// onmousedown='MouseDown(this)' onmousemove='MouseMove()' onmouseup='MouseUp()'
with (message.style){
position = "absolute";
left = "37%";
top = "30%";
width = "500px";
height = "500px";
}
document.body.appendChild(message);//添加图层 div;
}
         function Hlose(){//清楚添加的两个div
             document.body.removeChild(document.getElementById("layer"));
             document.body.removeChild(document.getElementById("messgebox"));
         }


         //以下代码是图层的移动 方法
         var Obj;
         function MouseDown(obj){
             Obj = obj;
             Obj.setCapture();
             Obj.l = event.x - Obj.style.pixelLeft;
             Obj.t = event.y - Obj.style.pixelTop;
         }
         function MouseMove() {
             if (Obj != null) {
                 Obj.style.left = event.x - Obj.l;
                 Obj.style.top = event.y - Obj.t;
             }
         }
         function MouseUp() {
             if (Obj != null) {
                 Obj.releaseCapture();
                 Obj = null;
             }
         }
         //禁止键盘按F5刷新
         document.onkeydown = function () {
             if (event.keyCode == 116) {
                 event.keyCode = 0;
                 event.returnValue = false;
             }
         }


     </script>
 </head>


<body>
 <div style="text-align:center;margin-top:20%;">
 <input type="button" value="显示" onclick="msgbox();" />
 </div>
 </body>
 </html>