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)松開,模態(tài)框也不會(huì)隨鼠標(biāo)移動(dòng)。<完整的代碼在最后哦>

分析思路:
1.點(diǎn)擊彈出層,模態(tài)框和遮擋層就會(huì)顯示出來。display:block
2.點(diǎn)擊關(guān)閉按鈕,模態(tài)框和遮擋層就會(huì)隱藏。display:none

3.在頁(yè)面中拖拽的步驟:鼠標(biāo)按下并移動(dòng),之后松開鼠標(biāo)
4.觸發(fā)事件是鼠標(biāo)按下mousedown,鼠標(biāo)移動(dòng)是mousemove,鼠標(biāo)松開:mouseup
5.拖拽過程:鼠標(biāo)移動(dòng)過程中,獲得最新的值賦值給模態(tài)框的left和top值,這樣模態(tài)框就可以跟著鼠標(biāo)走了
6.鼠標(biāo)按下觸發(fā)的時(shí)間源是最上面一行,也就是說,鼠標(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)’來計(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)即可。注意移送事件要寫到按下事件里面
10.鼠標(biāo)松開,就停止拖拽,可以讓鼠標(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),之后松開鼠標(biāo)
? ? // 5.拖拽過程:鼠標(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顯示出來
? ? 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.開始拖拽
?? ?//(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)文章
多瀏覽器兼容的動(dòng)態(tài)加載 JavaScript 與 CSS
Omar AL Zabir這位MVP總是喜歡搞些稀奇古怪同時(shí)又很實(shí)用的小東西,并且還十分值得參考。最近他就做了一個(gè)叫做ensure的小工具用于動(dòng)態(tài)加載JavaScript、CSS與HTML,而且IE、Firefox、Opera、Safari都支持了,那么我們就來看看ensure是如何做到動(dòng)態(tài)加載JavaScript與CSS的。2008-09-09
微信開發(fā) 使用picker封裝省市區(qū)三級(jí)聯(lián)動(dòng)模板
這篇文章主要學(xué)習(xí)微信開發(fā),如何使用picker封裝省市區(qū)三級(jí)聯(lián)動(dòng)模板,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
Javascript遞歸打印Document層次關(guān)系實(shí)例分析
這篇文章主要介紹了Javascript遞歸打印Document層次關(guān)系的方法,實(shí)例分析了javascript中Document的層次關(guān)系,需要的朋友可以參考下2015-05-05
js字符串轉(zhuǎn)換成xml對(duì)象并使用技巧解讀
在js中有方法可以將字符串轉(zhuǎn)化為xml對(duì)象,感興趣的朋友可以參考下面的代碼片段,希望對(duì)你有所幫助2013-04-04
javascript實(shí)現(xiàn)小型區(qū)塊鏈功能
這篇文章主要介紹了javascript實(shí)現(xiàn)小型區(qū)塊鏈功能,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
JavaScript使用WebSocket實(shí)現(xiàn)實(shí)時(shí)通信的技術(shù)詳解
WebSocket作為一種高效的通信協(xié)議,為開發(fā)者提供了一種在客戶端和服務(wù)器之間進(jìn)行全雙工通信的方法,本文將深入探討WebSocket技術(shù),并提供實(shí)戰(zhàn)代碼示例2024-04-04
JS自定義對(duì)象創(chuàng)建與簡(jiǎn)單使用方法示例
這篇文章主要介紹了JS自定義對(duì)象創(chuàng)建與簡(jiǎn)單使用方法,結(jié)合實(shí)例形式分析了JavaScript創(chuàng)建與使用自定義對(duì)象的相關(guān)操作技巧,需要的朋友可以參考下2020-01-01
深入理解JavaScript系列(6) 強(qiáng)大的原型和原型鏈
JavaScript 不包含傳統(tǒng)的類繼承模型,而是使用 prototypal 原型模型2012-01-01

