当前位置:网站首页>JS to realize the encapsulation of the function of obtaining the mouse click position
JS to realize the encapsulation of the function of obtaining the mouse click position
2022-06-25 05:51:00 【The rain fell on the city with a smile】
A good function can be designed as a tool , Variables are used where changes occur [ That is, the parameters that need to be passed in by the developer ], What does not change is the logical area , So you can call... Anywhere .
Advantages of using encapsulated functions :
1. Reduce the amount of code
2. The code interface is more clear
3. It is conducive to multiple use of code
document.documentElement.scrollTop||document.body.scrollTop; This is a compatible practice , If you can't get from document Get the scrolling height of the element , From body In order to get .
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
#box{
width: 200px;
height: 200px;
background-color: green;
position: absolute;
}
</style>
<script>
function getPost(ev){
var scrollTop = document.documentElement.scrollTop||document.body.scrollTop; // Scrolling height
var scrollLeft = document.documentElement.scrollLeft||document.body.scrollLeft; // Rolling time left Value
return {x:ev.clientX+scrollLeft,y:ev.clientY+scrollTop};
}
document.onmousemove=function(ev){
var oEvent = ev||event;
var oDiv = document.getElementById('box');
var post = getPost(oEvent);
oDiv.style.left=post.x+'px';
oDiv.style.top=post.y+'px';
}
</script>
</head>
<body>
<div style="height: 1200px;">
<div id="box"></div>
</div>
</body>
</html>
边栏推荐
- How SAP ui5 device type detection device API works
- SAP ui5 tutorial for beginners part XXVI - detailed steps for using OData service with mock server trial version
- Deep analysis of recursion in quick sorting
- Voxel based and second network learning
- Go Context - Cancelation and Propagation
- SAP Fiori tools and corresponding cli (command line interface)
- Duplicate symbols for architecture i386 clang
- The locally developed SAP ui5 application is deployed to the ABAP server to perform error analysis
- Use of pytorch tensorboard
- What changes have taken place in the project file after SAP ui5 tools ran the Fiori add deploy config command
猜你喜欢
Go language map sorting (key/value sorting)

Array and simple function encapsulation cases
Kubevela v1.2 release: the graphical operation console velaux you want is finally here!

Timed thread pool

Classic usage of the sumproduct function

2022.1.21 diary
How the sap ui5 framework performs single step debugging of batch requests
Linus' speech recordings, which were lost in 1994, were made public
SAP Fiori tools and corresponding cli (command line interface)
DOM proficient? What is the difference between node and elment?
随机推荐
Learn the interface test, see it is very good, and make a note
MySQL tuning --01--- optimization steps and system performance parameters
About the definition of pointer variables (personal notes)
The locally developed SAP ui5 application is deployed to the ABAP server to perform error analysis
"APEC industry +" biov Tech talks about the cross-border of Chinese biotechnology enterprises and "Pratt & Whitney Kangyu" | apec-hub public welfare
Trouble of setting table property to null
Classic usage of the sumproduct function
Incorrect dependency of POM file
Array introduction plus example 01
Word of the Day
Introduction to sap ui5 tools
Interviewer: what is an iterator? What is the relationship between async await and iterators?
Technology Review: Interpretation of cloud native architecture trend in 2022
Is the securities account of Qiantang education safe? Is it reliable?
SAP Fiori tools and corresponding cli (command line interface)
MySQL transaction learning notes (I) first encounter
2.20 learning content
Go Concurrency
Duplicate symbols for architecture i386 clang
Pytorch- daily learning notes of some small functions involving training