Article directory
- Event class for MouseEvent
- Block the default events of the mouse
- Remove the right-click menu
- Prevent images from being dragged by default
- Prevent text dragging and selection
- Prevent form submission and reset
- Print out the contents of the MouseEvent object
- clientX and clientY with x and y
- offsetXY
- layerXY
- pageXY
- screenXY
- Summarize
Event class for MouseEvent
serial number event describe 1 mousedown
mouse down 2 mouseup
mouse release 3 click
left click 4 dblclick
left double click 5 mousemove
mouse movement 6 mouseover
mouse over 7 mouseout
mouse out 8 mouseenter
mouse enter 9 mouseleave
mouse out 10 contextmenu
right click menu The execution sequence is
mousedown
=>mouseup
=>click
mouseover
andmouseout
sub-elements will also trigger, which can be triggered by bubbling
mouseenter
andmouseleave
triggered by the listening object, preventing bubbling
Block the default events of the mouse
event.preventDefault();
event.returnValue = false;
//IE8
and the following compatible writing
return false;
//IE
Compatible writing, only used foron
events to prevent default events
Remove the right-click menu
document.body.addEventListener("contextmenu", clickHandler);
function clickHandler(event) {
// 阻止事件默认行为
event.preventDefault();
console.log(event.type);
}
Prevent images from being dragged by default
let img = document.querySelector("img");
img.addEventListener("mousedown", mouseHandler);
function mouseHandler(event) {
event.preventDefault();
}
Prevent text dragging and selection
document.body.addEventListener("mousedown", mouseHandler);
function mouseHandler(event){
event.preventDefault();
}
Prevent form submission and reset
let bn = document.querySelector("[type=submit]");
bn.addEventListener("click", clickHandler);
function clickHandler(event){
event.preventDefault();
}
Or write to the form
let form = document.querySelector("form");
form.addEventListener("submit", submitHandler);
function submitHandler(event){
event.preventDefault();
// IE8 及以下兼容写法
// event.returnValue = false;
}
Print out the contents of the MouseEvent object
document.body.addEventListener("mousedown", clickHandler); function clickHandler(event){ console.log(event); }
altKey
,ctrlKey
,shiftKey
,metaKey
whether the button is clicked
button
,buttons
,which
used to determine which button of the mouse is operated,
the value corresponding to the left button0
,1
,1
the value corresponding to the middle button1
, ,4
the2
value corresponding to the right button2
,2
,3
timeStamp
the time from the page opening to the trigger event
clientX and clientY with x and y
clientX
clientY
Same as and ,x
bothy
are client area coordinates, referring to the coordinates of the mouse, starting from the upper left corner of the browser display area,x
andy
are supported by new browsers.
offsetXY
offsetX
,offsetY
for the upper-left coordinates of the target element (event.target).
layerXY
layerX
,layerY
look up to the upper left corner of the parent element with positioning attributes (if it has positioning attributes, it is relative to itself), if there is none, it is relative to the upperbody
left corner.
When neither the element nor its parent has a positioning attribute, the body
upper left corner of is used as the origin
When the parent of the element has a positioning attribute, the upper left corner of the parent is used as the origin
When the element itself has a positioning attribute, use its upper left corner as the origin
pageXY
pageX
,pageY
the distance from the upper left corner of the page.
screenXY
screenX
,screenY
relative to the upper left corner of the screen.
Summarize
clientX
SameclientY
as and , starting from the upper left corner of the browser display area, referring to the coordinates of the mouse. and is a product of new browser support. and for the upper-left coordinates of the target element, from the start. And go up to find the upper left corner of the parent element with positioning attributes (if it has positioning attributes, it is relative to itself), if there is none, it is relative to the upper left corner. The distance from the upper left corner of the relative page. and relative to the upper left corner of the screen.x
y
x
y
offsetX
offsetY
padding
layerX
layerY
body
pageX
pageY
screenX
screenY