How to Use jQuery’s .mouseover() Method

jQuery's .mouseover() method is used to execute some jQuery code whenever an element is "moused-over" (or, as the action is perhaps more commonly referred to, hovered upon). Really, the concept isn't much different from jQuery's .hover() method or CSS's :hover effect, and .mouseover() can be used more or less interchangeably with them.

The syntax for .mouseover() is similar to the syntax of many jQuery event methods. The mouseover is the trigger event, and the code that's within mouseover's callback function is the result of the mouseover taking place. As an example, let's say that when the mouse is moved over an h1 element, you want the color to change to blue. Here's how that would look:

$("h1").mouseover(function(){
   $(this).css("color", "blue");
})

The biggest difference between using jQuery to achieve this effect rather than CSS's hover is that with jQuery it's a lot easier to have a mouseover effect change the styling of an element other than the one that was hovered upon than it is in CSS. For example, let's say we want all of the text in the p elements to turn blue when we mouseover that same h1 tag. Here's what that code would look like:

$("h1").mouseover(function(){
   $("p").css("color", "blue");
})

In the example above, all we had to do to make the mouseover effect apply to an element other than the h1 is replace the "this" keyword with the element we wanted to apply the CSS change to (in this case, "p"). In CSS, achieving this same effect would be more complicated.



Responsive Menu
Add more content here...