javascript實(shí)現(xiàn)登錄框拖拽
本文實(shí)例為大家分享了javascript實(shí)現(xiàn)登錄框拖拽的具體代碼,供大家參考,具體內(nèi)容如下
效果:

代碼:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
.login-header {
width: 100%;
text-align: center;
height: 30px;
font-size: 24px;
line-height: 30px;
}
ul,
li,
ol,
dl,
dt,
dd,
div,
p,
span,
h1,
h2,
h3,
h4,
h5,
h6,
a {
padding: 0px;
margin: 0px;
}
.login {
display: none;
width: 512px;
height: 280px;
position: fixed;
border: #ebebeb solid 1px;
left: 50%;
top: 50%;
background: #ffffff;
box-shadow: 0px 0px 20px #ddd;
z-index: 9999;
transform: translate(-50%, -50%);
}
.login-title {
width: 100%;
margin: 10px 0px 0px 0px;
text-align: center;
line-height: 40px;
height: 40px;
font-size: 18px;
position: relative;
cursor: move;
}
.login-input-content {
margin-top: 20px;
}
.login-button {
width: 50%;
margin: 30px auto 0px auto;
line-height: 40px;
font-size: 14px;
border: #ebebeb 1px solid;
text-align: center;
}
.login-bg {
display: none;
width: 100%;
height: 100%;
position: fixed;
top: 0px;
left: 0px;
background: rgba(0, 0, 0, .3);
}
a {
text-decoration: none;
color: #000000;
}
.login-button a {
display: block;
}
.login-input input.list-input {
float: left;
line-height: 35px;
height: 35px;
width: 350px;
border: #ebebeb 1px solid;
text-indent: 5px;
}
.login-input {
overflow: hidden;
margin: 0px 0px 20px 0px;
}
.login-input label {
float: left;
width: 90px;
padding-right: 10px;
text-align: right;
line-height: 35px;
height: 35px;
font-size: 14px;
}
.login-title span {
position: absolute;
font-size: 12px;
right: -20px;
top: -30px;
background: #ffffff;
border: #ebebeb solid 1px;
width: 40px;
height: 40px;
border-radius: 20px;
}
</style>
</head>
<body>
<div class="login-header"><a id="link" href="javascript:;">點(diǎn)擊,彈出登錄框</a></div>
<div id="login" class="login">
<div id="title" class="login-title">登錄會(huì)員
<span><a id="closeBtn" href="javascript:void(0);" class="close-login">關(guān)閉</a></span>
</div>
<div class="login-input-content">
<div class="login-input">
<label>用戶名:</label>
<input type="text" placeholder="請(qǐng)輸入用戶名" name="info[username]" id="username" class="list-input">
</div>
<div class="login-input">
<label>登錄密碼:</label>
<input type="password" placeholder="請(qǐng)輸入登錄密碼" name="info[password]" id="password" class="list-input">
</div>
</div>
<div id="loginBtn" class="login-button"><a href="javascript:void(0);" id="login-button-submit">登錄會(huì)員</a></div>
</div>
<!-- 遮蓋層 -->
<div id="bg" class="login-bg"></div>
<script>
// 1. 獲取元素
var login = document.querySelector('.login');
var mask = document.querySelector('.login-bg');
var link = document.querySelector('#link');
var closeBtn = document.querySelector('#closeBtn');
var title = document.querySelector('#title');
// 2. 點(diǎn)擊彈出層這個(gè)鏈接 link 讓mask 和login 顯示出來(lái)
link.addEventListener('click', function() {
mask.style.display = 'block';
login.style.display = 'block';
})
// 3. 點(diǎn)擊 closeBtn 就隱藏 mask 和 login
closeBtn.addEventListener('click', function() {
mask.style.display = 'none';
login.style.display = 'none';
})
// 4. 開始拖拽
// (1) 當(dāng)我們鼠標(biāo)按下, 就獲得鼠標(biāo)在盒子內(nèi)的坐標(biāo)
title.addEventListener('mousedown', function(e) {
var x = e.pageX - login.offsetLeft;
var y = e.pageY - login.offsetTop;
// (2) 鼠標(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';
}
// (3) 鼠標(biāo)彈起,就讓鼠標(biāo)移動(dòng)事件移除
document.addEventListener('mouseup', function() {
document.removeEventListener('mousemove', move);
})
})
</script>
</body>
</html>注意:mousemove和mouseup應(yīng)該在mousedown里面,并且目標(biāo)對(duì)象是document,而不是title。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 原生JS實(shí)現(xiàn)可拖拽登錄框
- Javascript實(shí)現(xiàn)登錄框拖拽效果
- js實(shí)現(xiàn)登錄框鼠標(biāo)拖拽效果
- js實(shí)現(xiàn)百度登錄框鼠標(biāo)拖拽效果
- 百度Popup.js彈出框進(jìn)化版 拖拽小框架發(fā)布 兼容IE6/7/8,Firefox,Chrome
- javascript 網(wǎng)頁(yè)編輯框及拖拽圖片的問(wèn)題
- js實(shí)現(xiàn)彈出框的拖拽效果實(shí)例代碼詳解
- 使用純JS實(shí)現(xiàn)checkbox的框選效果(鼠標(biāo)拖拽多選)
- JavaScript實(shí)現(xiàn)模態(tài)框拖拽效果
- HTML+CSS+JavaScript實(shí)現(xiàn)可拖拽模態(tài)框
相關(guān)文章
微信小程序列表時(shí)間戳轉(zhuǎn)換實(shí)現(xiàn)過(guò)程解析
這篇文章主要介紹了微信小程序列表時(shí)間戳轉(zhuǎn)換實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
使用JavaScript實(shí)現(xiàn)一個(gè)簡(jiǎn)單的待辦事項(xiàng)列表todo-list
這篇文章主要為大家詳細(xì)介紹了如何使用JavaScript實(shí)現(xiàn)一個(gè)簡(jiǎn)單的待辦事項(xiàng)列表todo-list,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下2024-04-04
學(xué)習(xí)JavaScript正則表達(dá)式
這篇文章主要介紹了JavaScript正則表達(dá)式,詳細(xì)深入的了解JavaScript正則表達(dá)式,從而更熟練掌握J(rèn)avaScript正則表達(dá)式,感興趣的小伙伴們可以參考一下2015-11-11
JS promise解決異步問(wèn)題過(guò)程詳解
Promise是異步編程的一種解決方案,比傳統(tǒng)的解決方案——回調(diào)函數(shù)和事件——更合理和更強(qiáng)大。簡(jiǎn)單說(shuō)就是一個(gè)容器,里面保存著某個(gè)未來(lái)才會(huì)結(jié)束的事件(通常是一個(gè)異步操作)的結(jié)果。從語(yǔ)法上說(shuō),Promise是一個(gè)對(duì)象,從它可以獲取異步操作的消息2023-04-04
使用layer彈窗提交表單時(shí)判斷表單是否輸入為空的例子
今天小編就為大家分享一篇使用layer彈窗提交表單時(shí)判斷表單是否輸入為空的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09
使用javascript實(shí)現(xiàn)json數(shù)據(jù)以csv格式下載
這篇文章主要介紹了使用javascript實(shí)現(xiàn)json數(shù)據(jù)以csv格式下載,需要的朋友可以參考下2015-01-01
location對(duì)象的屬性和方法應(yīng)用(解析URL)
本文將與大家分享下location對(duì)象使用屬性和方法來(lái)解析URL的實(shí)例,感興趣的朋友可以參考下,希望對(duì)你有所幫助2013-04-04

