当前位置:网站首页>[positioning in JS]
[positioning in JS]
2022-07-05 03:33:00 【renrenrenrenqq】
js Middle positioning
1. Mouse events
clientX: When a mouse event occurs ( Whether it's onclick, still omousemove,onmouseover etc. ), Mouse relative to browser ( This is the effective area of the browser )x Position of the shaft ;
clientY: When a mouse event occurs , Mouse relative to browser ( This is the effective area of the browser )y Position of the shaft ;
screenX: When a mouse event occurs , The mouse is relative to the display screen x Position of the shaft ;
screenY: When a mouse event occurs , The mouse is relative to the display screen y Position of the shaft ;
offsetX: When a mouse event occurs , The mouse is relative to the event source x Position of the shaft
offsetY: When a mouse event occurs , The mouse is relative to the event source y Position of the shaft
pageX: When a mouse event occurs , Mouse relative to browser x Position of the shaft
pagey: When a mouse event occurs , Mouse relative to browser Y Position of the shaft
for instance
<script>
let div= document.querySelector('div')
div.addEventListener('click',function(event){
console.log('screenX:',event.screenX)
console.log('clientX:',event.clientX)
console.log('offsetX:',event.offsetX)
console.log('pageX:',event.pageX)
})
</script>
Exhibition :
If the page does not have a scroll bar client value =page value
The following figure shows the values with scroll bars
client The value is the viewport size
page The value is the browser size
2. Application
1. A picture appears at the mouse click
img style="left:0px;top: 0px;" src="./html-01/images/ house .png" alt=""> -->
<!-- <script>
let img=document.querySelector('img')
document.onclick=function(event){
img.style.display='block'
}
document.onmousemove=function(event){
img.style.left=event.offsetX+'px'
img.style.top=event.offsetY+'px'
}
</script> -->
2. Animation appears when the mouse is dragged
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#img{
position: absolute;
}
body{
height: 1000px;
width: 1000px;
}
</style>
</head>
<body>
<img id="img" src="./html-01/images/ house .png" alt="">
<script src="common.js"></script>
<script>
var img=document.getElementById("img");
document.onmousemove = function(event){
// Solve compatibility problems
event = event||window.event;
// Get the coordinates of the mouse
//client Visible window coordinates
// var X=event.clientX;
// var Y=event.clientY;
//div The offset of is relative to the whole page
// var X=event.pageX;//IE8 Do not apply
// var Y=event.pageY;
// Firefox and other browsers think that the browser's scroll bar is html Of
var st=document.body.scrollTop||document.documentElement.scrollTop;
var sl=document.body.scrollLeft||document.documentElement.scrollLeft;
var X=event.clientX;
var Y=event.clientY;
// Set picture coordinates
img.style.left=X+sl+"px";
img.style.top=Y+st+"px";
}
</script>
</body>
</html>
边栏推荐
- 【软件逆向-分析工具】反汇编和反编译工具
- Pdf things
- Why is this an undefined behavior- Why is this an undefined behavior?
- Huawei MPLS experiment
- Tiny series rendering tutorial
- Leetcode42. connect rainwater
- Devtools的简单使用
- Anchor free series network yolox source code line by line explanation Part 2 (a total of 10, ensure to explain line by line, after reading, you can change the network at will, not just as a participan
- 2021 Li Hongyi machine learning (1): basic concepts
- [learning notes] month end operation -gr/ir reorganization
猜你喜欢
Linux安装Redis
How to learn to get the embedding matrix e # yyds dry goods inventory #
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
1. Five layer network model
Basic knowledge of tuples
Leetcode92. reverse linked list II
Qrcode: generate QR code from text
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
Why are there fewer and fewer good products produced by big Internet companies such as Tencent and Alibaba?
Share the newly released web application development framework based on blazor Technology
随机推荐
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
[groovy] string (string splicing | multi line string)
What is the most effective way to convert int to string- What is the most efficient way to convert an int to a String?
Class inheritance in C #
Why are there fewer and fewer good products produced by big Internet companies such as Tencent and Alibaba?
Flex flexible layout
this+闭包+作用域 面试题
Hot knowledge of multithreading (I): introduction to ThreadLocal and underlying principles
Kubernetes - identity and authority authentication
有個疑問 flink sql cdc 的話可以設置並行度麼, 並行度大於1會有順序問題吧?
Anchor free series network yolox source code line by line explanation four (a total of ten, ensure line by line explanation, after reading, you can change the network at will, not just as a participan
問下,這個ADB mysql支持sqlserver嗎?
Sqoop installation
Azkaban installation and deployment
Tiny series rendering tutorial
[Chongqing Guangdong education] 2777t green space planning reference questions of National Open University in autumn 2018
Devtools的简单使用
[2022 repair version] community scanning code into group activity code to drain the complete operation source code / connect the contract free payment interface / promote the normal binding of subordi
[groovy] string (string injection function | asBoolean | execute | minus)
Kubernetes -- cluster expansion principle