【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 事件 ;

相关推荐
brzhang10 小时前
MCP A2A Skills 这三个词搞懂了 再去写你的智能体
前端·后端·架构
灰灰勇闯IT10 小时前
RN原生模块交互:打通JS与原生的桥梁
开发语言·javascript·交互
LYFlied10 小时前
浅谈跨端开发:大前端时代的融合之道
前端·flutter·react native·webview·大前端·跨端开发·hybrid
LYFlied10 小时前
浅谈前端构建工具核心理解&&主流工具对比
前端·webpack·软件构建·rollup·vite·开发工具·工程化
weixin_3077791310 小时前
Jenkins jQuery3 API 插件详解:赋能插件前端开发的利器
运维·开发语言·前端·jenkins·jquery
LinDon_10 小时前
【企业微信快速登录适配 Chrome/Edge 142+】
前端·chrome·企业微信
JosieBook10 小时前
【Vue】google chrome中安装vue_dev_tools.crx的时候提示“无法安装扩展程序,因为它使用了不受支持的清单版本。”
前端·vue.js·chrome
前端不太难10 小时前
RN 性能优化:列表滚动掉帧、卡顿怎么办?
前端·react native·性能优化
亿元程序员10 小时前
祖传项目二开快上线了,却还有很多旧的资源,怎么办?
前端