当前位置:网站首页>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>
边栏推荐
- Deep learning non local neural networks
- [interview with a large factory] meituan had two meetings. Was there a surprise in the end?
- Day19 (variable parameter, enhanced for loop traversal, generic wildcard <? >, TreeSet, linkedhashset, nested traversal of sets, set set, static import,)
- ThreadLocal
- Guava-IO
- Vscode voice notes to enrich information (medium)
- Go Basics
- First blog
- Day22(File,DiGui,FileOutputStream)
- CSDN cerebral palsy bug has wasted nearly two hours of hard work
猜你喜欢
SAP ui5 date type sap ui. model. type. Analysis of date parsing format
[golang] leetcode intermediate - Search rotation sort array & search two-dimensional matrix II

2022.1.21 diary
Technology inventory: Technology Evolution and Future Trend Outlook of cloud native Middleware

05 virtual machine stack

What is the use of the subprocess module

HashSet implementation class
![The k-th node of the binary search tree [sword finger offer]](/img/9c/26b508100d8b49c114cf72346a8e7c.jpg)
The k-th node of the binary search tree [sword finger offer]

Array and simple function encapsulation cases

Get the first letter of Chinese phonetic alphabet in Excel and capitalize it
随机推荐
MySQL tuning -- 02 -- slow query log
Vscode voice notes to enrich information (medium)
Day19 (variable parameter, enhanced for loop traversal, generic wildcard <? >, TreeSet, linkedhashset, nested traversal of sets, set set, static import,)
Leetcode topic [array] -36- effective Sudoku
JMeter stress testing and agent recording
Monkey test of APP automation
同花顺软件究竟是什么?网上开户安全么?
Jz-066- motion range of robot
Solve some prompt codes that pychar cannot recognize selenium
C language - minesweeping
Oracle SQL statement operand: rounding, rounding, differentiation and formatting
Go Concurrency
How to add an external header file in vs?
Create a complete binary tree in array order
Day17 (set)
Use of arrays tool class
Design of IM login server and message server
MySQL uses the where condition to find strange results: solve
Day13 (inner class, anonymous inner class, API common class)
[day40 literature extensive reading] space and time in the child's mind: metallic or atomic