js實現(xiàn)模態(tài)框的拖拽效果
本文實例為大家分享了js實現(xiàn)模態(tài)框拖拽效果的具體代碼,供大家參考,具體內(nèi)容如下
之前學(xué)習(xí)js遇到了這樣的需求:
鼠標按下后,移動鼠標,模態(tài)框隨鼠標移動,鼠標松開,模態(tài)框也不會隨鼠標移動。<完整的代碼在最后哦>
分析思路:
1.點擊彈出層,模態(tài)框和遮擋層就會顯示出來。display:block
2.點擊關(guān)閉按鈕,模態(tài)框和遮擋層就會隱藏。display:none
3.在頁面中拖拽的步驟:鼠標按下并移動,之后松開鼠標
4.觸發(fā)事件是鼠標按下mousedown,鼠標移動是mousemove,鼠標松開:mouseup
5.拖拽過程:鼠標移動過程中,獲得最新的值賦值給模態(tài)框的left和top值,這樣模態(tài)框就可以跟著鼠標走了
6.鼠標按下觸發(fā)的時間源是最上面一行,也就是說,鼠標只有放在最上面一行,才能觸發(fā)該事件。放在其他區(qū)域不會觸發(fā)該事件。
7.鼠標的坐標減去鼠標在盒子內(nèi)的坐標,才是模態(tài)框真正的位置。(因為模態(tài)框是可移動的,只有第一次才能拿到模態(tài)框的left和top,其他時候并不能直接拿到。所以采用‘鼠標的坐標 - 鼠標在模態(tài)框內(nèi)的坐標’來計算模態(tài)框的位置)
8.鼠標按下,我們要得到鼠標在盒子內(nèi)的坐標
9.鼠標移動,就讓模態(tài)框的坐標設(shè)置為:鼠標坐標 - 盒子坐標即可。注意移送事件要寫到按下事件里面
10.鼠標松開,就停止拖拽,可以讓鼠標移動事件解除
代碼實現(xiàn):
<!DOCTYPE html> <html lang="en"> <head> ? ? <meta charset="UTF-8"> ? ? <meta http-equiv="X-UA-Compatible" content="IE=edge"> ? ? <meta name="viewport" content="width=device-width, initial-scale=1.0"> ? ? <title>模態(tài)框拖拽</title> ? ? <style> ? ? ? ? * { ? ? ? ? ? ? margin: 0; ? ? ? ? ? ? padding: 0; ? ? ? ? } ? ? ? ? #link { ? ? ? ? ? ? color: #000; ? ? ? ? ? ? text-decoration: none; ? ? ? ? ? ? border: 1px solid #000; ? ? ? ? } ? ? ? ? .login { ? ? ? ? ? ? width: 300px; ? ? ? ? ? ? height: 200px; ? ? ? ? ? ? background-color: antiquewhite; ? ? ? ? ? ? position: absolute; ? ? ? ? ? ? top: 50%; ? ? ? ? ? ? left: 50%; ? ? ? ? ? ? transform: translate(-50%, -50%); ? ? ? ? ? ? z-index: 2; ? ? ? ? ? ? display: none; ? ? ? ? } ? ? ? ? .login-title { ? ? ? ? ? ? text-align: center; ? ? ? ? ? ? width: 100%; ? ? ? ? ? ? height: 40px; ? ? ? ? ? ? line-height: 40px; ? ? ? ? ? ? background-color: aqua; ? ? ? ? ? ? cursor: move; ? ? ? ? } ? ? ? ? .login-title span { ? ? ? ? ? ? display: block; ? ? ? ? ? ? height: 30px; ? ? ? ? ? ? width: 30px; ? ? ? ? ? ? background-color: antiquewhite; ? ? ? ? ? ? line-height: 30px; ? ? ? ? ? ? border-radius: 50%; ? ? ? ? ? ? position: absolute; ? ? ? ? ? ? top: -10px; ? ? ? ? ? ? right: -10px; ? ? ? ? ? ? font-size: 12px; ? ? ? ? } ? ? ? ? .login-title span a { ? ? ? ? ? ? text-decoration: none; ? ? ? ? ? ? color: #000; ? ? ? ? } ? ? ? ? .login-input-content { ? ? ? ? ? ? margin: 15px 20px 0; ? ? ? ? ? ? line-height: 30px; ? ? ? ? } ? ? ? ? .login-button { ? ? ? ? ? ? width: 200px; ? ? ? ? ? ? height: 20px; ? ? ? ? ? ? margin: 10px auto; ? ? ? ? ? ? border: 1px solid rgb(77, 73, 73); ? ? ? ? ? ? text-align: center; ? ? ? ? } ? ? ? ? .login-button a { ? ? ? ? ? ? text-decoration: none; ? ? ? ? ? ? color: #000; ? ? ? ? ? ? font-size: 14px; ? ? ? ? } ? ? ? ? #bg { ? ? ? ? ? ? display: none; ? ? ? ? ? ? background-color: #000; ? ? ? ? ? ? width: 100%; ? ? ? ? ? ? height: 100%; ? ? ? ? ? ? opacity: 0.3; ? ? ? ? ? ? z-index: -1; ? ? ? ? ? ? position: absolute; ? ? ? ? ? ? top: 0; ? ? ? ? ? ? left: 0; ? ? ? ? } ? ? </style> </head> <body> ? ? <div class="login-header"><a href="javascript:;" id="link">點擊,彈出登錄框</a></div> ? ? <div class="login" id="login"> ? ? ? ? <div class="login-title">登錄會員 ? ? ? ? ? ? <span><a id="colseBth" href="javascript:void(0);" class="close-login">關(guān)閉</a></span> ? ? ? ? </div> ? ? ? ? <div class="login-input-content"> ? ? ? ? ? ? <div class="login-input"> ? ? ? ? ? ? ? ? <label for="">用 戶 名:</label> ? ? ? ? ? ? ? ? <input type="text" placeholder="請輸入用戶名" name="info[sername]" id="username" class="username"> ? ? ? ? ? ? </div> ? ? ? ? ? ? <div class="login-inpit"> ? ? ? ? ? ? ? ? <label for="">登錄密碼:</label> ? ? ? ? ? ? ? ? <input type="password" placeholder="請輸入登錄密碼" name="info[password]" id="password"> ? ? ? ? ? ? </div> ? ? ? ? </div> ? ? ? ? <div class="login-button" id="loginBtn"><a href="javascript:void(0);" id="login-button-submit">會員登錄</a></div> ? ? </div> ? ? <!-- 遮罩層 --> ? ? <div class="login-bg" id="bg"></div> </body> <script> ? ? // 1.點擊,彈出模態(tài)框和遮罩層 ? ? // 3.點擊關(guān)閉模態(tài)框和遮罩層自動隱藏 ? ? // 4.頁面拖拽原理:鼠標按下且移動,之后松開鼠標 ? ? // 5.拖拽過程:鼠標移動的時候獲得新的值賦值給模態(tài)框的left和top值。 ?? ? ?? ?//1.獲取DOM元素 ? ? var oLink = document.querySelector('#link'); ? ? var oLogin = document.querySelector('.login'); ? ? var oBg = document.querySelector('#bg'); ? ? var oClose = oLogin.querySelector('#colseBth'); ? ? var title = oLogin.querySelector('.login-title'); ?? ?//2.點擊彈出層這個鏈接link,讓mask和login顯示出來 ? ? oLink.addEventListener('click', function () { ? ? ? ? oLogin.style.display = 'block'; ? ? ? ? oBg.style.display = 'block'; ? ? }) ?? ?//3.點擊closeBtn就隱藏mask和login ? ? oClose.addEventListener('click', function () { ? ? ? ? oLogin.style.display = 'none'; ? ? ? ? oBg.style.display = 'none'; ? ? }) ?? ?//4.開始拖拽 ?? ?//(1)當我們鼠標按下,就獲得鼠標在盒子內(nèi)的坐標 ? ? title.addEventListener('mousedown', function (e) { ? ? ? ? var x = e.pageX - oLogin.offsetLeft; ? ? ? ? var y = e.pageY - oLogin.offsetTop; ? ? ? ? console.log(x, y) ?? ??? ? ?? ??? ?//(2)鼠標移動的時候,把鼠標在頁面中的坐標 減去 鼠標在盒子內(nèi)的坐標就是模態(tài)框的left和top值 ? ? ? ? document.addEventListener('mousemove', move)? ? ? ? ? ? ? function move(e) { ? ? ? ? ? ? ? ? oLogin.style.left = e.pageX - x + 'px'; ? ? ? ? ? ? ? ? oLogin.style.top = e.pageY - y + 'px'; ? ? ? ? ? ? } ?? ??? ??? ? ?? ??? ??? ?//(3)鼠標彈起,就讓鼠標移動事件移除 ? ? ? ? ? ? document.addEventListener('mouseup', function () { ? ? ? ? ? ? ? ? document.removeEventListener('mousemove', move); ? ? ? ? ? ? ? ?? ? ? ? ? ? ? }) ? ? ? ? }) ? ?? </script> </html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
如何阻止復(fù)制剪切和粘貼事件為了表單內(nèi)容的安全
提交表單內(nèi)容如(密碼)重要信息時,為了安全,需要阻止一些復(fù)制剪切和粘貼事件,下面與大家分享一個實例,感興趣的朋友可以參考下哈2013-05-05基于Bootstrap+jQuery.validate實現(xiàn)表單驗證
這篇文章主要為大家詳細介紹了基于Bootstrap+jQuery.validate實現(xiàn)表單驗證,感興趣的小伙伴們可以參考一下2016-05-05個人總結(jié)的一些JavaScript技巧、實用函數(shù)、簡潔方法、編程細節(jié)
這篇文章主要介紹了個人總結(jié)的一些JavaScript技巧、實用函數(shù)、簡潔方法、編程細節(jié),本文講解了變量轉(zhuǎn)換、取整同時轉(zhuǎn)換成數(shù)值型、日期轉(zhuǎn)數(shù)值、類數(shù)組對象轉(zhuǎn)數(shù)組、進制之間的轉(zhuǎn)換等方法技巧,需要的朋友可以參考下2015-06-06