javascript--自定義彈出登陸窗口(彈出窗)


web開發中瀏覽器對象封裝了諸如prompt、alert、confirm等彈出框,但是有的彈出框並不能滿足開發需要,需要我們自己定義彈出框,諸如用戶登陸框、消息提示框等。本文利用彈出用戶登陸框示例,對這部分知識做個小結。

示例需求:點擊按鈕,彈出登陸窗口,且該窗口可以拖拽,彈出窗口的同時,整個頁面變成灰色半透明。

效果圖如下:圖1是起始頁面,圖2是點擊“點擊,彈出登陸框”按鈕后頁面,圖3是登陸框自由拖動后頁面。

                                       

                            圖1                        圖2                       圖3

分析

1.讓整個頁面變成灰色半透明,需要使用div對整個屏幕進行覆蓋,這個div稱為覆蓋層。

2.點擊按鈕的時候,彈出登陸窗口和覆蓋層,注意,登陸窗口的z-index應該最高。

3.點擊關閉按鈕的時候,隱藏登陸窗口和覆蓋層。

4.實現登陸窗口的拖拽。(該功能在上節博文中有詳細講解)

重點關注:

①登陸窗口的position為absolute,牢記怎么讓定位屬性的盒子居中,這個需要用到數學知識,設置left:50%,然后給margin-left設置為盒子寬度一般的負數。以后在HTML+CSS標簽博文中需要重點標記。

②盒子拖拽中,用到的事件對象的相關屬性的瀏覽器兼容性問題。

③重點復習一下相對定位和絕對定位。

代碼如下

  1 <!DOCTYPE html>
  2 <html>
  3     <head>
  4         <meta charset="utf-8">
  5         <title>自定義登陸窗口</title>
  6         <style type="text/css">
  7             *{
  8                 margin: 0px;
  9                 padding: 0px;
 10             }
 11             /* 彈出登陸框按鈕 */
 12             #login-header{
 13                 text-align: center;
 14                 height: 30px;
 15                 line-height: 30px;
 16             }
 17             #login-header a{
 18                 font-size: 24px;
 19                 text-decoration: none;
 20                 color: black;
 21             }
 22             
 23             /* 登陸框主體 */
 24             .login{
 25                 position: absolute;
 26                 width: 512px;
 27                 height: 284px;
 28                 z-index: 9999;
 29                 display: none;
 30                 background-color: white;
 31                 /* 這里要注意絕對定位的盒子怎么在屏幕顯示居中 */
 32                 left: 50%;
 33                 margin-left: -256px;
 34                 margin-top: 142px;
 35                 border: 1px solid gray;
 36             }
 37             /* 登陸框標題 */
 38             .login-title{
 39                 width: 100%;
 40                 height: 40px;
 41                 line-height: 40px;
 42                 text-align: center;
 43                 margin-bottom: 20px;
 44                 cursor: move;
 45             }
 46             .login-title span a{
 47                 text-decoration: none;
 48                 border: 1px solid gray;
 49                 font-size: 12px;
 50                 color: black;
 51                 border-radius: 20px;
 52                 width: 40px;
 53                 height: 40px;
 54                 background-color: #fff;
 55                 position: absolute;
 56                 top: -20px;
 57                 right: -20px;
 58             }
 59             
 60             /* 登陸表單 */
 61             .login-input{
 62                 margin: 20px 0px 30px 0px;
 63             }
 64             .login-input label{
 65                 float: left;
 66                 height: 35px;
 67                 line-height: 35px;
 68                 width: 90px;
 69                 padding-left: 10px;
 70                 text-align: right;
 71                 font-size: 14px;
 72             }
 73             .login-input input.list-input{
 74                 height: 35px;
 75                 line-height: 35px;
 76                 width: 350px;
 77                 text-indent: 5px;
 78             }
 79             /* 登陸框登陸按鈕 */
 80             .loginSubmit{
 81                 width: 260px;
 82                 height: 40px;
 83                 text-align: center;
 84                 border: 1px solid gray;
 85                 background-color: white;
 86                 margin-left: 120px;
 87                 
 88             }
 89             
 90             /* 遮蓋層 */
 91             .bg{
 92                 background-color: #000;
 93                 width: 100%;
 94                 height: 100%;
 95                 top: 0px;
 96                 position: fixed;
 97                 opacity: 0.3;
 98                 -webkit-opacity: 0.3;
 99                 -moz-opacity: 0.3;
100                 display: none;
101             }
102         </style>
103     </head>
104     <body>
105         <!-- 彈出登陸框按鈕 -->
106         <div id="login-header">
107             <a id="adminBtn" href="javascript:void(0)">點擊,彈出登陸框</a>
108         </div>
109         
110         <!-- 登陸框主體 -->
111         <div id="login" class="login">
112             <!-- 登陸框標題 -->
113             <div id="login-title" class="login-title">
114                 登陸會員  
115                 <span><a id="closeBtn" href="javascript:void(0)">關閉</a></span>
116             </div>
117             <!-- 登陸框表單 -->
118             <div id="login-form">
119                 <div class="login-input">
120                     <label>登錄名:</label>
121                     <input type="text" placeholder="請輸入登錄名" class="list-input"/>
122                 </div>
123                 
124                 <div class="login-input">
125                     <label>密&nbsp;&nbsp;&nbsp;碼:</label>
126                     <input type="password" placeholder="請輸入密碼" class="list-input"/>
127                 </div>
128             </div>
129             <!-- 登陸框登陸按鈕 -->
130             <input type="submit"  id="loginSubmit" value="登陸會員" class="loginSubmit"/>
131         </div>
132         
133         <!-- 遮蓋層 -->
134         <div id="bg" class="bg">sada</div>
135         
136         
137         <!-- 插入JS代碼 -->
138         <script type="text/javascript">
139             var login=document.getElementById('login');
140             var bg=document.getElementById('bg');
141             // 1.點擊"點擊,彈出登陸框",彈出登陸窗口和遮蓋層
142             var adminBtn=document.getElementById('adminBtn');
143             adminBtn.onclick=function(){
144                 login.style.display="block";
145                 bg.style.display="block";
146                 return false;
147             }
148             // 2.點擊"關閉",隱藏登陸窗口和遮蓋層
149             var closeBtn=document.getElementById('closeBtn');
150             closeBtn.onclick=function(){
151                 login.style.display="none";
152                 bg.style.display="none";
153                 return false;
154             }
155             // 3.鼠標拖拽功能
156             var login_title=document.getElementById('login-title');
157             login_title.onmousedown=function(e){
158                 e = e || window.event;
159                 var x=e.pageX || e.clientX +(document.body.scrollLeft || document.documentElement.scrollLeft);
160                 var y=e.pageY || e.clientY +(document.body.scrollTop || document.documentElement.scrollTop);
161                 
162                 var boxX=login.offsetLeft;
163                 var boxY=login.offsetTop;
164                 
165                 var mouse_in_boxX=x-boxX;
166                 var mouse_in_boxY=y-boxY;
167                 
168                 document.onmousemove=function(e){
169                     var x=e.pageX || e.clientX +(document.body.scrollLeft || document.documentElement.scrollLeft);
170                     var y=e.pageY || e.clientY +(document.body.scrollTop || document.documentElement.scrollTop);
171                     
172                     login.style.left=x-mouse_in_boxX+256+'px';
173                     login.style.top=y-mouse_in_boxY-142+'px';
174                 }
175             }
176             
177             login_title.onmouseup = function(){
178                 document.onmousemove=null;
179             }
180             
181         </script>
182     </body>
183 </html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM