当前位置:网站首页>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
- Global and Chinese market of two in one notebook computers 2022-2028: Research Report on technology, participants, trends, market size and share
- First knowledge of database
- Explore the internal mechanism of modern browsers (I) (original translation)
- Do you really know how old you are?
- In 2021, the global general crop protection revenue was about $52750 million, and it is expected to reach $64730 million in 2028
- Commands related to files and directories
- Cap and base theory
- MySQL master-slave synchronization principle
- Typora charges, WTF? Still need support
猜你喜欢

Vscode reports an error according to the go plug-in go get connectex: a connection attempt failed because the connected party did not pro

thrift go

In 2021, the global foam protection packaging revenue was about $5286.7 million, and it is expected to reach $6615 million in 2028

Basic knowledge of dictionaries and collections

Derivation of decision tree theory

Phpstudy set LAN access

Based on laravel 5.5\5.6\5 X solution to the failure of installing laravel ide helper

强基计划 数学相关书籍 推荐

MySQL master-slave synchronization principle

2.5 conversion of different data types (2)
随机推荐
What is the difference between a kill process and a close process- What are the differences between kill process and close process?
Micro service knowledge sorting - three pieces of micro Service Technology
MySQL dump - exclude some table data - MySQL dump - exclude some table data
2.2 integer
The simplicity of laravel
2.6 formula calculation
FPGA learning notes: vivado 2019.1 project creation
Basic knowledge of dictionaries and collections
LabVIEW training
JMeter plug-in installation
Parental delegation mechanism
4. Data splitting of Flink real-time project
Change deepin to Alibaba image source
The global industrial design revenue in 2021 was about $44360 million, and it is expected to reach $62720 million in 2028. From 2022 to 2028, the CAGR was 5.5%
Blue Bridge Cup: the fourth preliminary - "simulated intelligent irrigation system"
Wargames study notes -- Leviathan
4. Data binding
Sightseeing - statistics of the number of shortest paths + state transfer + secondary small paths
Global and Chinese market of full authority digital engine control (FADEC) 2022-2028: Research Report on technology, participants, trends, market size and share
Virtual machine installation deepin system