How to determine whether the mouse is in the DIV area _ javascript skills

Source: Internet
Author: User
You can use mouseover or mouseout to trigger an event to determine whether the mouse is in this area. However, the limitation of this method is that it is necessary to trigger mouseover or mouseout, And the mouseleave event can only be known if this problem has been studied today.

Method 1:

You can use mouseover or mouseout to trigger an event to determine whether the mouse is in this area. However, the limitation of this method is that the mouseover, or mouseout, mouseleave event must be triggered to be known.

The Code is as follows:


Function chkIn ()
{
P_1.innerText = "now you have moved your mouse to the layer! ";
P_1.style.font = "normal black ";
}
Function chkOut ()
{
P_1.innerText = "now you have moved the mouse out of the layer! ";
P_1.style.font = "bold red ";
}


The Code is as follows:


OnMouseOver = "chkIn ()" onMouseOut = "chkOut ()"> This is a DIV



Method 2:

The Code is as follows:


Function checkIn (e ){
Var x = window. event. clientX;
Var y = window. event. clientY;
Var str = '';
For (I = 0; I Var obj = document. body. children [I];
If (x> obj. offsetLeft
& X <(obj. offsetLeft + obj. clientWidth)
& Y> obj. offsetTop
& Y <(obj. offsetTop + obj. clientHeight )){
Str + =' <鼠标位于层 '+obj.id+ '范围之内> \ N ';
} Else {
Str + =' <鼠标位于层 '+obj.id+ '范围之外> \ N ';
}
}
Alert (str );
}
Document. onclick = checkIn


Method 3:

This method is the simplest and practical.

The Code is as follows:


If (myDiv. contains (window. event. srcElement ))


That is, if (myDiv. contains (the element object at the mouse position ))

The specific situation should be selected based on your own needs. I debugged method 3, but it was not used. Other methods, continue to study.

Related Article

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.