当前位置:网站首页>Mouse events in JS
Mouse events in JS
2022-07-02 04:39:00 【Da Da Q】
click: Click events .
dblclick: Double-click the event .
mousedown: Trigger when mouse button is pressed .
mouseup: Triggered when the pressed mouse button is released .
mousemove: Mouse movement events .
mouseover: Move in event .
mouseout: Remove Events .
mouseenter: Move in event .
mouseleave: Remove Events .
contextmenu: Right click events .
mouseover Events and mouseenter event , It is triggered when the mouse enters a node . The difference between the two ,mouseenter The event is triggered only once , As long as the mouse moves inside the node ,mouseover The event will trigger multiple times on the child node .
var div = document.getElementById("div")
var p = document.getElementById("p")
div.onmouseover=function(){
console.log("div")
}
p.onmouseover=function(){
console.log("p")
} // bubbling phase
div.onmouseenter=function(){
console.log("div")
}
p.onmouseenter=function(){
console.log("p")
} // Capture phase
Mouse event properties
MouseEvent.altKey
MouseEvent.ctrlKey
MouseEvent.metaKey
MouseEvent.shiftKey
Respectively represents when the mouse event occurs , Whether the corresponding keyboard key is pressed . Return value is Boolean .
document.body.onclick=function(e){
e=e||window.event
console.log("altKey:"+e.altKey); // Whether to press alt key
console.log("ctrlKey:"+e.ctrlKey); // Whether to press Ctrl key
console.log("metaKey:"+e.metaKey); // Whether to press meta key
console.log("shiftKey:"+e.shiftKey); // Whether to press shift key
}
MouseEvent.button Property returns a value , Indicates which mouse button was pressed when the event occurred .
0 For the left button
1 Represents the middle key
2 Right click
document.body.onmousedown=function(e){
e=e||window.event
console.log(e.button)
}
MouseEvent.clientX,MouseEvent.clientY
MouseEvent.clientX Property returns the horizontal coordinate of the mouse position relative to the upper left corner of the browser window ,
MouseEvent.clientY Property returns the vertical coordinate of the mouse position relative to the upper left corner of the browser window ,
Both properties are read-only .
document.body.onmousedown=function(e){
e=e||window.event
console.log(e.clientX,e.clientY)
}
MouseEvent.offsetX,MouseEvent.offsetY
MouseEvent.offsetX Property returns the horizontal distance between the mouse position and the left edge of the event object ,
MouseEvent.offsetY Property returns the vertical distance between the mouse position and the left edge of the event object ,
Both properties are read-only .
div.onclick=function(e){
e=e||window.event
console.log(e.offsetX,e.offsetY)
// When the mouse event is triggered , The distance between the current mouse position and the upper left corner of the target node
}
MouseEvent.pageX,MouseEvent.pageY
MouseEvent.pageX Property returns the distance between the mouse position and the left edge of the document ,
MouseEvent.pageY Property returns the distance from the mouse position to the top of the document .
Both properties are read-only .
document.body.onclick=function(e){
e=e||window.event
console.log("pageY:"+e.pageY) // From the top of the document
console.log("clientY:"+e.clientY) // From the top of the visual window
}
MouseEvent.movementX,MouseEvent.movementY( understand )
MouseEvent.movementX Property returns the previous mousemove Events and current mousemove Horizontal displacement between events ,
MouseEvent.movementY Property returns the previous mousemove Events and current mousemove The vertical distance of the event .
Both properties are read-only .
e.screenX,e,screenY
Horizontal and vertical distance from the screen
Mouse wheel events
The scroll event in Firefox is DOMMouseScroll, In other browsers onmousewheel.
Roll up and down and save to e.detail Inside
firefox :e.detail Drop too Oh
Scroll up to return a value greater than 0
Scroll down to return a value less than 0
Non Firefox :e.wheelDelta Take special
Scroll up to return a value less than 0
Scroll down to return a value greater than 0
function wheelEvent(e){
e=e||window.event
if(e.detail){ // Judge whether to support e.detail If you support it, it means Firefox
if(e.detail<0){
console.log(" Scroll up ")
}else{
console.log(" Scroll down ")
}
}else{ // I won't support it , Description is other browsers
if(e.wheelDelta<0){
console.log(" Scroll down ")
}else{
console.log(" Scroll up ")
}
}
}
document.body.onmousewheel=wheelEvent
document.body.addEventListener("mousewheel",wheelEvent) // Non Firefox
document.body.addEventListener("DOMMouseScroll",wheelEvent) // firefox
边栏推荐
- LeetCode-对链表进行插入排序
- 【提高课】ST表解决区间最值问题【2】
- Analyze the space occupied by the table according to segments, clusters and pages
- geotrust ov多域名ssl證書一年兩千一百元包含幾個域名?
- Embedded-c language-8-character pointer array / large program implementation
- Common sense of cloud server security settings
- win11安装pytorch-gpu遇到的坑
- Cannot activate CONDA virtual environment in vscode
- Introduction to Luogu 3 [circular structure] problem list solution
- 第十六周作业
猜你喜欢

CorelDRAW Graphics Suite2022免费图形设计软件

Deeply understand the concepts of synchronization and asynchrony, blocking and non blocking, parallel and serial

Federal learning: dividing non IID samples according to Dirichlet distribution

How much can a job hopping increase? Today, I saw the ceiling of job hopping.

阿里云polkit pkexec 本地提权漏洞

Pytorch---使用Pytorch实现U-Net进行语义分割
![[understand one article] FD_ Use of set](/img/57/276f5ef438adee2cba31dceeabb95c.jpg)
[understand one article] FD_ Use of set

Thinkphp Kernel wo system source Commercial Open source multi - user + multi - Customer Service + SMS + email notification

Embedded-c language-9-makefile/ structure / Consortium

FAQ | FAQ for building applications for large screen devices
随机推荐
Research on the security of ognl and El expressions and memory horse
二叉树解题(一)
正大留4的主账户信息汇总
What methods should service define?
Markdown edit syntax
Markdown编辑语法
CY7C68013A之keil编译代码
Play with concurrency: what's the use of interruptedexception?
Pytoch --- use pytoch to predict birds
BGP experiment the next day
The solution to the complexity brought by lambda expression
Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products
Idea autoguide package and autodelete package Settings
VMware installation win10 reports an error: operating system not found
How to modify data file path in DM database
Play with concurrency: draw a thread state transition diagram
洛谷入门3【循环结构】题单题解
Common sense of cloud server security settings
10 minutes to understand CMS garbage collector in JVM
C language guessing numbers game