javascript實(shí)現(xiàn)移動(dòng)的模態(tài)框效果
本文實(shí)例為大家分享了javascript實(shí)現(xiàn)移動(dòng)的模態(tài)框效果的具體代碼,供大家參考,具體內(nèi)容如下
頁(yè)面效果:
點(diǎn)擊鏈接后,彈出登錄模態(tài)框,點(diǎn)擊關(guān)閉鏈接可以關(guān)閉模態(tài)框,鼠標(biāo)在模態(tài)框標(biāo)題區(qū)域按下后可以拖拽模態(tài)框,松開(kāi)鼠標(biāo)后,模態(tài)框停止移動(dòng)
實(shí)現(xiàn)思路:
1、html、css搭建好頁(yè)面,設(shè)置好模態(tài)框內(nèi)容和樣式后,將模態(tài)框隱藏:display: none;如果點(diǎn)擊彈出模態(tài)框后,頁(yè)面背景色發(fā)生改變,可以添加一個(gè)遮罩層,將遮罩層也先隱藏
2、給點(diǎn)擊后彈出模態(tài)框的元素添加點(diǎn)擊事件- - -onclick
事件處理程序中設(shè)置- - -模態(tài)框 和 遮罩層 顯示- - -eg:
login.style.display = ‘block'; loginBg.style.display = ‘block';
3、給關(guān)閉模態(tài)框元素添加點(diǎn)擊事件- - -onclick
事件處理程序中設(shè)置- - -模態(tài)框 和 遮罩層 隱藏- - -eg:
login.style.display = ‘none'; loginBg.style.display = ‘none';
4、給模態(tài)框標(biāo)題部分添加鼠標(biāo)按下事件- - -mousedown
獲取鼠標(biāo)在模態(tài)框中的位置
5、鼠標(biāo)按下事件中再添加鼠標(biāo)移動(dòng)事件- - -mousemove
document.addEventListener(‘mousemove', move);
獲取鼠標(biāo)在頁(yè)面中的位置
鼠標(biāo)的位置值 - 鼠標(biāo)在模態(tài)框中的位置值 = 模態(tài)框在頁(yè)面中的位置值
將計(jì)算得出的位置值賦值給模態(tài)框的top、left,就可以達(dá)到拖拽鼠標(biāo)的效果,跟隨鼠標(biāo)移動(dòng)
6、當(dāng)鼠標(biāo)松開(kāi)后,模態(tài)框要停止移動(dòng)。鼠標(biāo)按下事件中再添加鼠標(biāo)松開(kāi)事件- - -mouseup
鼠標(biāo)松開(kāi)事件處理程序:頁(yè)面移除鼠標(biāo)移動(dòng)事件- - -document.removeEventListener(‘mousemove’, move);
注意:要添加移除事件,所以給鼠標(biāo)移動(dòng)函數(shù)單獨(dú)拿出來(lái),寫(xiě)一個(gè)函數(shù)名,添加和移除事件時(shí)引用函數(shù)名就可以了
代碼示例:
<!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>
* {
margin: 0;
padding: 0;
}
a {
text-decoration: none;
color: #000;
}
.login-header {
margin: 100px auto;
height: 30px;
line-height: 30px;
font-size: 24px;
text-align: center;
}
.login {
display: none;
width: 515px;
height: 282px;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
border: 1px solid #ebebeb;
background-color: #fff;
box-shadow: 0px 0px 20px #ddd;
text-align: center;
z-index: 99999;
}
.login-title {
position: relative;
height: 50px;
line-height: 50px;
font-size: 18px;
cursor: move;
}
.close {
position: absolute;
top: 0;
right: 0;
transform: translate(50%, -50%);
width: 36px;
height: 36px;
line-height: 36px;
font-size: 12px;
border-radius: 18px;
border: 1px solid #ddd;
color: #666;
background-color: #fff;
}
.login-input-content {
margin: 10px 0;
}
.login-input label {
display: inline-block;
width: 80px;
text-align: right;
}
.login-input input {
width: 300px;
height: 40px;
margin: 10px 0;
padding-left: 10px;
border: 1px solid #ddd;
outline-color: royalblue;
}
.loginBtn a {
display: block;
width: 180px;
height: 35px;
line-height: 35px;
margin: 10px auto;
border: 1px solid #ddd;
}
.login-bg {
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="login">
<div class="login-title">
登錄會(huì)員
<span><a class="close" href="javascript:void(0);" >關(guān)閉</a></span>
</div>
<div class="login-input-content">
<div class="login-input">
<label for="username">用戶(hù)名:</label>
<input type="text" name="info[username]" id="username" placeholder="請(qǐng)輸入用戶(hù)名"><br>
</div>
<div class="login-input">
<label for="password">登錄密碼:</label>
<input type="password" name="info[password]" id="password" placeholder="請(qǐng)輸入登錄密碼"><br>
</div>
</div>
<div type="submit" value="登錄會(huì)員" class="loginBtn"><a href="javascript:void(0);" >登錄會(huì)員</a></div>
</div>
<!-- 遮蓋層 -->
<div class="login-bg"></div>
<script>
var link = document.querySelector('#link');
var login = document.querySelector('.login');
var loginBg = document.querySelector('.login-bg');
var close = document.querySelector('.close');
var loginTitle = document.querySelector('.login-title');
link.addEventListener('click', function() {
login.style.display = 'block';
loginBg.style.display = 'block';
})
close.addEventListener('click', function() {
login.style.display = 'none';
loginBg.style.display = 'none';
})
loginTitle.addEventListener('mousedown', function(e) {
// 計(jì)算出鼠標(biāo)按下時(shí),鼠標(biāo)在模態(tài)框中的位置
var x = e.pageX - login.offsetLeft;
var y = e.pageY - login.offsetTop;
// 給移動(dòng)的模態(tài)框賦值位置
function move(e) {
login.style.left = e.pageX - x + 'px';
login.style.top = e.pageY - y + 'px';
}
// 添加鼠標(biāo)事件,按下鼠標(biāo)時(shí)模態(tài)框跟著鼠標(biāo)移動(dòng)
document.addEventListener('mousemove', move);
// 鼠標(biāo)松開(kāi)時(shí),模態(tài)框停止移動(dòng)
document.addEventListener('mouseup', function() {
document.removeEventListener('mousemove', move);
})
})
</script>
</body>
</html>頁(yè)面效果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
微信小程序input抖動(dòng)問(wèn)題的修復(fù)方法
這篇文章主要給大家介紹了關(guān)于微信小程序input抖動(dòng)問(wèn)題的修復(fù)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
前端實(shí)現(xiàn)下載文件(包含壓縮包下載)方式詳細(xì)總結(jié)
這篇文章主要給大家介紹了關(guān)于前端實(shí)現(xiàn)下載文件(包含壓縮包下載)方式的相關(guān)資料,這段時(shí)間項(xiàng)目需要下載文件,所以這里給大家總結(jié)下,需要的朋友可以參考下2023-09-09
基于JavaScript實(shí)現(xiàn)隨機(jī)顏色輸入框
這篇文章主要介紹了基于JavaScript實(shí)現(xiàn)隨機(jī)顏色輸入框的實(shí)例代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),需要的朋友參考下吧2016-12-12
js監(jiān)聽(tīng)滾動(dòng)條滾動(dòng)事件使得某個(gè)標(biāo)簽內(nèi)容始終位于同一位置
js如何監(jiān)聽(tīng)滾動(dòng)條滾動(dòng)事件,使得某個(gè)標(biāo)簽內(nèi)容始終位于同一位置,下面有個(gè)不錯(cuò)的示例,大家可以參考下2014-01-01
前端使用JS內(nèi)置Blob實(shí)現(xiàn)下載各種形式的文件實(shí)例
通過(guò)使用JavaScript我們可以很方便地實(shí)現(xiàn)文件的下載功能,這篇文章主要給大家介紹了關(guān)于前端使用JS內(nèi)置Blob實(shí)現(xiàn)下載各種形式文件的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06
JS簡(jiǎn)單測(cè)試循環(huán)運(yùn)行時(shí)間的方法
這篇文章主要介紹了JS簡(jiǎn)單測(cè)試循環(huán)運(yùn)行時(shí)間的方法,涉及針對(duì)javascript中for循環(huán)、for...in循環(huán)及foreach循環(huán)的相關(guān)使用方法及運(yùn)行時(shí)間測(cè)試,需要的朋友可以參考下2016-09-09
詳解如何通過(guò)JavaScript實(shí)現(xiàn)函數(shù)重載
這篇文章主要為大家詳細(xì)介紹了如何通過(guò)JavaScript實(shí)現(xiàn)函數(shù)重載,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)JavaScript有一定的幫助,感興趣的可以了解一下2023-01-01
layer子層給父層頁(yè)面元素賦值,以達(dá)到向父層頁(yè)面?zhèn)髦档男Ч麑?shí)例
下面小編就為大家?guī)?lái)一篇layer子層給父層頁(yè)面元素賦值,以達(dá)到向父層頁(yè)面?zhèn)髦档男Ч麑?shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
JS實(shí)現(xiàn)iframe自適應(yīng)高度的方法(兼容IE與FireFox)
這篇文章主要介紹了JS實(shí)現(xiàn)iframe自適應(yīng)高度的方法,涉及javascript與iframe交互動(dòng)態(tài)操作頁(yè)面元素屬性的相關(guān)技巧,需要的朋友可以參考下2016-06-06

