jQuery document handle mouse move event

Introduction

Use the CSS :hover pseudo-class selector with the jQuery mousemove() to check whether the mouse is over an element.

View in separate window

<!DOCTYPE html>
<html lang="en">
<head>
<title>Detect If Hovering Over an Element in jQuery</title>
<style>
    div{/*ww  w.  jav  a2 s .c om*/
        margin: 80px;
        height: 200px;
        background: blue;
    }
</style>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$(document).ready(function(){
    $(document).mousemove(function(){
         if($("#myDiv:hover").length != 0){
            $(".hint").text("Mouse is Over the DIV Element.");
        } else{
            $(".hint").text("Mouse is Outside the DIV Element.");
        }
    });
});
</script>
</head>
<body>
    <div id="myDiv"></div>
    <p class="hint"></p>
</body>
</html>



PreviousNext

Related