登录页面 (带遮罩层的) ---2017-04--5

注意事项:

1、遮罩层大小、登陆框大小都是随窗体变化而变化的。

2、onclick事件的定义。

3、js创建div,移除属性,以及将内容写入。

4、登录框中的用户名和密码均是用input写的,样式不够美观,升级版待更新。。。。

 

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
*{
margin: 0px;
padding: 0px;
}
.mask{
width: 100%;
/*height: 500px; 不能定义高,因为要跟随首页大小变化而变化*/
background-color: ghostwhite;
opacity: 0.5;
z-index: 998;
position: absolute;
top: 0px;
left: 0px;
}
.login{
width: 400px;
height: 300px;
background-color: gainsboro;
position: fixed;
/*left: 200px; 通过js读取,不需要定义
top: 300px;*/
z-index:999;
}
.close-btn{
width:30px;
height:30px;
position:absolute;
top:10px;
right:10px;
background-color: gray;
text-align: center;
line-height: 30px;
}
.close-btn:hover{
cursor: pointer;}
#a1{
width: 300px;
height: 450px;
position: absolute;
}
#inner{
width: 250px;
height: 40px;
position: relative;

left: 60px;
text-align: center;
vertical-align: middle;
line-height: 40px;


}
.yonghuming{
position: relative;
margin-top: 50px;

}
</style>
</head>
<body>
<!--<input type="button" value="登录" οnclick="showLogIn()" />-->
<!--目的是换用文字,不使用input作为按钮-->
<p οnclick="showLogIn()" >登录</p> <!--给文字添加onclick事件-->

<!--以下div为弹出框中的内容,要设置其display属性为none,因为只要它们在遮罩层中显示-->
<div id="a1" style="display: none;">
<div id='inner' class='yonghuming'>
用户名:<input type="text" placeholder="用户名/手机号">
</div>
<div id='inner'class='password'>
密码:&nbsp;&nbsp;<input type="password" placeholder="请输入密码">
</div>
<div id='inner'class='denglu' style="background-color:gray;margin-top: 10px;
width:220px ;margin-left: 18px;">登录</div>
<div id='inner'class='weizhuce'><br />没有账号?-点击注册</div>
</div>


</body>
</html>
<script>
function showLogIn(){
var bodyHeight=document.body.clientHeight; // 获取内容高度
var clientHeight=document.documentElement.clientHeight;
var clientWidth=document.documentElement.clientWidth;
//创建div,即最大的遮罩层
var mask=document.createElement("div");
mask.className="mask";
mask.style.height=bodyHeight+"px";
document.body.appendChild(mask);
//点击遮罩层任意一处,退出遮罩层
mask.οnclick=function(){
mask.remove();
login.remove();
}
// 创建新div
var login=document.createElement("div");
login.className="login";
//使登陆框位于中间位置
login.style.left=clientWidth/2-200+"px";
login.style.top=clientHeight/2-150+"px";
//去掉display属性,使div能够显示
var d=document.getElementById("a1").removeAttribute("display");
// 将关闭按钮和id为a1的div写进classname为login的div中
login.innerHTML="<div class='close-btn'>X</div>"+ document.getElementById("a1").innerHTML;
document.body.appendChild(login);
//点击关闭按钮,退出遮罩层
document.getElementsByClassName("close-btn")[0].οnclick=function(){
mask.remove();
login.remove();
}
}

//当窗体大小发生变化时,让登陆框随窗体大小而改变
document.body.onresize = function(){
var clientHeight = document.documentElement.clientHeight;
var clientWidth = document.documentElement.clientWidth;
var login = document.getElementsByClassName("login")[0];
login.style.left = clientWidth/2 - 200 + "px";
login.style.top = clientHeight/2 - 150 + "px";
}
</script>

 

下面代码和上面一样,方便以后copy:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <style>
            *{
                margin: 0px;
                padding: 0px;
            }
            .mask{
                width: 100%;
            /*height: 500px;               不能定义高,因为要跟随首页大小变化而变化*/
                background-color: ghostwhite;
                opacity: 0.5;
                z-index: 998;
                position: absolute;
                top: 0px;
                left: 0px;
            }
            .login{
                width: 400px;
                height: 300px;
                background-color: gainsboro;
                position: fixed;
                /*left: 200px;             通过js读取,不需要定义   
                top: 300px;*/
                z-index:999;
            }
            .close-btn{
                width:30px;
                height:30px;
                position:absolute;
                top:10px;
                right:10px;
                background-color: gray;
                text-align: center;
                line-height: 30px;
            }
            .close-btn:hover{
                cursor: pointer;}
                #a1{
                    width: 300px;
                    height: 450px;
                    position: absolute;
                }
            #inner{
                width: 250px;
                height: 40px;
                position: relative;
                
                left: 60px;
                text-align: center;
                vertical-align: middle;
                line-height: 40px;
                
                
            }
            .yonghuming{
                position: relative;
                margin-top: 50px;
                
            }
        </style>
    </head>
    <body>
        <!--<input type="button" value="登录" οnclick="showLogIn()" />-->  
        <!--目的是换用文字,不使用input作为按钮-->
        <p οnclick="showLogIn()" >登录</p>          <!--给文字添加onclick事件-->
        
        <!--以下div为弹出框中的内容,要设置其display属性为none,因为只要它们在遮罩层中显示-->
        <div id="a1" style="display: none;">
            <div id='inner' class='yonghuming'> 
            用户名:<input type="text" placeholder="用户名/手机号">
            </div>
            <div id='inner'class='password'>
                密码:&nbsp;&nbsp;<input type="password" placeholder="请输入密码">
            </div>
            <div id='inner'class='denglu' style="background-color:gray;margin-top: 10px;
                width:220px ;margin-left: 18px;">登录</div>
            <div id='inner'class='weizhuce'><br />没有账号?-点击注册</div>
        </div>    
        
        
    </body>
</html>
<script>
    function showLogIn(){
        var bodyHeight=document.body.clientHeight;       // 获取内容高度
        var clientHeight=document.documentElement.clientHeight;
        var clientWidth=document.documentElement.clientWidth;
 //创建div,即最大的遮罩层    
        var mask=document.createElement("div");     
        mask.className="mask";
        mask.style.height=bodyHeight+"px";
            document.body.appendChild(mask);
//点击遮罩层任意一处,退出遮罩层
        mask.οnclick=function(){
            mask.remove();
            login.remove();
        }
//    创建新div
        var login=document.createElement("div");
        login.className="login";
          //使登陆框位于中间位置
        login.style.left=clientWidth/2-200+"px";      
        login.style.top=clientHeight/2-150+"px";  
//去掉display属性,使div能够显示
        var d=document.getElementById("a1").removeAttribute("display");  
//        将关闭按钮和id为a1的div写进classname为login的div中
        login.innerHTML="<div class='close-btn'>X</div>"+ document.getElementById("a1").innerHTML;
        document.body.appendChild(login);
//点击关闭按钮,退出遮罩层
        document.getElementsByClassName("close-btn")[0].οnclick=function(){
            mask.remove();
            login.remove();
        }
    }
    
//当窗体大小发生变化时,让登陆框随窗体大小而改变
    document.body.onresize = function(){
        var clientHeight = document.documentElement.clientHeight;
        var clientWidth = document.documentElement.clientWidth;
        var login = document.getElementsByClassName("login")[0];
        login.style.left = clientWidth/2 - 200 + "px";
        login.style.top = clientHeight/2 - 150 + "px";
    }
</script>

 

转载于:https://www.cnblogs.com/chenguanai/p/6669397.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值