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

相关推荐
kyriewen几秒前
我用AI把公司10万行代码屎山重构了,CTO看了代码后说:你提前转正
前端·javascript·ai编程
ttwuai3 分钟前
XYGo Admin 菜单与路由:Vue3 动态路由 + GoFrame 权限菜单的完整实现方案
前端·vue·后台框架
程序员码歌11 分钟前
OpenSpec 到 Superpowers:AI 编码从说清到做对
android·前端·人工智能
爱编程的小新☆12 分钟前
LangGraph4j工作流框架
前端·数据库·ai·langchain·langgraph4j
@PHARAOH28 分钟前
HOW - 构建一个轻量前后端一体服务
前端·微服务·服务端
无限进步_39 分钟前
【C++】C++11的类功能增强与STL变化
java·前端·数据结构·c++·后端·算法
一只小小Java40 分钟前
Echarts单表多图实现
前端·javascript·echarts
跟着珅聪学java40 分钟前
Element UI 的 Tabs 标签页开发教程
javascript·vue.js·elementui
dunky1 小时前
Spring AI 深度解析:把 LLM 抽象成 Spring Bean 的底层逻辑
前端
星栈1 小时前
Rust WASM 文件上传全链路:从浏览器到 S3,一个字节都不能少
前端·前端框架·开源