The principle of jQuery MouseEnter and MouseLeave method

Source: Internet
Author: User

MouseEnter

The MouseEnter event occurs when the mouse pointer crosses an element.

Grammar

$ (selector). MouseEnter ()

Cases

The code is as follows Copy Code

$ ("P"). MouseEnter (function () {
$ ("P"). CSS ("Background-color", "#E9E9E4");
});


MouseLeave

The MouseLeave event occurs when the mouse pointer leaves an element.

Trigger MouseLeave Event

Grammar

$ (selector). MouseLeave ()

Cases

The code is as follows Copy Code

$ ("P"). MouseLeave (function () {
$ ("P"). CSS ("Background-color", "#E9E9E4");
});


Note: This event is most often used with the MouseEnter event, the MouseLeave () method triggers the MouseLeave event, or the function that runs when the MouseLeave event occurs


The impression may be that it is just a simple encapsulation of the mouseover/mouseout.

The code is as follows Copy Code

Hover:function (Fnover, fnout) {
Return This.mouseenter (Fnover). MouseLeave (Fnout | | fnover);
}

However, when we open the source will find that the function does not mouseover/mouseout figure,

What's that mouseenter/mouseleave? Let us first through a DEMO to feel!

From the DEMO we can find the Mouseenter/mouseleave bound event handler,

Only executes when the mouse pointer enters (leaves) the element, and the mouse movement within the element is ignored!

The code is as follows Copy Code
Add Relatedtarget, if necessary
if (!event.relatedtarget && fromelement) {
Event.relatedtarget = Fromelement = = Event.target? Original.toElement:fromElement;
}

In order to achieve this judgment, it is necessary to capture the elements of moving/moving the mouse in real time while the mouse is moving,

High-version browsers (chrome/firefox/ie9+) can be obtained by Event.relatedtarget,

The low version browser (IE6/7/8) can be implemented by comparing the event.fromelement to the event target.

The code is as follows Copy Code

For Mousenter/leave call the handler if related is outside the target.
Nb:no Relatedtarget If the mouse left/entered the browser window
if (!related | | (Related!== target &&!jquery.contains (target, related)) {
Event.type = Handleobj.origtype;
RET = handleObj.handler.apply (this, arguments);
Event.type = fix;
}

Finally attached mouseenter/mouseleave is triggered when handle core source code for everyone to reference,

The criteria related and target correspond to the encapsulated Event.relatedtarget and event targets.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.