欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

js实现登录框鼠标拖拽效果

程序员文章站 2022-05-12 23:39:17
效果图: 代码如下:

效果图:

js实现登录框鼠标拖拽效果

代码如下:

<!doctype html>
<html lang="en">
<head>
 <meta charset="utf-8">
 <title>登录框鼠标拖拽效果</title>
 <style type="text/css">
 body {
  background: url("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1488778794834&di=e97c96dfe7860297d1968c30adc862a2&imgtype=0&src=http%3a%2f%2fpic1.5442.com%3a82%2f2015%2f0409%2f01%2f15.jpg%2521960.jpg") no-repeat top center #ffffff;
  background-size: 100%;
  padding: 0;
  margin: 0;
  font-size: 12px;
  font-family: "微软雅黑", sans-serif;
 }
 .ui-dialog {
  width: 380px;
  position: absolute;
  z-index: 9000;
  top: 100px;
  left: 100px;
  border: 1px solid #d5d5d5;
  background-color: #ffffff;
  /*display: none;*/
 }
 .ui-dialog-title {
  height: 48px;
  line-height: 48px;
  padding-left: 20px;
  color: #535353;
  font-size: 16px;
  background-color: #f5f5f5;
  border-bottom: 1px solid #efefef;
  cursor: move;
 }
 .ui-dialog-title-closebutton {
  width: 16px;
  height: 16px;
  display: inline-block;
  position: absolute;
  right: 20px;
  color: #000;
  text-decoration: unset;
 }
 .ui-dialog-title-closebutton:hover {
  color: #4ca8ff;
 }
 .ui-dialog-content {
  padding: 15px 20px;
 }
 .ui-dialog-pt15 {
  padding-top: 15px;
 }
 .ui-dialog-l40 {
  height: 40px;
  line-height: 40px;
  text-align: right;
 }
 .ui-dialog-input {
  width: 100%;
  height: 40px;
  margin: 0;
  padding: 0;
  border:1px solid #d5d5d5;
  font-size: 16px;
  color: #c1c1c1;
  text-indent: 25px;
  outline: none;
 }
 .ui-dialog-input-username {
  background: url("images/input_username.png") no-repeat 2px;
 }
 .ui-dialog-input-password {
  background: url("images/input_password.png") no-repeat 2px;
 }
 .ui-dialog-submit {
  width: 100%;
  height: 50px;
  background: #3b7ae3;
  border: none;
  font-size: 16px;
  color: #ffffff;
  outline: none;
  text-decoration: none;
  display: block;
  text-align: center;
  line-height: 50px;
 }
 .ui-dialog-submit:hover {
  background: #3f81b0;
 }
 .ui-mask {
  width: 100%;
  height: 100%;
  background: #000;
  opacity: 0.4;
  position: absolute;
  top: 0;
  left: 0;
  z-index: 8000;
  display: none;
 }
 .link {
  text-align: right;
  line-height: 20px;
  padding-right: 40px;
 }
 </style>
</head>
<body>
 <div class="ui-dialog" id="dialog">
 <div class="ui-dialog-title" id="dialogtitle">
  登录
  <!-- 右上角的关闭按钮 -->
  <a href="javascript:hidedialog();" rel="external nofollow" class="ui-dialog-title-closebutton">x</a>
 </div>
 <div class="ui-dialog-content">
  <div class="ui-dialog-l40 ui-dialog-pt15">
  <input class="ui-dialog-input ui-dialog-input-username" type="input" placeholder="手机/邮箱/用户名" />
  </div>
  <div class="ui-dialog-l40 ui-dialog-pt15">
  <input class="ui-dialog-input ui-dialog-input-password" type="input" placeholder="密码" />
  </div>
  <div class="ui-dialog-l40">
  <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" >忘记密码</a>
  </div>
  <div>
  <a class="ui-dialog-submit" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" >登录</a>
  </div>
  <div class="ui-dialog-l40">
  <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" >立即注册</a>
  </div>
 </div>
 </div>
 <div class="ui-mask" id="mask"></div>
 <div class="link">
 <a href="javascript:showdialog();" rel="external nofollow" style=" text-decoration: unset;color: #4ca8ff">登录</a>
 </div>
 <script type="text/javascript">
 // 获取元素对象
 function g(id) {
  return document.getelementbyid(id);
 }
 // 自动居中函数 -- 登录浮层
 // el {element}
 function autocenter(el) {
  // 获得可视区域的宽和高
  var bodyw = document.documentelement.clientwidth;
  var bodyh = document.documentelement.clientheight;
  // 获得元素 el 的宽和高
  var elw = el.offsetwidth;
  var elh = el.offsetheight;
  // 设置元素的 style 样式
  el.style.left = (bodyw - elw) / 2 + 'px';
  el.style.top = (bodyh - elh) / 2 + 'px';
 }
 // 扩展元素到整个可视区域 -- 遮罩层
 // el {element}
 function filltobody(el) {
  // 将元素的宽和高设置的和可视区域一样
  el.style.width = document.documentelement.clientwidth + 'px';
  el.style.height = document.documentelement.clientheight + 'px';
 }
 // 定义全局变量
 var mouseoffsetx = 0;
 var mouseoffsety = 0;
 var isdragging = false;
 // 鼠标事件1 -- 在标题栏上按下
 // 计算鼠标相对拖拽元素的的左上角的坐标, 并且标记元素为可拖动
 g('dialogtitle').addeventlistener('mousedown', function(e) {
  var e = e || window.event;
  // 用鼠标按下时的坐标减去 dialog 的左上角坐标
  mouseoffsetx = e.pagex - g('dialog').offsetleft;
  mouseoffsety = e.pagey - g('dialog').offsettop;
  isdragging = true;
 });
 // 鼠标事件2 -- 鼠标移动
 document.onmousemove = function(e) {
  var e = e || window.event;
  // 鼠标当前位置
  var mousex = e.pagex;
  var mousey = e.pagey;
  // 鼠标从单击时至当前时刻移动的距离
  var movex = 0;
  var movey = 0;
  if (isdragging === true) {
  movex = mousex - mouseoffsetx;
  movey = mousey - mouseoffsety;
  // 范围限定
  // movex > 0 且 movex < (页面最大宽度 - 浮层宽度)
  // movey > 0 且 movey < (页面最大宽度 - 浮层高度)
  var pagewidth = document.documentelement.clientwidth;
  var pageheight = document.documentelement.clientheight;
  // 登录浮层的宽、高
  var dialogwidth = g('dialog').offsetwidth;
  var dialogheight = g('dialog').offsetheight;
  var maxx = pagewidth - dialogwidth;
  var maxy = pageheight - dialogheight;
  movex = math.min(maxx, math.max(0, movex));
  movey = math.min(maxy, math.max(0, movey));
  g('dialog').style.left = movex + 'px';
  g('dialog').style.top = movey + 'px';
  }
 };
 // 鼠标事件3 -- 鼠标松开
 document.onmouseup = function() {
  isdragging = false;
 };
 // 展现登录浮层
 function showdialog() {
  g('dialog').style.display = 'block';
  g('mask').style.display = 'block';
  autocenter(g('dialog'));
  filltobody(g('mask'));
 }
 // 隐藏登录浮层
 function hidedialog() {
  g('dialog').style.display = 'none';
  g('mask').style.display = 'none';
 }
 window.onresize = function() {
  autocenter(g('dialog'));
  filltobody(g('mask'));
 };
 showdialog();
 autocenter(g('dialog'));
 </script>
</body>
</html>

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!