javascript 彈出層置中效果的製作_javascript技巧

來源:互聯網
上載者:User
問題:做一個帶拖動的彈出層效果(像提示框那種)
先寫了一半,明天繼續奮鬥:
<!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> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <meta name="author" content="Stri" /> <title>javascript 彈出層置中效果的製作</title> <style type="text/css"> * { padding:0; margin:0; list-style:none; } body { font-family:Verdana, Geneva, sans-serif; font-size:14px; } #a { width:300px; height:80px; border:5px solid #d3d3d3; background-color:#f7f7f7; position:absolute; padding:10px; } a.btn { display:block; width:80px; cursor:pointer; background-color:#f7f7f7; padding:2px 0px; border:1px solid #d5d5d5; vertical-align:middle; text-align:center; margin:50px auto 5px; text-decoration:none; } a:hover { background-color:#333; color:#fff; font-weight:bold; } </style> <script language="javascript" type="text/javascript"> window.onload=function divcenter() { var c=document.createElement("div"); document.body.appendChild(c); c.setAttribute("id","a"); var divId=document.getElementById("a"); divId.innerHTML+="對瀏覽器變形,重新整理看是否置中"; divId.style.left=(document.documentElement.clientWidth-divId.clientWidth)/2+document.documentElement.scrollLeft+"px"; setTimeout(function(){divId.style.top=(document.documentElement.clientHeight-divId.clientHeight)/2+document.documentElement.scrollTop-50+"px";},200); } </script> </head> <body> <script type="text/javascript"> x:for(var i=1; i<100;i++){ var a=document.createElement("div"); a.innerHTML=""+i+""; document.body.appendChild(a);} </script> </body> </html>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

點擊右下角的"OPEN"
<!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> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <meta name="author" content="Stri" /> <title>javascript 彈出層置中效果的製作</title> <style type="text/css"> * { padding:0; margin:0; list-style:none; } body { font-family:Verdana, Geneva, sans-serif; font-size:14px; } #a { width:380px; height:80px; border:5px solid #d3d3d3; background-color:#f7f7f7; position:absolute; padding:10px; z-index:9999; } a.btn { display:block; width:80px; cursor:pointer; background-color:#f7f7f7; padding:2px 0px; border:1px solid #d5d5d5; vertical-align:middle; text-align:center; margin:50px auto 5px; text-decoration:none; } a:hover { background-color:#333; color:#fff; font-weight:bold; } #aa { position:fixed; bottom:20px; right:10px; } </style> <script language="javascript" type="text/javascript"> var drg=true; var ns=(document.all) ? false : true; function divcenter() {remove(); var c=document.createElement("div"); document.body.appendChild(c); c.setAttribute("id","a"); c.setAttribute("dragable","dragable"); c.onmousedown=foo; var divId=document.getElementById("a"); divId.innerHTML+="出現代碼異常!<br>錯誤編號:0015;"; divId.style.left=(document.documentElement.clientWidth-divId.clientWidth)/2+document.documentElement.scrollLeft+"px"; setTimeout(function(){divId.style.top=(document.documentElement.clientHeight-divId.clientHeight)/2+document.documentElement.scrollTop-50+"px";},100); } //移除 function remove(){ if(document.getElementById("a")) {document.body.removeChild(document.getElementById("a")); }} function foo(){ if(drg){ if(document.getElementById("a")!=null) { obj=document.getElementById("a"); document.onmousedown=m_d; document.onmouseup=m_u; } } else { document.onmousemove=no_f; document.onmousedown=no_f; document.onmouseup=no_f; } } function m_d(e){ document.onmousemove=m_m; x=(ns)?e.pageX:event.x; y=(ns)?e.pageY:event.y; cx=obj.offsetLeft-x; cy=obj.offsetTop-y; } function m_m(e){ drg=false; x=(ns)?e.pageX:event.x; y=(ns)?e.pageY:event.y; obj.style.left=(cx+x)+"px"; obj.style.top=(cy+y)+"px"; } function m_u(){ drg=true; document.onmousedown=no_f; document.onmousemove=no_f; document.onmouseup=no_f; } function no_f(){ return false;} </script> </head> <body> open <script type="text/javascript"> x:for(var i=1; i<100;i++){ var a=document.createElement("div"); a.innerHTML=""+i+""; document.body.appendChild(a);} </script> </body> </html>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

關於每一步的理解,最後解釋.畢竟代碼還沒完全整理
被從中間擴充緩衝的效果演算法難住了!
*****************OK,也不算難****************
就是讓div的left和top屬性隨著width一起漸層就行了
<!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> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <meta name="author" content="Stri" /> <title>javascript 彈出層置中效果的製作</title> <style type="text/css"> * { padding:0; margin:0; list-style:none; } body { font-family:Verdana, Geneva, sans-serif; font-size:14px; } #a { width:380px; height:80px; border:5px solid #d3d3d3; background-color:#f7f7f7; position:absolute; padding:10px; z-index:9999; } a.btn { display:block; width:80px; cursor:pointer; background-color:#f7f7f7; padding:2px 0px; border:1px solid #d5d5d5; vertical-align:middle; text-align:center; margin:50px auto 5px; text-decoration:none; } a:hover { background-color:#333; color:#fff; font-weight:bold; } #aa { position:fixed; bottom:20px; right:10px; } </style> <script language="javascript" type="text/javascript"> var drg=true; var ns=(document.all) ? false : true; function divcenter() {remove(); var c=document.createElement("div"); document.body.appendChild(c); c.setAttribute("id","a"); c.setAttribute("dragable","dragable"); c.onmousedown=foo; var divId=document.getElementById("a"); divId.innerHTML+="出現代碼異常!<br>錯誤編號:0015;"; //divId.style.left=(document.documentElement.clientWidth-divId.clientWidth)/2+document.documentElement.scrollLeft+"px"; //setTimeout(function(){divId.style.top=(document.documentElement.clientHeight-divId.clientHeight)/2+document.documentElement.scrollTop-50+"px";},100); h_w(300,80); } //移除 function remove(){ if(document.getElementById("a")) {document.body.removeChild(document.getElementById("a")); }} function foo(){ if(drg){ if(document.getElementById("a")!=null) { obj=document.getElementById("a"); document.onmousedown=m_d; document.onmouseup=m_u; } } else { document.onmousemove=no_f; document.onmousedown=no_f; document.onmouseup=no_f; } } function m_d(e){ document.onmousemove=m_m; x=(ns)?e.pageX:event.x; y=(ns)?e.pageY:event.y; cx=obj.offsetLeft-x; cy=obj.offsetTop-y; } function m_m(e){ drg=false; x=(ns)?e.pageX:event.x; y=(ns)?e.pageY:event.y; obj.style.left=(cx+x)+"px"; obj.style.top=(cy+y)+"px"; } function m_u(){ drg=true; document.onmousedown=no_f; document.onmousemove=no_f; document.onmouseup=no_f; } function no_f(){ return false;} function h_w(a,b){ divId=document.getElementById("a"); divId.style.width="0px"; divId.style.height="0px"; var s=function(){ var w=parseInt(divId.style.width); if(w<a){ divId.style.width=w+Math.ceil((a-w)/20)+"px"; divId.style.left=(document.documentElement.clientWidth-divId.clientWidth)/2+document.documentElement.scrollLeft+"px";} else {clearInterval(st); divId.style.width=a+"px";} } var t=function(){ var h=parseInt(divId.style.height); if(h<b){ divId.style.height=h+Math.ceil((b-h)/10)+"px"; divId.style.top=(document.documentElement.clientHeight-divId.clientHeight)/2+document.documentElement.scrollTop-10+"px";} else {clearInterval(tt); divId.style.height=b+"px";} } var st = window.setInterval(s,1); var tt=window.setInterval(t,1); } </script> </head> <body> 提交 <script type="text/javascript"> x:for(var i=1; i<100;i++){ var a=document.createElement("div"); a.innerHTML=""+i+""; document.body.appendChild(a);} </script> </body> </html>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.