js實(shí)現(xiàn)拖動(dòng)模態(tài)框效果
本文實(shí)例為大家分享了js實(shí)現(xiàn)拖動(dòng)模態(tài)框效果的具體代碼,供大家參考,具體內(nèi)容如下
1.實(shí)現(xiàn)效果:

點(diǎn)擊鏈接,彈出模態(tài)框。點(diǎn)擊關(guān)閉,關(guān)閉模態(tài)框。
點(diǎn)擊標(biāo)題部分,可以隨意移動(dòng)模態(tài)框的位置。
主要是獲取鼠標(biāo)位置。
2.思路:

3.代碼:
<!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>
? ? <style>
? ? ? ? body {
? ? ? ? ? ? padding: 0;
? ? ? ? ? ? margin: 0;
? ? ? ? }
? ? ? ? .login {
? ? ? ? ? ? display: none;
? ? ? ? ? ? position: fixed;
? ? ? ? ? ? top: 50%;
? ? ? ? ? ? left:50%;
? ? ? ? ? ? box-sizing:border-box;
? ? ? ? ? ? width:400px;
? ? ? ? ? ? height: 200px;
? ? ? ? ? ? background-color: pink;
? ? ? ? ? ? box-shadow: 0px 0px 20px #ddd;
? ? ? ? ? ? z-index: 9999;
? ? ? ? ? ? transform: translate(-50%, -50%);
? ? ? ? ? ? /* 讓一個(gè)固定定位的盒子 垂直居中 */
?
? ? ? ??
? ? ? ? }
? ? ? ? h4 {
? ? ? ? ? ? text-align: center;
? ? ? ? ? ? line-height: 70px;
? ? ? ? }
? ? ? ? form {
? ? ? ? ? ? text-align: center;
? ? ? ? ? ? margin-left: 40px;
? ? ? ? }
? ? ? ? a {
? ? ? ? ? ? text-decoration: none;
? ? ? ? ? ? text-align: center;
? ? ? ? }
? ? ? ? .finish {
? ? ? ? ? ? position: absolute;
? ? ? ? ? ? top:-15px;
? ? ? ? ? ? right:-15px;
? ? ? ? ? ? width: 30px;
? ? ? ? ? ? height: 30px;
? ? ? ? ? ? border: 1px solid white;
? ? ? ? ? ? border-radius: 50%;
? ? ? ? ? ? background-color: white;
? ? ? ? ? ? font-size: 10px;
? ? ? ? ? ? line-height: 30px;
? ? ? ? }
? ? ? ? .login-bg {
? ? ? ? ? ? display: none;
? ? ? ? ? ? width: 100%;
? ? ? ? ? ? height: 100%;
? ? ? ? ? ? position: fixed;
? ? ? ? ? ? top:0px;
? ? ? ? ? ? left: 0px;
? ? ? ? ? ? background-color: rgba(0,0,0,0.3);
? ? ? ? }
? ? ? ? .title {
? ? ? ? ? ? width: 400px;
? ? ? ? ? ? height: 50px;
? ? ? ? ? ? cursor: move;
? ? ? ? ? ? margin-top: -20px;
? ? ? ? ? ? height: 70px;
? ? ? ? ?
? ? ? ? }
? ? </style>
</head>
<body>
? ? <div class="bg">
? ? ? ? <div class="link"><a href="javascript:;" >點(diǎn)擊,彈出登錄框</a></div>
? ? ? ? <div class="login">
? ? ? ? ? ? <div class="finish">關(guān)閉</div>
? ? ? ? ? ? <div class = "title"><h4>登錄會(huì)員</h4></div>
? ? ? ? ? ? <form action="">
? ? ? ? ? ? ? ? <table>
? ? ? ? ? ? ? ? ? ? <tr>
? ? ? ? ? ? ? ? ? ? ? ? <td>用戶名:</td>
? ? ? ? ? ? ? ? ? ? ? ? <td><input type="text" name="" id="" value="請(qǐng)輸入用戶名"></td>
? ? ? ? ? ? ? ? ? ? </tr>
? ? ? ? ? ? ? ? ? ? <tr>
? ? ? ? ? ? ? ? ? ? ? ? <td>登錄密碼:</td>
? ? ? ? ? ? ? ? ? ? ? ? <td><input type="text" name="" id="" value="請(qǐng)輸入密碼"></td>
? ? ? ? ? ? ? ? ? ? </tr>
? ? ? ? ? ? ? ? </table>
? ? ? ? ? ? ? ? <input type="submit" name="" id="" value="登錄會(huì)員">
? ? ? ? ? ? </form>
? ? ? ? </div>
? ? </div>
? ? <!-- 遮蓋層 -->
? ? <div id="bg" class="login-bg"></div>
? ? <script>
? ? ? ? var a = document.querySelector('a');
? ? ? ? var login = document.querySelector('.login');
? ? ? ? var mask = document.querySelector('.login-bg');
? ? ? ? var finish = document.querySelector('.finish');
? ? ? ? a.addEventListener('click',function() {
? ? ? ? ? ? login.style.display = 'block';
? ? ? ? ? ? mask.style.display = 'block';
? ? ? ? });
? ? ? ? finish.addEventListener('click', function(){
? ? ? ? ? ? login.style.display = 'none';
? ? ? ? ? ? mask.style.display = 'none';
? ? ? ? })
? ? ? ? var title = document.querySelector('.title');
? ? ? ? title.addEventListener('mousedown', function(e){
? ? ? ? ? ? var x = e.pageX - login.offsetLeft;
? ? ? ? ? ? var y = e.pageY - login.offsetTop;
? ? ? ? ? ? // 鼠標(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) {
? ? ? ? ? ? ? ? // 別忘了加單位。
? ? ? ? ? ? ? ? login.style.left = e.pageX - x + 'px';
? ? ? ? ? ? ? ? login.style.top = e.pageY - y + 'px';
? ? ? ? ? ? }
? ? ? ? ? ? // 鼠標(biāo)彈起,讓鼠標(biāo)移動(dòng)事件停止。
? ? ? ? ? ? document.addEventListener('mouseup', function() {
? ? ? ? ? ? ? ? document.removeEventListener('mousemove', move);
? ? ? ? ? ? })
? ? ? ? })
? ? </script>
</body>
</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)模態(tài)框的拖拽效果
- js實(shí)現(xiàn)拖動(dòng)模態(tài)框
- JavaScript實(shí)現(xiàn)模態(tài)框拖拽效果
- js實(shí)現(xiàn)模態(tài)框拖拽
相關(guān)文章
js中Math之random,round,ceil,floor的用法總結(jié)
本篇文章是對(duì)js中Math之random,round,ceil,floor的用法進(jìn)行了總結(jié)介紹,需要的朋友可以過來(lái)參考下,希望對(duì)大家有所幫助2013-12-12
用VsCode編輯TypeScript的實(shí)現(xiàn)方法
這篇文章主要介紹了用VsCode編輯TypeScript的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
JavaScript使用享元模式實(shí)現(xiàn)文件上傳優(yōu)化操作示例
這篇文章主要介紹了JavaScript使用享元模式實(shí)現(xiàn)文件上傳優(yōu)化操作,結(jié)合實(shí)例形式分析了javascript基于享元模式進(jìn)行文件上傳優(yōu)化操作的原理、步驟及相關(guān)使用技巧,需要的朋友可以參考下2018-08-08
JavaScript實(shí)現(xiàn)的瀏覽器下載文件的方法
本文通過一段簡(jiǎn)單的代碼給大家介紹了js實(shí)現(xiàn)瀏覽器下載文件的方法,需要的的朋友參考下吧2017-08-08
Javascript 構(gòu)造函數(shù),公有,私有特權(quán)和靜態(tài)成員定義方法
其中公有方法聲明的部分采用的兩種方式,在實(shí)際應(yīng)用中一般采取一種方式就可以了,如果兩種方式都要采用的話,應(yīng)注意順序,防止前面寫的方法被清空或覆蓋。2009-11-11
JS/HTML5游戲常用算法之碰撞檢測(cè) 地圖格子算法實(shí)例詳解
這篇文章主要介紹了JS/HTML5游戲常用算法之碰撞檢測(cè) 地圖格子算法,結(jié)合實(shí)例形式詳細(xì)分析了javascript碰撞檢測(cè)算法的相關(guān)原理、實(shí)現(xiàn)技巧與操作注意事項(xiàng),需要的朋友可以參考下2018-12-12
js編寫一個(gè)簡(jiǎn)單的產(chǎn)品放大效果代碼
這篇文章主要為大家分享了js編寫一個(gè)簡(jiǎn)單的產(chǎn)品放大效果代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06

