当前位置:网站首页>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
边栏推荐
- 正大留4的主账户信息汇总
- Ten thousand volumes are known to all, and one page of a book is always relevant. TVP reading club will take you through the reading puzzle!
- Deep understanding of lambda expressions
- Wechat applet map annotation
- Thinkphp Kernel wo system source Commercial Open source multi - user + multi - Customer Service + SMS + email notification
- ThinkPHP kernel work order system source code commercial open source version multi user + multi customer service + SMS + email notification
- 【c语言】动态规划---入门到起立
- AcrelEMS高速公路微电网能效管理平台与智能照明解决方案智慧点亮隧道
- 6月书讯 | 9本新书上市,阵容强大,闭眼入!
- Landing guide for "prohibit using select * as query field list"
猜你喜欢
Play with concurrency: what's the use of interruptedexception?
Record the bug of unity 2020.3.31f1 once
Mysql表insert中文变?号的问题解决办法
Alibaba cloud polkit pkexec local rights lifting vulnerability
win10 磁盘管理 压缩卷 无法启动问题
面试会问的 Promise.all()
Starting from the classification of database, I understand the map database
One step implementation of yolox helmet detection (combined with oak intelligent depth camera)
cs架构下抓包的几种方法
Playing with concurrency: what are the ways of communication between threads?
随机推荐
Why can't you remember when reading? Why can't you remember- My technology learning methodology
60后关机程序
Pytoch yolov5 runs bug solution from 0:
正大美欧4的主账户关注什么数据?
Markdown edit syntax
Unit testing classic three questions: what, why, and how?
10 minutes to understand CMS garbage collector in JVM
Unity particle Foundation
CorelDRAW graphics suite2022 free graphic design software
office_ Delete the last page of word (the seemingly blank page)
Analyze the space occupied by the table according to segments, clusters and pages
I sorted out some basic questions about opencv AI kit.
Pytorch---使用Pytorch进行鸟类的预测
How much can a job hopping increase? Today, I saw the ceiling of job hopping.
Websites that it people often visit
Pit encountered in win11 pytorch GPU installation
Pytorch yolov5 exécute la résolution de bogues à partir de 0:
Common locks in MySQL
Keil compilation code of CY7C68013A
Let正版短信测压开源源码