当前位置:网站首页>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>
边栏推荐
- Technology Review: Interpretation of cloud native architecture trend in 2022
- Some common errors and solutions of using SAP ui5 to consume OData services
- Go Concurrency
- Interview experience - list of questions
- What is SAP sup - Sybase unwired platform
- D compile time reflection
- SAP Fiori tools and corresponding cli (command line interface)
- SAP ui5 beginner tutorial 25 - using proxy server to solve the cross domain problem of SAP ui5 application accessing remote OData service trial version
- The simplest way to tell you is to hash and not hash
- Introduction to the main features of kyma when the cloud native application runs
猜你喜欢
Linus' speech recordings, which were lost in 1994, were made public

【LeetCode】40. Combined summation II (2 strokes of wrong questions)

Double recursion in deep analysis merge sort
Kubevela v1.2 release: the graphical operation console velaux you want is finally here!

Timed thread pool
MySQL transaction learning notes (I) first encounter
Technology inventory: past, present and future of Message Oriented Middleware
[kicad image] download and installation
Go quiz: considerations for function naming return value from the go interview question (more than 80% of people answered wrong)
Part 33 of SAP ui5 application development tutorial - trial version of responsiveness of SAP ui5 applications
随机推荐
Guava common collection tool classes
Synchonized introduction
Vscode voice notes to enrich information (Part 1)
SAP Fiori tools and corresponding cli (command line interface)
Semantic segmentation cvpr2020 unsupervised intra domain adaptation for semantic segmentation through self supervision
Vscode voice notes to enrich information (medium)
How the sap ui5 framework performs single step debugging of batch requests
Create a complete binary tree in array order
CVPR2021-Semi-supervised Domain Adaptation based on Dual-level Domain Mixing for Semantic Segmentati
Guava-IO
Jenkins installation and configuration
Simple student management system
Go implements LRU cache
The k-th node of the binary search tree [sword finger offer]
Interviewer: what is an iterator? What is the relationship between async await and iterators?
Go Basics
SAP ui5 Application Development Tutorial Part 30 - parameter transfer in the routing process of SAP ui5
Timed thread pool
Day19 (variable parameter, enhanced for loop traversal, generic wildcard <? >, TreeSet, linkedhashset, nested traversal of sets, set set, static import,)
Which securities company is good for opening a mobile account? Is it safe to open a mobile account?