js實(shí)現(xiàn)模態(tài)框的拖拽效果
本文實(shí)例為大家分享了js實(shí)現(xiàn)模態(tài)框拖拽效果的具體代碼,供大家參考,具體內(nèi)容如下
之前學(xué)習(xí)js遇到了這樣的需求:
鼠標(biāo)按下后,移動(dòng)鼠標(biāo),模態(tài)框隨鼠標(biāo)移動(dòng),鼠標(biāo)松開(kāi),模態(tài)框也不會(huì)隨鼠標(biāo)移動(dòng)。<完整的代碼在最后哦>
分析思路:
1.點(diǎn)擊彈出層,模態(tài)框和遮擋層就會(huì)顯示出來(lái)。display:block
2.點(diǎn)擊關(guān)閉按鈕,模態(tài)框和遮擋層就會(huì)隱藏。display:none
3.在頁(yè)面中拖拽的步驟:鼠標(biāo)按下并移動(dòng),之后松開(kāi)鼠標(biāo)
4.觸發(fā)事件是鼠標(biāo)按下mousedown,鼠標(biāo)移動(dòng)是mousemove,鼠標(biāo)松開(kāi):mouseup
5.拖拽過(guò)程:鼠標(biāo)移動(dòng)過(guò)程中,獲得最新的值賦值給模態(tài)框的left和top值,這樣模態(tài)框就可以跟著鼠標(biāo)走了
6.鼠標(biāo)按下觸發(fā)的時(shí)間源是最上面一行,也就是說(shuō),鼠標(biāo)只有放在最上面一行,才能觸發(fā)該事件。放在其他區(qū)域不會(huì)觸發(fā)該事件。
7.鼠標(biāo)的坐標(biāo)減去鼠標(biāo)在盒子內(nèi)的坐標(biāo),才是模態(tài)框真正的位置。(因?yàn)槟B(tài)框是可移動(dòng)的,只有第一次才能拿到模態(tài)框的left和top,其他時(shí)候并不能直接拿到。所以采用‘鼠標(biāo)的坐標(biāo) - 鼠標(biāo)在模態(tài)框內(nèi)的坐標(biāo)’來(lái)計(jì)算模態(tài)框的位置)
8.鼠標(biāo)按下,我們要得到鼠標(biāo)在盒子內(nèi)的坐標(biāo)
9.鼠標(biāo)移動(dòng),就讓模態(tài)框的坐標(biāo)設(shè)置為:鼠標(biāo)坐標(biāo) - 盒子坐標(biāo)即可。注意移送事件要寫(xiě)到按下事件里面
10.鼠標(biāo)松開(kāi),就停止拖拽,可以讓鼠標(biāo)移動(dòng)事件解除
代碼實(shí)現(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">點(diǎn)擊,彈出登錄框</a></div> ? ? <div class="login" id="login"> ? ? ? ? <div class="login-title">登錄會(huì)員 ? ? ? ? ? ? <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="請(qǐng)輸入用戶名" name="info[sername]" id="username" class="username"> ? ? ? ? ? ? </div> ? ? ? ? ? ? <div class="login-inpit"> ? ? ? ? ? ? ? ? <label for="">登錄密碼:</label> ? ? ? ? ? ? ? ? <input type="password" placeholder="請(qǐng)輸入登錄密碼" name="info[password]" id="password"> ? ? ? ? ? ? </div> ? ? ? ? </div> ? ? ? ? <div class="login-button" id="loginBtn"><a href="javascript:void(0);" id="login-button-submit">會(huì)員登錄</a></div> ? ? </div> ? ? <!-- 遮罩層 --> ? ? <div class="login-bg" id="bg"></div> </body> <script> ? ? // 1.點(diǎn)擊,彈出模態(tài)框和遮罩層 ? ? // 3.點(diǎn)擊關(guān)閉模態(tài)框和遮罩層自動(dòng)隱藏 ? ? // 4.頁(yè)面拖拽原理:鼠標(biāo)按下且移動(dòng),之后松開(kāi)鼠標(biāo) ? ? // 5.拖拽過(guò)程:鼠標(biāo)移動(dòng)的時(shí)候獲得新的值賦值給模態(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.點(diǎn)擊彈出層這個(gè)鏈接link,讓mask和login顯示出來(lái) ? ? oLink.addEventListener('click', function () { ? ? ? ? oLogin.style.display = 'block'; ? ? ? ? oBg.style.display = 'block'; ? ? }) ?? ?//3.點(diǎn)擊closeBtn就隱藏mask和login ? ? oClose.addEventListener('click', function () { ? ? ? ? oLogin.style.display = 'none'; ? ? ? ? oBg.style.display = 'none'; ? ? }) ?? ?//4.開(kāi)始拖拽 ?? ?//(1)當(dāng)我們鼠標(biāo)按下,就獲得鼠標(biāo)在盒子內(nèi)的坐標(biāo) ? ? title.addEventListener('mousedown', function (e) { ? ? ? ? var x = e.pageX - oLogin.offsetLeft; ? ? ? ? var y = e.pageY - oLogin.offsetTop; ? ? ? ? console.log(x, y) ?? ??? ? ?? ??? ?//(2)鼠標(biāo)移動(dòng)的時(shí)候,把鼠標(biāo)在頁(yè)面中的坐標(biāo) 減去 鼠標(biāo)在盒子內(nèi)的坐標(biāo)就是模態(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)鼠標(biāo)彈起,就讓鼠標(biāo)移動(dòng)事件移除 ? ? ? ? ? ? document.addEventListener('mouseup', function () { ? ? ? ? ? ? ? ? document.removeEventListener('mousemove', move); ? ? ? ? ? ? ? ?? ? ? ? ? ? ? }) ? ? ? ? }) ? ?? </script> </html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JS實(shí)現(xiàn)簡(jiǎn)單可拖動(dòng)的模態(tài)框
- JS實(shí)現(xiàn)模態(tài)框拖拽動(dòng)態(tài)效果
- JavaScript實(shí)現(xiàn)拖動(dòng)模態(tài)框
- JS實(shí)現(xiàn)簡(jiǎn)單拖動(dòng)模態(tài)框案例
- HTML+CSS+JavaScript實(shí)現(xiàn)可拖拽模態(tài)框
- js實(shí)現(xiàn)拖動(dòng)模態(tài)框效果
- JS實(shí)現(xiàn)拖動(dòng)模態(tài)框案例
- js實(shí)現(xiàn)拖動(dòng)模態(tài)框
- JavaScript實(shí)現(xiàn)模態(tài)框拖拽效果
- js實(shí)現(xiàn)模態(tài)框拖拽
相關(guān)文章
如何阻止復(fù)制剪切和粘貼事件為了表單內(nèi)容的安全
提交表單內(nèi)容如(密碼)重要信息時(shí),為了安全,需要阻止一些復(fù)制剪切和粘貼事件,下面與大家分享一個(gè)實(shí)例,感興趣的朋友可以參考下哈2013-05-05基于Bootstrap+jQuery.validate實(shí)現(xiàn)表單驗(yàn)證
這篇文章主要為大家詳細(xì)介紹了基于Bootstrap+jQuery.validate實(shí)現(xiàn)表單驗(yàn)證,感興趣的小伙伴們可以參考一下2016-05-05個(gè)人總結(jié)的一些JavaScript技巧、實(shí)用函數(shù)、簡(jiǎn)潔方法、編程細(xì)節(jié)
這篇文章主要介紹了個(gè)人總結(jié)的一些JavaScript技巧、實(shí)用函數(shù)、簡(jiǎn)潔方法、編程細(xì)節(jié),本文講解了變量轉(zhuǎn)換、取整同時(shí)轉(zhuǎn)換成數(shù)值型、日期轉(zhuǎn)數(shù)值、類數(shù)組對(duì)象轉(zhuǎn)數(shù)組、進(jìn)制之間的轉(zhuǎn)換等方法技巧,需要的朋友可以參考下2015-06-06