当前位置:网站首页>Offset related concepts + drag modal box case
Offset related concepts + drag modal box case
2022-07-03 20:25:00 【MyDreamingCode】
One 、 offset Relevant concepts
offsetLeft: Refers to the left offset of the parent element with positioning ( If none, then body Benchmarking )
offsetTop: Refers to the offset above the parent element with positioning
offsetWidth: Self width + padding + border read-only
offsetHeight: Self height + padding + border read-only
demo: Calculate when the mouse clicks on the box , Distance from the box
var box = document.querySelector('.box');
box.addEventListener('click', function(e) {
var x = e.pageX - this.offsetLeft;
var y = e.pageY - this.offsetTop;
console.log(x,y);
})
Two 、 Drag modal box
Icon : First think about how to calculate the distance that the big box needs to move :
1. When the mouse is pressed : Mouse pageX - Big box offsetLeft -> The distance of the mouse in the big box ( It doesn't change after )
2. When the mouse moves : Mouse pageX - The distance of the mouse in the big box ( I asked before )
3. When the mouse is released : To remove move event
<!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>Document</title>
<style>
* {
padding: 0;
margin: 0;
}
.bg {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, .2);
display: none;
}
button,.close {
cursor: pointer;
}
.login {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
width: 300px;
padding: 20px 0;
z-index: 1;
text-align: center;
background-color: #fff;
display: none;
}
.close {
position: absolute;
top: 0;
right: 0;
width: 20px;
height: 20px;
line-height: 20px;
font-size: 20px;
}
h2 {
margin-bottom: 15px;
cursor: move;
}
</style>
</head>
<body>
<button> Click to pop up the login box </button>
<div class="login">
<span class="close">x</span>
<h2> Sign in </h2>
user name :<input type="text" placeholder=" Please enter a user name "><br/>
password :<input type="password" name="" id="" placeholder=" Please input a password "><br/>
</div>
<div class="bg"></div>
<script>
var btn = document.querySelector('button');
var login = document.querySelector('.login');
var bg = document.querySelector('.bg');
var close = document.querySelector('.close');
var chooseArea = document.querySelector('h2');
btn.addEventListener('click', function() {
login.style.display = 'block';
bg.style.display = 'block';
})
close.addEventListener('click', function() {
login.style.display = 'none';
bg.style.display = 'none';
})
chooseArea.addEventListener('mousedown', function(e) {
var x = e.pageX - login.offsetLeft;
var y = e.pageY - login.offsetTop;
document.addEventListener('mousemove', fn);
function fn(e) {
login.style.left = e.pageX - x + 'px';
login.style.top = e.pageY - y + 'px';
}
document.addEventListener('mouseup', function() {
document.removeEventListener('mousemove', fn);
})
})
</script>
</body>
</html>
边栏推荐
- Don't be afraid of no foundation. Zero foundation doesn't need any technology to reinstall the computer system
- How to read the source code [debug and observe the source code]
- [Yu Yue education] basic reference materials of manufacturing technology of Shanghai Jiaotong University
- HCIA-USG Security Policy
- How to set the system volume programmatically- How to programmatically set the system volume?
- 3. Data binding
- Global and Chinese market of full authority digital engine control (FADEC) 2022-2028: Research Report on technology, participants, trends, market size and share
- [raid] [simple DP] mine excavation
- Test panghu was teaching you how to use the technical code to flirt with girls online on Valentine's Day 520
- String and+
猜你喜欢
Task of gradle learning
Do you really know how old you are?
你真的知道自己多大了吗?
Virtual machine installation deepin system
MySQL master-slave synchronization principle
Professional interpretation | how to become an SQL developer
2.6 formula calculation
Explore the internal mechanism of modern browsers (I) (original translation)
Research Report on the overall scale, major manufacturers, major regions, products and application segmentation of rotary tablet presses in the global market in 2022
Commands related to files and directories
随机推荐
In 2021, the global revenue of thick film resistors was about $1537.3 million, and it is expected to reach $2118.7 million in 2028
MySQL learning notes - single table query
Phpexcel import export
[Yu Yue education] basic reference materials of manufacturing technology of Shanghai Jiaotong University
Popularize the basics of IP routing
How to read the source code [debug and observe the source code]
Plan for the first half of 2022 -- pass the PMP Exam
[postgresql]postgresql custom function returns an instance of table type
JMeter plug-in installation
Global and Chinese market of high purity copper foil 2022-2028: Research Report on technology, participants, trends, market size and share
Realize user registration and login
About unregistered transfer login page
MPLS configuration
Typora charges, WTF? Still need support
From the behind the scenes arena of the ice and snow event, see how digital builders can ensure large-scale events
Viewing Chinese science and technology from the Winter Olympics (II): when snowmaking breakthrough is in progress
Global and Chinese market of liquid antifreeze 2022-2028: Research Report on technology, participants, trends, market size and share
2.5 conversion of different data types (2)
[raid] [simple DP] mine excavation
MySQL dump - exclude some table data - MySQL dump - exclude some table data