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

相关推荐
于慨20 小时前
Lambda 表达式、方法引用(Method Reference)语法
java·前端·servlet
石小石Orz20 小时前
油猴脚本实现生产环境加载本地qiankun子应用
前端·架构
从前慢丶20 小时前
前端交互规范(Web 端)
前端
@yanyu66620 小时前
07-引入element布局及spring boot完善后端
javascript·vue.js·spring boot
CHU72903520 小时前
便捷约玩,沉浸推理:线上剧本杀APP功能版块设计详解
前端·小程序
GISer_Jing20 小时前
Page-agent MCP结构
前端·人工智能
王霸天20 小时前
💥别再抄网上的Scale缩放代码了!50行源码教你写一个永不翻车的大屏适配
前端·vue.js·数据可视化
小领航21 小时前
用 Three.js + Vue 3 打造炫酷的 3D 行政地图可视化组件
前端·github
@大迁世界21 小时前
2026年React大洗牌:React Hooks 将迎来重大升级
前端·javascript·react.js·前端框架·ecmascript
PieroPc21 小时前
一个功能强大的 Web 端标签设计和打印工具,支持服务器端直接打印到局域网打印机。Fastapi + html
前端·html·fastapi