JavaScript實(shí)現(xiàn)拖動(dòng)對話框效果的實(shí)現(xiàn)代碼
代碼實(shí)現(xiàn):
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> * { margin: 0; top: 0; } .login-btn { width: 50px; height: 50px; line-height: 50px; font-size: 16px; text-align: center; margin: 100px auto; background-color: #1E1E1E; color: white; border-radius: 50%; } .login-btn:hover { cursor: pointer; background-color: #323233; box-shadow: 3px 3px 10px rgba(0, 0, 0, .3); } .bg { display: none; width: 100%; height: 100%; position: fixed; top: 0; left: 0; background-color: rgba(0, 0, 0, .4); } .login { display: none; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 300px; height: 200px; background-color: #1E1E1E; box-shadow: 4px 4px 15px rgba(0, 0, 0, .3); } .hd { position: relative; width: 100%; height: 26px; background-color: #323233; } .hd:hover { cursor: move; } .close { position: absolute; top: 3px; right: 5px; width: 20px; height: 20px; background-color: red; text-align: center; line-height: 20px; border-radius: 50%; box-shadow: 0 0 5px rgba(0, 0, 0, .7) inset; } .close:hover { background-color: yellow; cursor: pointer; } </style> </head> <body> <div class="login-btn">點(diǎn)擊</div> <div class="bg"></div> <div class="login"> <div class="hd"> <div class="close">×</div> </div> </div> <script> // 獲取元素 var btn = document.querySelector('.login-btn'); var bg = document.querySelector('.bg'); var login = document.querySelector('.login'); var close = document.querySelector('.close'); var hd = document.querySelector('.hd'); // 按下btn,彈出對話框 btn.addEventListener('click', function() { bg.style.display = 'block'; login.style.display = 'block'; }); // 按下close,關(guān)閉對話框 close.addEventListener('click', function() { bg.style.display = 'none'; login.style.display = 'none'; }); hd.addEventListener('mousedown', function(e) { // 鼠標(biāo)按下對話框頂部時(shí),獲取鼠標(biāo)到對話框的距離 var x = e.pageX - login.offsetLeft; var y = e.pageY - login.offsetTop; // 鼠標(biāo)按下并移動(dòng)時(shí),實(shí)時(shí)更新對話框的位置 document.addEventListener('mousemove', move); function move(e) { login.style.left = e.pageX - x + 'px'; login.style.top = e.pageY - y + 'px'; } // 鼠標(biāo)松開時(shí),移除拖拽的動(dòng)作 document.addEventListener('mouseup', function() { document.removeEventListener('mousemove', move); }); }); </script> </body> </html>
實(shí)現(xiàn)效果:
點(diǎn)擊點(diǎn)擊
按鈕,彈出對話框。
按住對話框頂部并移動(dòng),實(shí)現(xiàn)拖動(dòng)效果。
點(diǎn)擊對話框右上角×
,關(guān)閉對話框。
到此這篇關(guān)于JavaScript實(shí)現(xiàn)拖動(dòng)對話框效果的實(shí)現(xiàn)代碼的文章就介紹到這了,更多相關(guān)js拖動(dòng)對話框內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
js獲取當(dāng)前年月日詳細(xì)教程(看這一篇就夠了)
這篇文章主要給大家介紹了關(guān)于js獲取當(dāng)前年月日的相關(guān)資料,JavaScript內(nèi)置的Date對象是獲取當(dāng)前日期最常用的工具之一,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12利用JavaScript在網(wǎng)頁實(shí)現(xiàn)八數(shù)碼啟發(fā)式A*算法動(dòng)畫效果
這篇文章主要介紹了利用JavaScript在網(wǎng)頁實(shí)現(xiàn)八數(shù)碼啟發(fā)式A*算法動(dòng)畫效果,需要的朋友可以參考下2017-04-04Bootstrap php制作動(dòng)態(tài)分頁標(biāo)簽
這篇文章主要為大家詳細(xì)介紹了Bootstrap php制作動(dòng)態(tài)分頁標(biāo)簽的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12JS傳遞對象數(shù)組為參數(shù)給后端,后端獲取的實(shí)例代碼
下面小編就為大家?guī)硪黄狫S傳遞對象數(shù)組為參數(shù)給后端,后端獲取的實(shí)例代碼。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-06-06JavaScript 數(shù)據(jù)元素集合與數(shù)組的區(qū)別說明
我們在獲取一組頁面元素時(shí)常會(huì)用到getElementsByName()或是getElementsByTagName()方法。2010-05-05微信小程序?qū)W習(xí)筆記之跳轉(zhuǎn)頁面、傳遞參數(shù)獲得數(shù)據(jù)操作圖文詳解
這篇文章主要介紹了微信小程序?qū)W習(xí)筆記之跳轉(zhuǎn)頁面、傳遞參數(shù)獲得數(shù)據(jù)操作,結(jié)合實(shí)例形式分析了微信小程序基于navigator組件的頁面跳轉(zhuǎn)及數(shù)據(jù)傳遞相關(guān)操作技巧,并結(jié)合圖文形式進(jìn)行詳細(xì)說明,需要的朋友可以參考下2019-03-03