js實現登入框滑鼠拖拽效果,js登入滑鼠拖拽

來源:互聯網
上載者:User

js實現登入框滑鼠拖拽效果,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>

以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的協助,同時也希望多多支援幫客之家!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.