mouseout离开区域后,与之重叠的部分都会触发mouseout,鼠标从子到父和鼠标从父到子都会触发,事件触发顺序是子到父.鼠标默认放在孙级,用键盘快捷键刷新网页,不会触发事件
mouseover,与之重叠的部分都会触发mouseover,鼠标从子到父和鼠标从父到子都会触发.事件触发顺序是子到父.鼠标默认放在孙级,用键盘快捷键刷新网页,会触发孙到父的所有事件
mouseleave只会触发自身离开区域的事件,不会冒泡到父级,鼠标是仅子到父,事件触发顺序是仅子元素触发.鼠标默认放在孙级,用键盘快捷键刷新网页,不会触发事件
mouseenter只会触发自身的进入区域事件,不会冒泡到父级,鼠标是仅父到子,事件触发顺序是仅子元素触发.鼠标在孙级,用键盘快捷键刷新网页,会触发父到孙的所有该事件(注意此处父孙的顺序)
结论:
mouseover和mouseout很像,都会冒泡.
平时需要用到鼠标移入移出的特效时,用enter和leave会更精准,因为他们不会影响后续
<!DOCTYPE html>
<html lang="n">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>mouse</title>
</head>
<style>
* {
margin: 0;
padding: 0;
}
</style>
<body>
<div style="height: 600px;width:500px; border: 1px solid #f09" id="aa">aaaa
<div style="width:300px;height: 500px; border: 1px solid #aca" id="bb">bbbb
<div style="width:100px;height: 200px; border: 1px solid #11a" id="cc">cccc</div>
</div>
</div>
<script>
//mouseout离开某个区域后,与之重叠的部分都会触发mouseout,鼠标从子到父和鼠标从父到子都会触发,事件触发顺序是子到父.鼠标默认放在孙级,用键盘快捷键刷新网页,不会触发事件
//mouseover,与之重叠的部分都会触发mouseover,鼠标从子到父和鼠标从父到子都会触发.事件触发顺序是子到父.鼠标默认放在孙级,用键盘快捷键刷新网页,会触发孙到父的所有事件
//mouseleave只会触发自身离开区域的事件,不会冒泡到父级,鼠标是仅子到父,事件触发顺序是仅子元素触发.鼠标默认放在孙级,用键盘快捷键刷新网页,不会触发事件
//mouseenter只会触发自身的进入区域事件,不会冒泡到父级,鼠标是仅父到子,事件触发顺序是仅子元素触发.鼠标在孙级,用键盘快捷键刷新网页,会触发父到孙的所有该事件
//结论:mouseover和mouseout很像,都会冒泡.
//平时需要用到鼠标移入移出的特效时,用enter和leave会更精准
// document.getElementById('aa').onmouseout = function () {
// console.log('==onmouseout===aaaa');
// }
// document.getElementById('bb').onmouseout = function () {
// console.log('====onmouseout==bbbb');
// }
// document.getElementById('cc').onmouseout = function () {
// console.log('==oonmouseout====cccc');
// }
// document.getElementById('aa').onmouseover = function () {
// console.log('==onmouseover===aaaa');
// }
// document.getElementById('bb').onmouseover = function () {
// console.log('====onmouseover==bbbb');
// }
// document.getElementById('cc').onmouseover = function () {
// console.log('==onmouseover====cccc');
// }
// document.getElementById('aa').onmouseleave = function () {
// console.log('==onmouseleave===aaaa');
// }
// document.getElementById('bb').onmouseleave = function () {
// console.log('===onmouseleave===bbbb');
// }
// document.getElementById('cc').onmouseleave = function () {
// console.log('===onmouseleave===cccc');
// }
document.getElementById('aa').onmouseenter = function () {
console.log('==onmouseenter===aaaa');
}
document.getElementById('bb').onmouseenter = function () {
console.log('====onmouseenter==bbbb');
}
document.getElementById('cc').onmouseenter = function () {
console.log('==onmouseenter====cccc');
}
</script>
</body>
</html>