前天参考51js一位牛人而整理的一个效果.
因这效果现下比较适用,所以先行试练.总体上没超过原作的思想,能力有限...
看代码吧
XML/HTML代码
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>弹出窗口 遮罩 能拖动 上下左右居中</title>
- <style type="text/css">
- <!--
- /*
- Style Name:Css reseat
- Author: 爱得太迟
- Email: sika2278@163.com
- http://www.lusisi.com/
- */
- html{height:100%;}
- body {font-family:"SimSun","Arial",Helvetica,sans-serif;font-size:12px;height:100%;}
- body,div,dl, t,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form, fieldset,legend,input,button,textarea,p,blockquote,th,td {margin:0;padding:0;}
- input,button,textarea {border:none;}
- fieldset,img {border:0;}
- table {border-collapse: collapse;border-spacing:0;}
- ul,ol {list-style:none;}
- a {text-decoration:none;}
- a:hover {text-decoration:none;}
- /* 清除浮动 */
- .clear:after{content:".";display:block;height:0;clear:both;visibility:hidden;}
- #dt_1{cursor: pointer;width:40px;height:24px;line-height:24px;margin:0px auto;}
- #bodyBg{position: absolute;top: 0px;left: 0px;width: 100%;height: 100%;background-color: #aaa;z-index: 10;filter: alpha(opacity=80);opacity:0.8;}
- #maiBox{position: absolute;width: 300px;height: 150px;background-color: #fefefe;border:1px solid #f4f4f4;z-index: 20;text-indent:12px;}
- #loginBox{width: 100%;height: 24px;line-height:24px;background-color: #f4f4f4;cursor: move;}
- .aiTable{margin:10px 10px 0px;height:42px;width:242px;position:relative;}
- .aiTable li{height:24px;line-height:24px;}
- .aiTable li label{display:inline-block;width:60px;}
- .aiTable li.aisubmit{position:absolute;left:170px;top:4px;}
- .aoie{border:1px solid #f4f4f4;width:106px;}
- .aiinput{text-align:center;margin-top:10px;}
- .aiinput input{margin-bottom:6px;padding:0 1px;}
- #subLogin{height:40px;width:70px}
- .wanji{text-align:center;margin-top:12px;}
- .wanji a{display:inline-block;margin-right:6px;}
- -->
- </style>
- </head>
- <body>
- <p id="dt_1">登录</p>
- <div id="bodyBg" style="display:none;"></div>
- <div id="maiBox" style="top:-900px;left:-900px;">
- <div id="loginBox">用户登录</div>
- <form action="#" method="">
- <ul class="aiTable">
- <li><label for="username">用户名:</label><input type="text" id="username" name="username" class="aoie" /></li>
- <li><label for="password">密 码:</label><input type="password" name="password" id="password" class="aoie" /></li>
- <li class="aisubmit"><label><input type="submit" name="submit" value="登录" id="subLogin" /></label></li>
- </ul>
- </form>
- <div class="wanji"><a href="#">忘记密码</a><a href="#">注册</a></div>
- <p class="aiinput"><a id="aiclose" href="#">关闭</a></p>
- </div>
- <script type="text/javascript">
- <!--
- for(var i=1;i<=2;i++)
- {
- eval("var dt_" + i + " = document.getElementById('dt_" + i + "')");
- }
- var bodyBg = document.getElementById("bodyBg");
- var maiBox = document.getElementById("maiBox");
- var loginBox = document.getElementById("loginBox");
- var boxh = document.getElementById("maiBox").scrollHeight;
- var boxw = document.getElementById("maiBox").scrollWidth;
- var winWidth = document.documentElement.clientWidth;
- var winHeight =document.documentElement.clientHeight;
- document.getElementById("aiclose").onclick = function()
- {
- hidden("maiBox");
- hidden("bodyBg");
- }
- //document.getElementById("subLogin").onclick = function()
- // {
- // hidden("maiBox");
- // hidden("bodyBg");
- // }
- dt_1.onclick = function()
- {
- maiBox.style.top = (winHeight-boxh)/2+"px";
- maiBox.style.left = (winWidth-boxw)/2+"px";
- show("maiBox");
- show("bodyBg")
- }
- //拖动信息
- loginBox.onmousedown = Down;
- var tHeight,lWidth;
- function Down(e)
- {
- var event = window.event || e;
- tHeight = event.clientY - parseInt(maiBox.style.top.replace(/px/,""));
- lWidth = event.clientX - parseInt(maiBox.style.left.replace(/px/,""));
- document.onmousemove = Move;
- document.onmouseup = Up;
- }
- function Move(e) {
- var event = window.event || e;
- var top = event.clientY - tHeight;
- var left = event.clientX - lWidth;
- //判断 top 和 left 是否超出边界
- toptop = top < 0 ? 0 : top;
- toptop = top > document.body.offsetHeight - 150 ? document.body.offsetHeight - 150 : top;
- leftleft = left < 0 ? 0 : left;
- leftleft = left > document.body.offsetWidth - 300 ? document.body.offsetWidth - 300 : left;
- maiBox.style.top = top + "px";
- maiBox.style.left = left +"px";
- }
- function Up() {
- document.onmousemove = null;
- }
- function show(ele)
- {
- eval(ele + ".style.display = ''");
- }
- function hidden(ele)
- {
- eval(ele + ".style.display = 'none'");
- }
- //-->
- </script>
- </body>
- </html>
这里有 demo

