jQuery hover to handle mouse enter and leave events

jQuery hover to handle mouse enter and leave events

jQuery hover binds handlers for both mouse enter 'mouseEnter' and mouse leave 'mouseLeave' events.

.hover(over, out)

jQuery hover event handler to update div background color

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>jQuery hover method</title>
    <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("div").hover(function () {
                $(this).css("background-color", "red");
            });
        });
    </script>
</head>
<body>
    <div style="height:200px;width:200px;background-color:blue;color:white">
        Mouse move in this area
    </div>
</body>
</html>
Output:
no mouse move events output
mouse move events output

Move the mouse on "Move mouse in this area" div and see the div background color changes from blue to red.

How to handle mouse enter and mouse leave events in common handler ?

This jQuery code is used to handle the common functionality to display the page positions x and y when mouse enter on the div area and leave on the div area.

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>jQuery hover method</title>
    <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("div").hover(function (event) {
                $(this).text("x: " + event.pageX + " y: " + event.pageY);
            });
        });
    </script>
</head>
<body>
    <div style="height:200px;width:200px;background-color:blue;color:white">
        Mouse move in this area
    </div>
</body>
</html>
Output:
no mouse move events output
mouse move events output

Move the mouse on "Move mouse in this area" div to get the mouse positions inside div and leave the div to see the outside mouse positions.

Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^