1. 程式人生 > >JavaScript:鼠標拖拽效果

JavaScript:鼠標拖拽效果

NPU 模式 mask 便宜 dde scale :hover 是否 --

(之前的那個模板方法模式實在沒搞懂...等幾天再去研究8)

預覽效果:

技術分享圖片

限制拖動範圍在視口內、調整窗口時自動居中...

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge"
> <title>Document</title> <style> body{ background-image: url("images/baidu_demo.png"); background-repeat: no-repeat; background-position: top center; background-color: #fff; /* 註意這個作用 */ padding
: 0px; margin: 0px; font-size: 12px; /* font-family: "微軟雅黑"; */ } .ui-dialog{ display: none; width: 380px; height: auto; position: absolute; z-index: 1000; top: 0px; left
: 0px; border: 1px solid #d5d5d5; background-color: #fff; } .ui-dialog-title{ height: 48px; line-height: 48px; padding:0px 20px; color: #535353; font-size: 16px; background: #f5f5f5; border-bottom: 1px solid #efefef; cursor: move; user-select: none; } .ui-dialog-content{ padding: 15px 20px; } .ui-dialog a{ text-decoration: none; } .ui-dialog-closeButton{ display: block; width: 16px; height: 16px; position: absolute; top: 12px; right: 20px; background: url("images/close_def.png") no-repeat; cursor: pointer; } .ui-dialog-closeButton:hover{ background: url("images/close_hov.png"); } .ui-dialog-pt15{ padding-top: 15px; } p{ margin-block-start: 0em; margin-block-end: 0em; margin-inline-start: 0px; margin-inline-end: 0px; } .ui-dialog-l40{ height: 40px; line-height: 40px; text-align: right; } .ui-dialog-input{ width: 100%; height: 40px; margin:0px; padding: 0px; border: 1px solid #d5d5d5; font-size: 16px; color: black; 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: #fff; outline: none; text-decoration: none; display: block; text-align: center; line-height: 50px; } .ui-mask{ width: 100%; height: 100%; background: #000; opacity: 0.4; position: absolute; top: 0; left: 0; z-index: 900; 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="dialog-title"> <a href="javascript:hideDialog();" class="ui-dialog-closeButton"></a> <p>登錄通行癥</p> </div> <!-- 內容區域 --> <div class="ui-dialog-content"> <div class="ui-dialog-l40 ui-dialog-pt15"> <input placeholder="手機/郵箱/用戶名" type="input" class="ui-dialog-input ui-dialog-input-username"> </div> <div class="ui-dialog-l40 ui-dialog-pt15"> <input placeholder="密碼" type="input" class="ui-dialog-input ui-dialog-input-password"> </div> <div class="ui-dialog-l40"> <a href="#">忘記密碼</a> </div> <div class="ui-dialog-submit"> <a href="#" class="ui-dialog-submit">登錄</a> </div> <div class="ui-dialog-l40"> <a href="#">立即註冊~</a> </div> </div> </div> <div class="ui-mask" id="mask" onselectstart="return false"></div> <div class="link"> <a href="javascript:showDialog();">登錄</a> </div> </body> <script> //按下時,標記為可拖動。開始拖動,更新position。松開,標記不可拖動 //獲取對象 function getElement(id){ return document.getElementById(id); }; //自動居中函數(提示框) function autoCenter(el){ var bodyWidth = document.documentElement.clientWidth; var bodyHeight = document.documentElement.clientHeight; var elWidth = el.offsetWidth; var elHeight = el.offsetHeight; el.style.left = (bodyWidth-elWidth)/2 + "px"; el.style.top = (bodyHeight-elHeight)/2 + "px"; } //自動全屏函數(遮罩) function furfillBody(el){ el.style.width = document.documentElement.clientWidth + "px"; el.style.Height = document.documentElement.clientHeight + "px"; } var mouseOffsetX = 0, mouseOffsetY = 0; var draggble = false;//是否可拖拽,一會用data-xxx寫 //在標題欄上按下,計算相對拖拽元素左上角坐標,並標記該元素可拖動 getElement("dialog-title").addEventListener(mousedown,function(e){ console.log(e); var e = e || window.event; mouseOffsetX = e.pageX - getElement(dialog).offsetLeft; mouseOffsetY = e.pageY - getElement(dialog).offsetTop; draggble = true; console.log(mouseOffsetX,mouseOffsetY,draggble); //記錄當前鼠標在標題欄中的相對位置 //註意這裏是整個登陸浮層 }) //按下開始移動,先檢查是否已被標記為可拖動,如果是則進行更新(減去第一步中已經獲得的便宜) document.onmousemove = function(e){ var e = e || window.event; mouseX = e.pageX;//鼠標當前位置 mouseY = e.pageY; var moveToX = 0,//新位置 moveToY = 0; if(draggble){ // console.log(1); moveToX = mouseX - mouseOffsetX; moveToY = mouseY - mouseOffsetY; var pageWidth = document.documentElement.clientWidth; var pageHeight = document.documentElement.clientHeight; var dialogWidth = getElement(dialog).offsetWidth; var dialogHeight = getElement(dialog).offsetHeight; var maxX = pageWidth - dialogWidth; var maxY = pageHeight - dialogHeight; moveToX = Math.min(maxX , Math.max(0,moveToX) ); moveToY = Math.min(maxY , Math.max(0,moveToY) ); getElement(dialog).style.left = moveToX + "px"; getElement(dialog).style.top = moveToY + "px"; } } //鼠標松開,標記為不可拖動 //針對於整個document document.onmouseup = function(){ draggble = false; } function showDialog(){ getElement(dialog).style.display = "block"; getElement(mask).style.display = "block"; autoCenter(getElement(dialog)); furfillBody(getElement(mask)); }; function hideDialog(){ getElement(dialog).style.display = "none"; getElement(mask).style.display = "none"; }; //窗口改變大小時的事件處理 window.onresize = function(){ autoCenter(getElement(dialog)); furfillBody(getElement(mask)) } </script> </html>

JavaScript:鼠標拖拽效果