当前位置:网站首页>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>边栏推荐
- Research Report on the overall scale, major manufacturers, major regions, products and application segmentation of rotary tablet presses in the global market in 2022
- How to handle wechat circle of friends marketing activities and share production and release skills
- [Yu Yue education] basic reference materials of manufacturing technology of Shanghai Jiaotong University
- The simplicity of laravel
- Popularize the basics of IP routing
- 7. Data broker presentation
- Wechat applet quick start (including NPM package use and mobx status management)
- 6. Data agent object Defineproperty method
- Camera calibration (I): robot hand eye calibration
- MPLS configuration
猜你喜欢

How to handle wechat circle of friends marketing activities and share production and release skills

MySQL master-slave synchronization principle

The 29th day of force deduction (DP topic)

Promethus

JMeter connection database

Explore the internal mechanism of modern browsers (I) (original translation)

Nerfplusplus parameter format sorting

Don't be afraid of no foundation. Zero foundation doesn't need any technology to reinstall the computer system

浅议.NET遗留应用改造

MDM mass data synchronization test verification
随机推荐
About callback function and hook function
4. Data splitting of Flink real-time project
How to read the source code [debug and observe the source code]
Node MySQL serialize cannot rollback transactions
11-grom-v2-04-advanced query
Deep search DFS + wide search BFS + traversal of trees and graphs + topological sequence (template article acwing)
Golang type assertion and conversion (and strconv package)
[Yugong series] February 2022 Net architecture class 004 ABP vNext used in WPF project
Get log4net log file in C - get log4net log file in C
[Yu Yue education] basic reference materials of manufacturing technology of Shanghai Jiaotong University
AI enhanced safety monitoring project [with detailed code]
2.3 other data types
Class loading process
Geek Daily: the system of monitoring employees' turnover intention has been deeply convinced off the shelves; The meta universe app of wechat and QQ was actively removed from the shelves; IntelliJ pla
Q&A:Transformer, Bert, ELMO, GPT, VIT
2.7 format output of values
4. Data splitting of Flink real-time project
PR notes:
Global and Chinese market of cyanuric acid 2022-2028: Research Report on technology, participants, trends, market size and share
JMeter plug-in installation