当前位置:网站首页>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>边栏推荐
- Gym welcomes the first complete environmental document, which makes it easier to get started with intensive learning!
- 【c】 Digital bomb
- Micro service knowledge sorting - three pieces of micro Service Technology
- Golang type assertion and conversion (and strconv package)
- Exercises of function recursion
- Pat grade B 1009 is ironic (20 points)
- Phpstudy set LAN access
- 2.7 format output of values
- 19、 MySQL -- SQL statements and queries
- Line segment tree blue book explanation + classic example acwing 1275 Maximum number
猜你喜欢

Upgrade PIP and install Libraries

44. Concurrent programming theory

Derivation of decision tree theory

jvm jni 及 pvm pybind11 大批量数据传输及优化

Nerfplusplus parameter format sorting

Test panghu was teaching you how to use the technical code to flirt with girls online on Valentine's Day 520

Popularize the basics of IP routing

Preliminary practice of niuke.com (11)

2.5 conversion of different data types (2)

Task of gradle learning
随机推荐
Viewing Chinese science and technology from the Winter Olympics (II): when snowmaking breakthrough is in progress
Test panghu was teaching you how to use the technical code to flirt with girls online on Valentine's Day 520
2. Template syntax
Acquisition and transmission of parameters in automatic testing of JMeter interface
Microservice framework - frequently asked questions
Cap and base theory
AcWing 1460. Where am i?
Use of CMD command
It is discussed that the success of Vit lies not in attention. Shiftvit uses the precision of swing transformer to outperform the speed of RESNET
MDM mass data synchronization test verification
How to handle wechat circle of friends marketing activities and share production and release skills
Q&A:Transformer, Bert, ELMO, GPT, VIT
In 2021, the global revenue of syphilis rapid detection kits was about US $608.1 million, and it is expected to reach US $712.9 million in 2028
Detailed and not wordy. Share the win10 tutorial of computer reinstallation system
Vscode reports an error according to the go plug-in go get connectex: a connection attempt failed because the connected party did not pro
6. Data agent object Defineproperty method
Machine learning support vector machine SVM
From the behind the scenes arena of the ice and snow event, see how digital builders can ensure large-scale events
你真的知道自己多大了吗?
Parental delegation mechanism