JS實(shí)現(xiàn)簡(jiǎn)單拖動(dòng)模態(tài)框案例
本文實(shí)例為大家分享了JS實(shí)現(xiàn)簡(jiǎn)單拖動(dòng)模態(tài)框的具體代碼,供大家參考,具體內(nèi)容如下
需要實(shí)現(xiàn)的效果:
①點(diǎn)擊“點(diǎn)擊,彈出登錄框”后模態(tài)框和遮擋層就會(huì)顯示出來
②點(diǎn)擊關(guān)閉按鈕,模態(tài)框和遮蓋層就會(huì)隱藏起來
③頁面拖拽
功能分析:
首先給上面的"點(diǎn)擊,彈出登錄框"設(shè)置點(diǎn)擊事件,點(diǎn)擊之后就顯示遮罩層和模態(tài)框,然后給模態(tài)框上面的關(guān)閉按鈕設(shè)置點(diǎn)擊事件,點(diǎn)擊之后就隱藏遮罩層和模態(tài)框。然后是拖拽過程,這個(gè)過程的實(shí)現(xiàn)較為復(fù)雜,主要分為下面幾步:
1.明確模態(tài)框的真正位置是鼠標(biāo)的坐標(biāo)減去鼠標(biāo)在模態(tài)框內(nèi)的坐標(biāo)。
2.鼠標(biāo)的坐標(biāo)通過鼠標(biāo)按下事件獲取,通過e.pageY和e.pageX來獲取。
3.按下之后想要獲得鼠標(biāo)在模態(tài)框中的坐標(biāo)(一直都不會(huì)變),需要獲得盒子的坐標(biāo),盒子坐標(biāo)通過element.offsetTop和element.offsetLeft來獲取,通過鼠標(biāo)的坐標(biāo)減去模態(tài)框的坐標(biāo)獲得鼠標(biāo)在模態(tài)框中的坐標(biāo)。
4.按下之后鼠標(biāo)移動(dòng),就讓模態(tài)框的坐標(biāo)設(shè)置稱為鼠標(biāo)的坐標(biāo)減去鼠標(biāo)在模態(tài)框中的坐標(biāo)。
5.鼠標(biāo)松開之后需要停止拖拽,就是移除鼠標(biāo)移動(dòng)事件。
完整代碼:
<!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>拖動(dòng)模態(tài)框</title> ? ? <script> ? ? ? ? window.onload = function() { ? ? ? ? ? ? var loginbox = document.querySelector('.loginbox'); ? ? ? ? ? ? var gray = document.querySelector('.gray'); ? ? ? ? ? ? var loginheader = document.querySelector('.login-header'); ? ? ? ? ? ? var close = document.querySelector('.close'); ? ? ? ? ? ? var move = document.querySelector('.move'); ? ? ? ? ? ? loginheader.addEventListener('click', function() { ? ? ? ? ? ? ? ? loginbox.style.display = 'block'; ? ? ? ? ? ? ? ? gray.style.display = 'block'; ? ? ? ? ? ? }); ? ? ? ? ? ? close.addEventListener('click', function() { ? ? ? ? ? ? ? ? loginbox.style.display = 'none'; ? ? ? ? ? ? ? ? gray.style.display = 'none'; ? ? ? ? ? ? }); ? ? ? ? ? ? move.addEventListener('mousedown', function(e) { ? ? ? ? ? ? ? ? // 鼠標(biāo)在盒子內(nèi)得距離 ? ? ? ? ? ? ? ? var x = e.pageX - loginbox.offsetLeft; ? ? ? ? ? ? ? ? var y = e.pageY - loginbox.offsetTop; ? ? ? ? ? ? ? ? // alert('hahah') ? ? ? ? ? ? ? ? document.addEventListener('mousemove', move) ? ? ? ? ? ? ? ? ? function move(e) { ? ? ? ? ? ? ? ? ? ? loginbox.style.left = e.pageX - x + 'px'; ? ? ? ? ? ? ? ? ? ? loginbox.style.top = e.pageY - y + 'px'; ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? // 鼠標(biāo)談起就移除鼠標(biāo)移動(dòng)事件 ? ? ? ? ? ? ? ? document.addEventListener('mouseup', function() { ? ? ? ? ? ? ? ? ? ? document.removeEventListener('mousemove', move) ? ? ? ? ? ? ? ? }) ? ? ? ? ? ? ? }) ? ? ? ? } ? ? </script> ? ? <style> ? ? ? ? .login-header { ? ? ? ? ? ? width: 100%; ? ? ? ? ? ? text-align: center; ? ? ? ? ? ? height: 30px; ? ? ? ? ? ? font-size: 24px; ? ? ? ? ? ? line-height: 30px; ? ? ? ? } ? ? ? ?? ? ? ? ? .move { ? ? ? ? ? ? cursor: move; ? ? ? ? } ? ? ? ?? ? ? ? ? ul, ? ? ? ? li, ? ? ? ? ol, ? ? ? ? dl, ? ? ? ? dt, ? ? ? ? dd, ? ? ? ? div, ? ? ? ? p, ? ? ? ? span, ? ? ? ? h1, ? ? ? ? h2, ? ? ? ? h3, ? ? ? ? h4, ? ? ? ? h5, ? ? ? ? h6, ? ? ? ? a { ? ? ? ? ? ? padding: 0px; ? ? ? ? ? ? margin: 0px; ? ? ? ? } ? ? ? ?? ? ? ? ? a { ? ? ? ? ? ? text-decoration: none; ? ? ? ? ? ? color: #000000; ? ? ? ? } ? ? ? ?? ? ? ? ? .loginbox { ? ? ? ? ? ? display: none; ? ? ? ? ? ? position: absolute; ? ? ? ? ? ? top: 200px; ? ? ? ? ? ? left: 50%; ? ? ? ? ? ? transform: translateX(-50%); ? ? ? ? ? ? z-index: 2; ? ? ? ? ? ? width: 520px; ? ? ? ? ? ? height: 300px; ? ? ? ? ? ? background-color: #fff; ? ? ? ? } ? ? ? ?? ? ? ? ? .loginbox .close { ? ? ? ? ? ? cursor: pointer; ? ? ? ? } ? ? ? ?? ? ? ? ? .loginbox p { ? ? ? ? ? ? float: left; ? ? ? ? ? ? font-size: 22px; ? ? ? ? ? ? text-align: center; ? ? ? ? ? ? margin-top: 30px; ? ? ? ? ? ? margin-left: 240px; ? ? ? ? } ? ? ? ?? ? ? ? ? .loginbox .content { ? ? ? ? ? ? float: right; ? ? ? ? ? ? margin-top: 30px; ? ? ? ? ? ? margin-right: 70px; ? ? ? ? } ? ? ? ?? ? ? ? ? .loginbox .content input { ? ? ? ? ? ? width: 300px; ? ? ? ? ? ? height: 25px; ? ? ? ? ? ? outline: none; ? ? ? ? ? ? border: 1px solid #ccc; ? ? ? ? } ? ? ? ?? ? ? ? ? .loginbox .btn { ? ? ? ? ? ? background-color: #fff; ? ? ? ? ? ? width: 180px; ? ? ? ? ? ? height: 30px; ? ? ? ? ? ? border: 1px solid #ccc; ? ? ? ? ? ? margin-top: 40px; ? ? ? ? ? ? margin-left: 170px; ? ? ? ? } ? ? ? ?? ? ? ? ? .loginbox span { ? ? ? ? ? ? position: absolute; ? ? ? ? ? ? font-size: 12px; ? ? ? ? ? ? text-align: center; ? ? ? ? ? ? line-height: 50px; ? ? ? ? ? ? z-index: 9999; ? ? ? ? ? ? top: -25px; ? ? ? ? ? ? right: -25px; ? ? ? ? ? ? width: 50px; ? ? ? ? ? ? height: 50px; ? ? ? ? ? ? background-color: #fff; ? ? ? ? ? ? color: #000000; ? ? ? ? ? ? border-radius: 50%; ? ? ? ? ? ? box-shadow: 1px 1px 5px rgba(0, 0, 0, .3); ? ? ? ? } ? ? ? ?? ? ? ? ? .gray { ? ? ? ? ? ? display: none; ? ? ? ? ? ? position: fixed; ? ? ? ? ? ? top: 0; ? ? ? ? ? ? left: 0; ? ? ? ? ? ? width: 100%; ? ? ? ? ? ? height: 100%; ? ? ? ? ? ? background-color: rgba(0, 0, 0, .3); ? ? ? ? } ? ? </style> </head> ? <body> ? ? <div class="login-header"><a id="link" href="javascript:;" >點(diǎn)擊,彈出登錄框</a></div> ? ? <div class="loginbox"> ? ? ? ? <p class="move">登錄會(huì)員</p> ? ? ? ? <div class="content"> ? ? ? ? ? ? <label for="">用戶名:</label> ? ? ? ? ? ? <input type="text" placeholder="請(qǐng)輸入用戶名"> ? ? ? ? </div> ? ? ? ? <div class="content"> ? ? ? ? ? ? <label for="">登錄密碼:</label> ? ? ? ? ? ? <input type="password" placeholder="請(qǐng)輸入用戶名"> ? ? ? ? </div> ? ? ? ? <input type="button" value="登錄會(huì)員" class="btn"> ? ? ? ? <span class="close">關(guān)閉</span> ? ? </div> ? ? <div class="gray"> ? ? ? </div> </body> ? </html>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript中關(guān)于iframe滾動(dòng)條的去除和保留
在開發(fā)中經(jīng)常遇到去掉全部的滾動(dòng)條,去掉右邊的滾動(dòng)條且保留底下的滾動(dòng)條,去掉底下的滾動(dòng)條且保留右邊的滾動(dòng)條,大家基于js是怎么實(shí)現(xiàn)的呢?下面小編通過本文給大家詳細(xì)介紹下,對(duì)js iframe滾動(dòng)條相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-11-11教你一步步實(shí)現(xiàn)一個(gè)簡(jiǎn)易promise
Promise是異步編程的一種解決方案,比傳統(tǒng)的解決方案回調(diào)函數(shù)和事件更合理且更強(qiáng)大,這篇文章主要給大家介紹了關(guān)于如何一步步實(shí)現(xiàn)一個(gè)簡(jiǎn)易promise的相關(guān)資料,需要的朋友可以參考下2021-11-11基于HTML+CSS+JS實(shí)現(xiàn)紙牌記憶游戲
這篇文章主要介紹了如何利用HTML、CSS?和?JavaScript?制作紙牌記憶游戲,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起動(dòng)手嘗試一下2022-04-04js ajaxfileupload.js上傳報(bào)錯(cuò)的解決方法
這篇文章主要為大家詳細(xì)介紹了js ajaxupload.js上傳報(bào)錯(cuò)的解決方法,感興趣的小伙伴們可以參考一下2016-05-05JS函數(shù)進(jìn)階之繼承用法實(shí)例分析
這篇文章主要介紹了JS函數(shù)進(jìn)階之繼承用法,結(jié)合實(shí)例形式分析了JavaScript函數(shù)繼承相關(guān)定義與使用操作技巧,需要的朋友可以參考下2020-01-01JS的執(zhí)行機(jī)制(EventLoop、宏任務(wù)和微任務(wù))
這篇文章主要介紹了JS的執(zhí)行機(jī)制(EventLoop、宏任務(wù)和微任務(wù)),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2023-01-01如何使用webpack打包一個(gè)庫library的方法步驟
這篇文章主要介紹了如何使用webpack打包一個(gè)庫library的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12原生javascript實(shí)現(xiàn)addClass,removeClass,hasClass函數(shù)
這篇文章主要介紹了原生javascript實(shí)現(xiàn)addClass,removeClass,hasClass函數(shù)的相關(guān)代碼,有需要的小伙伴可以參考下2016-02-02