代碼實作:
<!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 >點擊</div>
<div ></div>
<div >
<div >
<div >×</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,關閉對話框
close.addEventListener('click', function() {
bg.style.display = 'none';
login.style.display = 'none';
});
hd.addEventListener('mousedown', function(e) {
// 滑鼠按下對話框頂部時,獲取滑鼠到對話框的距離
var x = e.pageX - login.offsetLeft;
var y = e.pageY - login.offsetTop;
// 滑鼠按下并移動時,實時更新對話框的位置
document.addEventListener('mousemove', move);
function move(e) {
login.style.left = e.pageX - x + 'px';
login.style.top = e.pageY - y + 'px';
}
// 滑鼠松開時,移除拖拽的動作
document.addEventListener('mouseup', function() {
document.removeEventListener('mousemove', move);
});
});
</script>
</body>
</html>
實作效果:
點擊點擊按鈕,彈出對話框,
按住對話框頂部并移動,實作拖動效果,
點擊對話框右上角×,關閉對話框,

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/168778.html
標籤:其他
下一篇:js生成條形碼
