1. 当发生mousedown、mouseup、dblclick、mouseover、mouseenter、mousemove、mouseout和 mouseleave事件时, 它们的执行函数都会传递MouseEvent(鼠标事件对象)参数。
2. MouseEvent.ctrlKey事件属性
2.1. MouseEvent.ctrlKey指示当事件发生时, Ctrl键是否被按下并保持住。可返回一个布尔值, 按下状态(true), 释放状态(false)。它是一个只读属性。
3. MouseEvent.altKey事件属性
3.1. MouseEvent.altKey指示在指定的事件发生时, Alt键是否被按下并保持住了。可返回一个布尔值, 按下状态(true), 释放状态(false)。它是一个只读属性。
4. MouseEvent.shiftKey事件属性
4.1. MouseEvent.shiftKey指示当事件发生时, "Shift"键是否被按下并保持住。可返回一个布尔值, 按下状态(true), 释放状态(false)。它是一个只读属性。
5. MouseEvent.metaKey事件属性
5.1. MouseEvent.metaKey指示当事件发生时, "meta"键是否被按下并保持住。可返回一个布尔值, 按下状态(true), 释放状态(false)。它是一个只读属性。
5.2. "meta"键如下图

6. MouseEvent.clientX事件属性
6.1. MouseEvent.clientX返回当事件被触发时鼠标指针相对于浏览器窗口的水平坐标。
7. MouseEvent.clientY事件属性
7.1. MouseEvent.clientY返回当事件被触发时鼠标指针向对于浏览器窗口的垂直坐标。
8. MouseEvent.screenX事件属性
8.1. MouseEvent.screenX可返回事件发生时鼠标指针相对于屏幕的水平坐标。
9. MouseEvent.screenY事件属性
9.1. MouseEvent.screenY可返回事件发生时鼠标指针相对于屏幕的垂直坐标。
10. MouseEvent.button事件属性
10.1. MouseEvent.button可返回一个整数, 指示当事件被触发时哪个鼠标按键被点击。
10.2. 鼠标左键对应的值: 0。
10.3. 鼠标中键对应的值: 1。
10.4. 鼠标右键对应的值: 2。
11. MouseEvent.type事件属性
11.1. MouseEvent.type返回一个字符串值, 指示当前触发的事件类型。
12. MouseEvent.pageX事件属性
12.1. MouseEvent.pageX返回当事件被触发时鼠标指针相对于文档的水平坐标。
13. MouseEvent.pageY事件属性
13.1. MouseEvent.pageY返回当事件被触发时鼠标指针向对于文档的垂直坐标。
14. 例子
14.1. 代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>鼠标事件对象(MouseEvent)</title>
<style type="text/css">
div {
width: 3000px;
height: 300px;
background: pink;
}
</style>
</head>
<body>
<div id="myDiv"></div>
<script type="text/javascript">
function mymousedown(e){ // e是MouseEvent对象
console.log('mousedown [ctrlKey: ' + e.ctrlKey + ', altKey: ' + e.altKey + ', shiftKey: ' + e.shiftKey + ', metaKey: ' + e.metaKey
+ ', clientX: ' + e.clientX + ', clientY: ' + e.clientY + ', screenX: ' + e.screenX + ', screenY: ' + e.screenY
+ ', button: ' + e.button + ', type: ' + e.type + ', pageX: ' + e.pageX + ', pageY: ' + e.pageY + ']');
}
function mymouseup(e){ // e是MouseEvent对象
console.log('mouseup [ctrlKey: ' + e.ctrlKey + ', altKey: ' + e.altKey + ', shiftKey: ' + e.shiftKey + ', metaKey: ' + e.metaKey
+ ', clientX: ' + e.clientX + ', clientY: ' + e.clientY+ ', screenX: ' + e.screenX + ', screenY: ' + e.screenY
+ ', button: ' + e.button + ', type: ' + e.type + ', pageX: ' + e.pageX + ', pageY: ' + e.pageY + ']');
}
function mydblclick(e){ // e是MouseEvent对象
console.log('dblclick [ctrlKey: ' + e.ctrlKey + ', altKey: ' + e.altKey + ', shiftKey: ' + e.shiftKey + ', metaKey: ' + e.metaKey
+ ', clientX: ' + e.clientX + ', clientY: ' + e.clientY + ', screenX: ' + e.screenX + ', screenY: ' + e.screenY
+ ', button: ' + e.button + ', type: ' + e.type + ', pageX: ' + e.pageX + ', pageY: ' + e.pageY + ']');
}
function mymouseover(e){ // e是MouseEvent对象
console.log('mouseover [ctrlKey: ' + e.ctrlKey + ', altKey: ' + e.altKey + ', shiftKey: ' + e.shiftKey + ', metaKey: ' + e.metaKey
+ ', clientX: ' + e.clientX + ', clientY: ' + e.clientY + ', screenX: ' + e.screenX + ', screenY: ' + e.screenY
+ ', button: ' + e.button + ', type: ' + e.type + ', pageX: ' + e.pageX + ', pageY: ' + e.pageY + ']');
}
function mymouseenter(e){ // e是MouseEvent对象
console.log('mouseenter [ctrlKey: ' + e.ctrlKey + ', altKey: ' + e.altKey + ', shiftKey: ' + e.shiftKey + ', metaKey: ' + e.metaKey
+ ', clientX: ' + e.clientX + ', clientY: ' + e.clientY+ ', screenX: ' + e.screenX + ', screenY: ' + e.screenY
+ ', button: ' + e.button + ', type: ' + e.type + ', pageX: ' + e.pageX + ', pageY: ' + e.pageY + ']');
}
var move = 0;
function mymousemove(e){ // e是MouseEvent对象
if(move >= 1) return;
move++;
console.log('mousemove [ctrlKey: ' + e.ctrlKey + ', altKey: ' + e.altKey + ', shiftKey: ' + e.shiftKey + ', metaKey: ' + e.metaKey
+ ', clientX: ' + e.clientX + ', clientY: ' + e.clientY + ', screenX: ' + e.screenX + ', screenY: ' + e.screenY
+ ', button: ' + e.button + ', type: ' + e.type + ', pageX: ' + e.pageX + ', pageY: ' + e.pageY + ']');
}
function mymouseout(e){ // e是MouseEvent对象
console.log('mouseout [ctrlKey: ' + e.ctrlKey + ', altKey: ' + e.altKey + ', shiftKey: ' + e.shiftKey + ', metaKey: ' + e.metaKey
+ ', clientX: ' + e.clientX + ', clientY: ' + e.clientY+ ', screenX: ' + e.screenX + ', screenY: ' + e.screenY
+ ', button: ' + e.button + ', type: ' + e.type + ', pageX: ' + e.pageX + ', pageY: ' + e.pageY + ']');
}
function mymouseleave(e){ // e是MouseEvent对象
console.log('mouseleave [ctrlKey: ' + e.ctrlKey + ', altKey: ' + e.altKey + ', shiftKey: ' + e.shiftKey + ', metaKey: ' + e.metaKey
+ ', clientX: ' + e.clientX + ', clientY: ' + e.clientY + ', screenX: ' + e.screenX + ', screenY: ' + e.screenY
+ ', button: ' + e.button + ', type: ' + e.type + ', pageX: ' + e.pageX + ', pageY: ' + e.pageY + ']');
}
var myDiv = document.getElementById('myDiv');
myDiv.addEventListener('mousedown', mymousedown);
myDiv.addEventListener('mouseup', mymouseup);
myDiv.addEventListener('dblclick', mydblclick);
myDiv.addEventListener('mouseover', mymouseover);
myDiv.addEventListener('mouseenter', mymouseenter);
myDiv.addEventListener('mousemove', mymousemove);
myDiv.addEventListener('mouseout', mymouseout);
myDiv.addEventListener('mouseleave', mymouseleave);
</script>
</body>
</html>
14.2. 效果图

本文详细解析了鼠标事件如mousedown、mouseup等触发时的MouseEvent对象参数,包括ctrlKey、altKey、shiftKey、metaKey等关键属性,以及客户端与屏幕坐标、按钮状态和事件类型等。通过JavaScript代码实例展示了如何使用这些属性。
353

被折叠的 条评论
为什么被折叠?



