【Web APIs】鼠标经过、离开事件 ( mouseover、mouseout 事件 | mouseenter、mouseleave 事件 )

文章目录

  • 一、鼠标经过、离开事件
    • [1、mouseover、mouseout 事件](#1、mouseover、mouseout 事件)
    • [2、mouseenter、mouseleave 事件](#2、mouseenter、mouseleave 事件)
  • 二、代码示例
    • [1、代码示例 - mouseenter、mouseleave 事件](#1、代码示例 - mouseenter、mouseleave 事件)
    • [2、代码示例 - mouseover、mouseout 事件](#2、代码示例 - mouseover、mouseout 事件)

一、鼠标经过、离开事件


1、mouseover、mouseout 事件

mouseover 事件 : 鼠标指针 从 元素外部 移入元素本身 , 或 从 元素本身 移入其 子元素 时触发 , 支持 事件冒泡 ;

mouseout 事件 : 鼠标指针 从 元素本身 移出到外部 , 或 从 元素子元素 移出到 本身时触发 , 支持 事件冒泡 ;

鼠标 在 元素 和 其子元素 间移动 会 频繁触发 mouseover、mouseout 事件 ;

2、mouseenter、mouseleave 事件

mouseenter 事件 : 鼠标指针 仅 从 外部 移入 元素本身 时触发 , 不支持事件冒泡 , 子元素的 鼠标移动 不会触发父元素的该事件 ;

mouseleave 事件 : 鼠标指针 仅 从元素本身移出到外部时触发 , 不支持事件冒泡 , 从子元素移回父元素不会触发 ;

mouseenter、mouseleave 事件 只关注 元素自身 的 鼠标进出 , 触发 更精准、频率更低 ;

二、代码示例


1、代码示例 - mouseenter、mouseleave 事件

代码示例 :

html 复制代码
<!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>鼠标经过、离开事件</title>
    <style>
        .father {
            width: 200px;
            height: 200px;
            background-color: blue;
        }
        
        .son {
            width: 100px;
            height: 100px;
            background-color: red;
        }
    </style>
</head>

<body>
    <!-- 父容器元素 : 包含子元素son,形成DOM父子层级关系 -->
    <div class="father">
        <!-- 子元素 : 嵌套在father容器内部 -->
        <div class="son"></div>
    </div>

    <script>
        // 1. 获取页面中第一个类名为 "father" 的 DOM 元素 ( 父元素 ) 
        var father = document.querySelector('.father');
        // 2. 获取页面中第一个类名为 "son" 的 DOM 元素 ( 子元素 ) 
        var son = document.querySelector('.son');

        // 3. 为父元素绑定 mouseenter 事件 ( 不冒泡事件 ) 
        // mouseenter 特性 : 仅鼠标从外部移入 father 自身区域时触发,移入子元素不会触发
        father.addEventListener('mouseenter', function() {
            // 事件触发时,在浏览器控制台打印指定文字
            console.log('father mouseenter');
        });

        // 4. 为子元素绑定 mouseleave 事件 ( 不冒泡事件 ) 
        // mouseleave特性 : 仅鼠标从son自身区域移出到外部时触发 ( 移到father区域也算 ) 
        father.addEventListener('mouseleave', function() {
            // 【修正错误】原代码写的是son.log(),DOM元素无log方法,需用console.log
            console.log('father mouseleave');
        });
    </script>
</body>

</html>

执行结果 : mouseenter、mouseleave 事件 只关注 元素自身 的 鼠标进出 , 触发 更精准、频率更低 ; 进出 子元素 不会触发 ;

2、代码示例 - mouseover、mouseout 事件

代码示例 :

html 复制代码
<!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>鼠标经过、离开事件</title>
    <style>
        .father {
            width: 200px;
            height: 200px;
            background-color: blue;
        }
        
        .son {
            width: 100px;
            height: 100px;
            background-color: red;
        }
    </style>
</head>

<body>
    <!-- 父容器元素 : 包含子元素son , 形成DOM父子层级关系 -->
    <div class="father">
        <!-- 子元素 : 嵌套在father容器内部 -->
        <div class="son"></div>
    </div>

    <script>
        // 1. 获取页面中第一个类名为 "father" 的 DOM 元素 ( 父元素 ) 
        var father = document.querySelector('.father');
        // 2. 获取页面中第一个类名为 "son" 的 DOM 元素 ( 子元素 ) 
        var son = document.querySelector('.son');

        // 3. 为父元素 father 绑定 mouseover事件 ( 支持事件冒泡 ) 
        // mouseover特性 : 鼠标从外部移入 father 自身 , 或从 father 移入其子元素 son 时 , 都会触发该事件
        father.addEventListener('mouseover', function() {
            // 事件触发时 , 在浏览器控制台打印 "father mouseover"
            console.log('father mouseover');
        });

        // 4. 为父元素 father 绑定 mouseout 事件 
        // mouseout 特性 : 鼠标从father自身移出到外部 , 或从其子元素 son 移回 father 时 , 都会触发该事件
        father.addEventListener('mouseout', function() {
            // 事件触发时 , 在浏览器控制台打印 "father mouseout"
            console.log('father mouseout');
        });
    </script>
</body>

</html>

执行结果 : 鼠标 在 元素 和 其子元素 间移动 会 频繁触发 mouseover、mouseout 事件 ;

相关推荐
有味道的男人10 分钟前
如何使用招标网API获取项目详情?
java·服务器·前端
qq_4061761411 分钟前
深入剖析JS中的XSS与CSRF漏洞:原理、攻击与防御全指南
服务器·开发语言·前端·javascript
RFCEO12 分钟前
HTML编程 课程六、:HTML5 新增多媒体标签
前端·html·html5·多媒体标签·嵌入音频、视频、动画
yanyu-yaya20 分钟前
速学兼复习之vue3章节4
前端·vue.js·前端框架
Mr-Wanter23 分钟前
vue 数据反显时数字/字母不换行导致的样式问题
前端·javascript·vue.js
梁萌31 分钟前
vue项目从npm升级为pnpm
前端·npm·node.js
修己xj32 分钟前
CSS魔法:对话生成器与奔驰骏马的创意实现
前端·css
琹箐1 小时前
Cursor 无法使用prettier格式化
前端
觉醒大王1 小时前
如何整理文献阅读笔记? (精读与泛读)
前端·css·笔记·深度学习·自然语言处理·html·学习方法
广州华水科技1 小时前
单北斗GNSS变形监测系统在水库安全监测中的应用与发展
前端