這篇文章將為大家詳細講解有關JavaScript如何實現可拖動模態框,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
具體內容如下
HTML代碼部分:
<style>
* {
margin: 0px;
padding: 0px;
}
.login-header {
width: 100%;
text-align: center;
height: 30px;
font-size: 24px;
line-height: 30px;
cursor: pointer;
}
.login {
display: none;
width: 500px;
height: 280px;
position: fixed;
border: 1px #ebebeb solid;
left: 50%;
top: 50%;
background: #fff;
box-shadow: 0px 0px 20px #ddd;
z-index: 999;
transform: translate(-50%,-50%);
}
.login-title {
width: 100%;
margin: 10px 0px 0px 0px;
text-align: center;
height: 40px;
line-height: 40px;
font-size: 10px;
position: relative;
cursor: move;
}
.login-title span {
position: absolute;
font-size: 12px;
right: -20px;
top: -30px;
background-color: #fff;
border: 1px #ebebeb solid;
width: 40px;
height: 40px;
border-radius: 20px;
}
.login-input-content {
margin-top: 20px;
}
.login-button {
width: 100px;
margin: 30px auto 0px auto;
line-height: 40px;
font-size: 14px;
border: 1px #ebebeb solid;
text-align: center;
}
a {
text-decoration: none;
color: #000;
}
.login-button a {
display: block;
}
.login-input input.list-input {
float: left;
line-height: 35px;
height: 35px;
width: 350px;
border: 1px #ebebeb solid;
text-indent: 5px;
}
.login-input {
overflow: hidden;
margin: 0px 0px 20px 0px;
}
.login-input label {
float: left;
width: 90px;
padding-right: 10px;
height: 35px;
line-height: 35px;
text-align: right;
font-size: 14px;
}
.login-mask {
display: none;
width: 100%;
height: 100%;
position: fixed;
top: 0px;
left: 0px;
background-color: rgba(0, 0, 0, .3);
}
</style>
</head>
<body>
<div class="login-header">點擊,彈出登錄框</div>
<div id="login" class="login">
<div id="title" class="login-title">登錄會員<span><a id="closeBtn" class="close-login" href="javascript:void(0);" >關閉</a></span></div>
<div class="login-input-content">
<div class="login-input">
<label>用戶名:</label>
<input type="text" placeholder="請輸入用戶名" id="username" class="list-input">
</div>
<div class="login-input">
<label>登錄密碼:</label>
<input type="password" placeholder="請輸入登錄密碼" id="password" class="list-input">
</div>
</div>
<div id="loginBtn" class="login-button"><a id="login-button-submit" href="javascript:void(0);" >登錄會員</a></div>
</div>
<!-- 遮罩層 -->
<div id="mask" class="login-mask"></div>JS部分:
<script>
// 1.獲取元素
var login = document.querySelector('.login');
var mask = document.querySelector('.login-mask');
var loginHeader = document.querySelector('.login-header');
var closeBtn = document.querySelector('.close-login');
var loginTitle = document.querySelector('.login-title');
// 2.點擊登錄提示 讓login和mask顯示出來;
loginHeader.addEventListener('click', function() {
login.style.display = 'block';
mask.style.display = 'block';
})
// 3.點擊關閉按鈕 隱藏login和mask;
closeBtn.addEventListener('click', function() {
login.style.display = 'none';
mask.style.display = 'none';
})
// 4.拖拽登錄框
// 4.1 鼠標按下獲得鼠標在盒子中的坐標
loginTitle.addEventListener('mousedown', function(e) {
var x = e.pageX-login.offsetLeft;
var y = e.pageY-login.offsetTop;
// 4.2 鼠標移動的時候,把鼠標在頁面中的坐標 減去 鼠標在盒子中的坐標,得到login盒子的left和top值
document.addEventListener('mousemove', move)
function move(event) {
login.style.left = event.pageX - x + 'px';
login.style.top = event.pageY - y + 'px';
}
// 4.3 鼠標松開的時候,移除移動事件
document.addEventListener('mouseup', function() {
document.removeEventListener('mousemove', move)
})
})
</script>效果演示:

思路:
給可拖動部分添加點擊事件,觸發時計算鼠標在可拖動部分中的坐標( e.pageX - box.offsetLeft ),得到 x y ,然后給 document 添加鼠標移動事件,因為當鼠標拖動模態框的時候,是在整個DOM窗口內移動的。保持鼠標與模態框的相對位置不變,所以需要計算此時的模態框的位置(e.pageX - x ),進而修改模態框位置。當鼠標彈起時,清除移動事件即可。
關于“JavaScript如何實現可拖動模態框”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。