js实现登陆页面的拖拽功能

Posted 不再犯错

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js实现登陆页面的拖拽功能相关的知识,希望对你有一定的参考价值。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登陆页面的拖拽功能实现</title>
</head>
<style type="text/css">
*{
margin:0;
padding:0;
}
a{
text-decoration: none;
}
.dialog{
width: 380px;
height: auto;
position:fixed;
z-index: 1000;
border: 1px solid #d5d5d5;
background-color:#fff;
display: none;
}
.diatitle{
height: 48px;
line-height: 40px;
text-align: center;
color:#535353;
background-color: #f5f5f5;
}
.diacontent{
padding: 15px 20px;
}
.close{
font-size: 20px;
float: right;
margin-right: 20px;
}
#txt{
width: 100%;
height: 40px;
border: 1px solid #ccc;
margin-bottom: 10px;
}
#pwd{
width: 100%;
height: 40px;
border: 1px solid #ccc;
margin-bottom: 10px;
}
#btn{
display: block;
width:80px;
height: 40px;
margin:0 auto;
background-color: #CCCCCC;
border:none;
outline-style: none;
}
.mask{
background-color:#000000;
opacity: 0.4;
filter:Alpha(opacity=40);
z-index: 900;
position: fixed;
top: 0px;
left: 0px;
display: none;
}
</style>
<body style="height:1500px">
<div class="dialog" id="dialog">
<div class="diatitle" id="diatitle">
登陆页
<a href="#" class="close">&times;</a>
</div>
<div class="diacontent" id="diacontent">
<form action="fsaf" method="post">
<input type="text" name="" id="txt" placeholder="请输入账号" />
<input type="password" name="" id="pwd" placeholder="请输入密码" />
<div id="" style="text-align:right;font-size: 12px;">
<a href="#">忘记密码</a>
</div>
<input type="button" name="" id="btn" value="登陆" />
<div id="" style="text-align:right;font-size: 12px;">
<a href="#">立即注册</a>
</div>
</form>
</div>
</div>
<div class="mask">

</div>
<div id="login">
点击弹出登陆图层
</div>
</body>
<script type="text/javascript">
window.onload=function(){
var close=document.getElementsByClassName("close")[0];
var dialog=document.getElementById("dialog");
var diatitle=document.getElementById("diatitle");
var diacontent=document.getElementById("diacontent");
var mask=document.getElementsByClassName("mask")[0];
var isDraging=false; //是否可拖拽的标记
diatitle.onmousedown=down;
document.onmousemove=move;
document.onmouseup=up;
var login=document.getElementById("login");
login.onclick=function(){
dialog.style.display="block";
mask.style.display="block";
}
//自动居中-登陆浮层
close.onclick=function(){
dialog.style.display="none";
mask.style.display="none";
}

//登陆层居中
autoCenter();
function autoCenter(){
var bodyW = document.documentElement.clientWidth;
var bodyH = document.documentElement.clientHeight;
var elW = dialog.offsetWidth;
var elH = dialog.offsetHeight;
dialog.style.left = ((bodyW-elW)/2-190) + \'px\';
dialog.style.top = ((bodyH-elH)/2-80) + \'px\';
}
//遮罩层
fillToBody();
function fillToBody(){
mask.style.width = document.documentElement.clientWidth +\'px\';
mask.style.height = document.documentElement.clientHeight + \'px\';
}
按下
function down(){
diatitle.style.cursor="move";
isDraging=true;
objleft=dialog.offsetLeft;
objtop=dialog.offsetTop;
posX = parseInt(mousePosition(event).x)
posY = parseInt(mousePosition(event).y);
offsetX=posX-objleft;
offsetY=posY-objtop;
}
//移动
function move(event) {
if (isDraging == true) {
var x=mousePosition(event).x-offsetX;
var y=mousePosition(event).y-offsetY;
var w = document.documentElement.clientWidth - dialog.offsetWidth;
var h = document.documentElement.clientHeight - dialog.offsetHeight;
x=Math.min(w,Math.max(0,x));
y=Math.min(h,Math.max(0,y));
dialog.style.left = x + \'px\';
dialog.style.top = y + \'px\';
}
}
//松开
function up() {
isDraging= false;
}
function mousePosition(evt){
var xPos, yPos;
evt = evt || window.event;
if (evt.pageX) {
xPos = evt.pageX;
yPos = evt.pageY;
} else {
xPos = evt.clientX + document.body.scrollLeft - document.body.clientLeft;
yPos = evt.clientY + document.body.scrollTop - document.body.clientTop;
}
return {
x: xPos,
y: yPos
}
}
}
</script>
</html>

 

以上是关于js实现登陆页面的拖拽功能的主要内容,如果未能解决你的问题,请参考以下文章

vuejs2.0使用Sortable.js实现的拖拽功能( 转)

js实现鼠标的拖拽效果

Html5 的拖拽功能

通过 JS 实现简单的拖拽功能并且可以在特定元素上禁止拖拽

js实现鼠标的拖拽效果

js实现模态窗口的拖拽功能