search
HomeWeb Front-endJS TutorialJavaScript Event Learning Chapter 9 Mouse Events_javascript skills

Let’s first take a look at what mouse events there are: mousedown, mouseup_and_click, dblclick, mousemove and mouseover mouseout. Then we will also explain the event attributes relatedTarget, fromElement and toElement. Finally, there are Microsoft's mouseenter and mouseleave events.

Browser compatibility issues can be viewed in the Browser Compatibility List.

Example
Here is an example. It can help to understand the following content.
mousedown, mouseup, click and dblclick are registered at this link. You can view it in the text box below. Or in a dialog box. (Please try in the original text: http://www.quirksmode.org/js/events_mouse.htm)
Mousedown,mouseup,click
If the user clicks on an element, then at least three events will be triggered, and the order is as follows:
1. mousedown, when the user presses the mouse button on this element
2. mouseup, When the user releases the mouse button on this element
3. click, occurs when both a mousedown and a mouseup are detected on this element
Usually mousedown and mouseup are more useful than click. Some browsers do not allow you to read onclick event information. And sometimes the click event does not keep up when the user makes certain actions with the mouse.
Suppose the user presses the mouse button on a link, then moves the mouse button away and releases the mouse button after moving away. Then this link only has a mousedown event. Similarly, if the user moves to the link after clicking the mouse, then the link will only mouseup. In both cases no click event occurs.
Whether this is a problem depends on user behavior. But you should register the onmousedown/up event unless you absolutely want the click to happen.
If you use a pop-up alert box, the browser may lose the track of the event and how many times it occurred, which will cause confusion. So it's better not to use that.
Dblclick
dblclick event is rarely used. If you want to use it, be sure not to register the onclick and dblclick event handlers on an HTML element. If both are registered, it is basically impossible for you to know what the user is doing.
In short, when the user double-clicks on an element, the click event always occurs before dblclick. In addition, in Netscape, the second click is always processed separately before the dblclick. Regardless, the warning box is very dangerous.
So ensuring that your click and dblclick are well separated can avoid many complications.
Mousemove
The mousemove event works well, but be aware that it may require a lot of system resources to handle all mousemove events. When the user moves the mouse one pixel, mousemove is triggered once. Even if nothing happens, long and complex functions that take a long time will affect the efficiency of the website: everything will slow down, especially on older machines.
So the best way is to register the onmousemove event when you need it, and remove it as soon as possible when not in use:

Copy code The code is as follows:
element.onmousemove = doSomething;
// later
element.onmousemove = null;

Mouseover and mouseout
Look at this example again, change to mouseover and try it. This example just adds an event handler for onmouseover on ev3. However, you will notice that the event is not only triggered on ev3 but also on ev4 or span. Before Mozilla 1.3, this was triggered whenever the mouse entered a text area.
The reason is of course event bubbling. User triggered mouseover event on ev4. There is no onmouseover event handler on this element, but there is on ev3. So when the event bubbles up to ev3, the program is executed.
Although these settings are completely correct, there is still a problem. The first issue is the goal. Assume the mouse enters ev4:
------------------------------------------------
| This is div id="ev3" |
| -------------------------------- |
| | This is div id="ev4" | |
| | -------- | | | span | | |

| | -------- | |
| ----------------------------- |
----------------------------------------
Now the target/srcElement of this event is ev4: it is the element where the event occurred, because the mouse moved to it. But when the following happens:
---------------------------------------- -
| This is div id="ev3" |
| -------------------------------- |
| | This is div id="ev4" | |
| | -------- | |
| | | span | | |
| | | ------ --> | |
| | -------- | |
| -------------------------- ---- |
-----------------------------------------
-------->: mouse movement
The target/srcElement of this event is the same. Here, the mouse still enters ev4. However you may do different things when the mouse comes from ev3 or from SPAN. So we need to know where the mouse comes from.
relatedTarget, fromElement, toElement
W3C added the relatedTarget attribute to the mouseover and mouseout events. In the mouseover event, it includes where the mouse comes from, and in the mouseout event, it includes where the mouse goes.
Microsoft also has two attributes that contain the following information:
1. fromElement refers to the element before the mouse comes. It is more useful in the case of mouseover
2. toElement indicates the element that the mouse will go to. More useful in mouseout situations.
In our first example, relatedTarget/fromElement contains a reference to ev3, in our second example it is SPAN. Now you know where the mouse comes from.
Cross-browser code
So if you want to know where the mouse is coming from in case of mouseover then:
Copy code The code is as follows:

function doSomething(e) {
if (!e) var e = window.event;
var relTarg = e.relatedTarget || e .fromElement;
}

If you want to know where the mouse is going in case of mouseout then:
Copy code The code is as follows:

function doSomething(e) {
if (!e) var e = window.event;
var relTarg = e.relatedTarget || e .toElement;
}

Mouse leaves a layer
In a layer-based navigation menu you may need to know when the mouse leaves a layer so that you can close that layer. So you register an event handler for this layer's onmouseout. Then the event bubbling will cause the onmouseout to be triggered when the mouse leaves any layer.
--------------
| Layer |.onmouseout = doSomething;
| -------- |
| | Link | -- --> We want to know about this mouseout

| -------- |
| -------- |
| | Link | |
| | ----> | but not about this one
| -------- |
--------------
-- -->: mouse movement
Another way to stop is when you move the mouse into this layer and then reaches a link, the browser registers a mouseout event on this layer. This baffles me (the mouse is still in the layer), but all browsers have no problem.
So how do we make mouseout happen when the mouse actually leaves the layer?
Copy code The code is as follows:
function doSomething(e) {
if (!e) var e = window.event;
var tg = (window.event) ? e.srcElement : e.target;
if (tg.nodeName != 'DIV') return;
var reltg = (e .relatedTarget) ? e.relatedTarget : e.toElement;
while (reltg != tg && reltg.nodeName != 'BODY')
reltg= reltg.parentNode
if (reltg== tg) return ;
// Mouseout took place when mouse actually left layer
// Handle event
}

First get the target of the event, which is the element the mouse left. If the target is not a DIV (layer), understand the end function because the mouse does not really leave the layer.
If the target is a layer, we cannot determine whether the mouse left the layer or entered a link in the layer. So you need to check the relatedTarget/toElement of the event again, which is the element to which the mouse is moved.
We read this element, and then we traverse upward through the DOM tree until the target of the event (that is, DIV), or the BODY element.
If the target we encounter is a child element of the layer, then the mouse has not left the layer. Just stop the function from running.
When the function passes all verifications we can be sure that the mouse has indeed left the layer, and we can start the appropriate action (usually hiding the layer).

Mouseenter and mouseleave
Microsoft has a solution. He added two new events mouseenter and mouseleave. It is basically the same as mouseover and mouseout except that it does not react to event bubbling. They treat the elements registered for the event as a whole block and do not react to
mouseover and mouseout that occur within the block.
So these two events also solve our problem: they only respond to mouseover/out on bound elements.
Now these two events are only supported by IE version 5.5 or above. Maybe other browsers will learn from this one day.
End
Now we have reached the end of the introduction of Event. good luck!
Original address: http://www.quirksmode.org/js/events_mouse.html
My Twitter: @rehawk
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
es6数组怎么去掉重复并且重新排序es6数组怎么去掉重复并且重新排序May 05, 2022 pm 07:08 PM

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

JavaScript的Symbol类型、隐藏属性及全局注册表详解JavaScript的Symbol类型、隐藏属性及全局注册表详解Jun 02, 2022 am 11:50 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

原来利用纯CSS也能实现文字轮播与图片轮播!原来利用纯CSS也能实现文字轮播与图片轮播!Jun 10, 2022 pm 01:00 PM

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

JavaScript对象的构造函数和new操作符(实例详解)JavaScript对象的构造函数和new操作符(实例详解)May 10, 2022 pm 06:16 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

JavaScript面向对象详细解析之属性描述符JavaScript面向对象详细解析之属性描述符May 27, 2022 pm 05:29 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

javascript怎么移除元素点击事件javascript怎么移除元素点击事件Apr 11, 2022 pm 04:51 PM

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

整理总结JavaScript常见的BOM操作整理总结JavaScript常见的BOM操作Jun 01, 2022 am 11:43 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

foreach是es6里的吗foreach是es6里的吗May 05, 2022 pm 05:59 PM

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software