DOM 鼠标事件树状图:
实例演示:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>DOM 鼠标事件</title>
<style>
section{
display: flex;
flex-flow: row wrap;
/* justify-content: space-between; */
}
.divGroup{
width: 200px;
height: 200px;
margin: 30px;
background-color: #FFEBCA;
text-align: center;
border: 2px solid #000;
}
</style>
</head>
<body>
<!-- <h4>onclick 和 onmouseup 一起使用时,鼠标‘左键’点击后,onmouseup执行完后立马执行onclick,但是使用鼠标‘滚轮’单击后,onmouseup会触发,onclick不会触发</h4>
<h4>onmousedown 、 onmouseenter 、onmouseout、 onmouseleave都是进入或离开时才触发,而onmousemovr是在对象内移动就触发</h4>
<h4>onmousedown 、 onmouseenter的区别: onmouseenter不支持冒泡,即在自身区域内进入子元素时不会触发,而onmousedown会触发</h4> -->
<section id="section">
<div class="divGroup" > <p style="color: #4AF2A1;">按下绿</p>
<p style="color: #5CC9F5;">单击蓝(滚轮单击无效)</p>
<p style="color: #F9BA48;">抬起黄</p>
<p style="color: #6638F0;">双击紫</p></div>
<div class="divGroup" > <p style="color: #F9BA48;">进入绿 onmouseover</p>
<p style="color: #6638F0;">离开蓝 onmouseout</p></div>
<div class="divGroup" > <p style="color: #4AF2A1;">进入黄 onmouseenter</p>
<p style="color: #5CC9F5;">离开紫 onmouseleave</p></div>
<div class="divGroup">右键变灰<br>禁止弹出菜单</div>
<div class="divGroup">滚动变绿</div>
<div class="divGroup">拖拽</div>
</section>
</body>
<script>
var divGroups = document.getElementsByClassName('divGroup');
var section = document.getElementById('section');
var count = 0;
// 鼠标按下
divGroups[0].onmousedown = function(){
this.style.backgroundColor = "#4AF2A1"
}
// 鼠标单击
divGroups[0].onclick = function(){
this.style.backgroundColor = "#5CC9F5"
console.log("onclick: " + count++) // 测试 onclick 与 onmouseup 事件先后
}
// 鼠标抬起
divGroups[0].onmouseup = function(){
this.style.backgroundColor = "#F9BA48"
console.log("onmouseup: " + count++) // 测试 onclick 与 onmouseup 事件先后
}
// 鼠标双击
divGroups[0].ondblclick = function(){
this.style.backgroundColor = "#6638F0"
}
// 鼠标进入
divGroups[1].onmouseover = function(){
this.style.backgroundColor = "#4AF2A1"
}
// 鼠标离开
divGroups[1].onmouseout = function(){
this.style.backgroundColor = "#5CC9F5"
}
// 鼠标抬起
divGroups[2].onmouseenter = function(){
this.style.backgroundColor = "#F9BA48"
}
// 鼠标离开
divGroups[2].onmouseleave = function(){
this.style.backgroundColor = "#6638F0"
}
// 鼠标右键
divGroups[3].oncontextmenu = function(event){
this.style.backgroundColor = "#9DA5AD"
event.preventDefault();
}
// 鼠标滚动
divGroups[4].onwheel = function(){
this.style.backgroundColor = "#70F735"
}
// divGroups[4].addEventListener('wheel',function(){ // IE 9+支持滚动事件, 且IE在 DOM 对象中没有 onwheel 属性, 需要使用监听 ‘wheel’ IE8+ 支持监听
// this.style.backgroundColor = "#70F735"
// })
// 鼠标拖拽
drag(divGroups[5]);
function drag(Obj){
Obj.style.top = Obj.offsetTop + 'px';
Obj.style.left = Obj.offsetLeft + 'px';
Obj.style.position = "absolute"; // 还原原始位置,脱离文档流 absolute 、fixed
Obj.style.margin = "0";
Obj.onmousedown = function(event){
originalTop = event.clientY - this.offsetTop; // 获取鼠标按下时,相对于盒子内部的Top
originalLeft = event.clientX - this.offsetLeft; // 获取鼠标按下时,相对于盒子内部的Left
// 使用document,而不用Obj,避免了鼠标快速滑动脱离对象问题
document.onmousemove = function(event){
Obj.style.top = event.clientY - originalTop + 'px'; // 获取鼠标移动结束的位置,并还原相对盒子的位置
Obj.style.left = event.clientX - originalLeft + 'px'; // 获取鼠标移动结束的位置,并还原相对盒子的位置
}
document.onmouseup = function(){ // 鼠标抬起时,停止拖拽
document.onmousemove = null;
}
}
}
// // 测试 onmouseover 和 onmouseenter 的区别
// var count_over = 0;
// var count_enter = 0;
// section.onmouseover = function(){
// console.log("count_over: " + count_over++)
// }
// section.onmouseenter = function(){
// console.log("count_enter: " + count_enter++)
// }
</script>
</html>
注意事项:
1、onclick 和 onmouseup 一起使用时,鼠标‘左键 ’点击后,onmouseup执行完后立马执行onclick,但是使用鼠标‘滚轮’单击后,onmouseup会触发,onclick不会触发
2、onmousedown 、 onmouseenter 、onmouseout、 onmouseleave都是进入或离开时才触发,而onmousemovr是在对象内移动就触发
3、onmousedown 、 onmouseenter的区别: onmouseenter不支持冒泡,即在自身区域内进入子元素时不会触发,而onmousedown会触发