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>

原文地址:https://www.cnblogs.com/WangYujie1994/p/10266029.html

时间: 2024-07-30 13:01:38

javascript--自定义弹出登陆窗口(弹出窗)的相关文章

帝国CMS弹出登录窗口实现方法

帝国CMS弹出登录窗口实现方法 看到好多网站都用弹出登陆窗口让用户登陆注册,其实就是用JS调用一个DIV层实现的 今天我用帝国CMS具体讲一下怎么实现这个效果: 一.打开帝国CMS后台-公共模板-JS讲用登陆模板 把附件里的调用登陆模板代码复制进去-修改 二.在你的首页加入CSS样式和JS代码 1.CSS: <style> #lggoodBox{ margin:0 auto; padding:0px; text-align:left; width:370px; height:220px; ba

Jquery+div+css实现弹出登录窗口

基本思路先隐藏(dispaly:none)再显示,半透明蒙版层通过 z-index:9998; z-index:9999; 值越大越在前面 index.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http:

页面点击Button按钮弹出登陆注册框(含短信验证功能)

1 <div class="login-hidd"></div> 2 <div class="login-wrap"> 3 <div class="login-cont"> 4 <img id="login-img-close" src="/views/image/close08.png" alt="登陆" title="&

点击Button弹出登陆注册框

1 <div class="login-hidd"></div> 2 <div class="login-wrap"> 3 <div class="login-cont"> 4 <div id="qiehuan"> 5 <img id="login-img-close" src="image/close08.png" al

javascript实现弹出登陆框效果

先来看看效果吧 初始情况下 2.点击登陆按钮  3.点击close按钮,或者单击灰色区域后,登陆窗口会消失. html结构 <body> <span class="btn" id="BTN">login</span> </body> css样式 *{ margin: 0; padding: 0; } .btn{ position: absolute; top: 50px; right: 100px; display:

JavaScript动态实现div窗口弹出&amp;消失功能

先积累一个JavaScript动态实现div窗口弹出&消失功能 首先是index.jsp代码 <html> <head> <link rel="stylesheet" href="css/DivStyle.css" media="screen"> <script type="text/javascript" src="scripts/div.js">&

JavaScript特效实例007-为弹出的窗口加入关闭按钮

实例007                        为弹出的窗口加入关闭按钮 实例说明 在弹出的窗口中加入关闭按钮,点击后弹出的窗口关闭. 技术要点 本实例主要应用window对象的close()方法实现.close()方法的语法如下. window.close() 功能:window对象的close()方法用于自动关闭浏览器窗口. 实现过程 (1)要弹出的窗口且是功能实现界面New.html. <html> <head> <meta charset="utf

JavaScript特效实例008-关闭弹出的窗口时,刷新父窗口

实例008                  关闭弹出的窗口时,刷新父窗口 实例说明 关闭弹出的窗口时,同时刷新父窗口,一般用来使父窗口获取最新的数据. 技术要点 本实例主要应用window.open()语句打开新窗口,并在新窗口中应用opener属性,该属性返回一个引用,用于指定打开本窗口的窗口对象. 语法: window.opener window.opener.方法 window.opener.属性 功能:返回的是一个窗口对象.opener属性与打开该窗口的父窗口相联系,当访问子窗口中op

Android的弹出登陆框的实现

最近在做一个项目,要用到登陆框,几经波折,最后用的是直接将Activity的Theme属性设置成Dialog,然后达到了我想要的效果. 下面是我的实现经历: 1.首先,我是直接使用AlertDialog来实现,确定是,形状有点难看,而且获得Dialog里面的控件略显麻烦(因为我要做的登陆框有一定的布局),然后就给我就放弃了,可能因为我太水了,不能很好的使用它 2.然后我就使用PopupWindow来实现,界面是达到了我的要求,控件的获得通过Inflater就可以获得了相对较简单,但是有一个缺点就