42

Some CSS styles need to be applied to an element on hover, and CSS styles have to be applied using javascript/jquery directly and not through stylesheets or $(this).addClass('someStyle') because I am injecting the DOM elements into another page.

We can apply the usual css styles using

$('#some-content').css({
    marginTop: '60px',
    display: 'inline-block'
});

How should we add the CSS styles for :hover events?


Do we have to resort to:

$('#some-content').hover(
       function(){ $(this).css('display', 'block') },
       function(){ $(this).css('display', 'none') }
)
5
  • Look the .hover() method in jquery. api.jquery.com/hover Commented Dec 4, 2012 at 16:15
  • why not add and remove a class based on the hover event in jquery or onmouseover and onmouseout? Commented Dec 4, 2012 at 16:16
  • you want the element to disappear when you've hovered away from it? How are you supposed to hover back onto it again? Commented Dec 4, 2012 at 16:18
  • @Alnitak Good point, i chose display attribute randomly, probably not a good example :) Commented Dec 4, 2012 at 16:18
  • 2
    Just to clarify. If I understood original question is not how to trigger jQuery event on hover/hout, but rather how to decorate the hover style of an element with particular CSS attributes, etc. However that is not possible, please see here more details: (jQuery Bug tracker: ACCESS :HOVER CSS PROPERTIES OF AN ELEMENT VIA JQUERY: bugs.jquery.com/ticket/4434). Therefore workarounds in other answers provided should be used instead. Commented Feb 21, 2015 at 16:51

4 Answers 4

35

I find using mouseenter and mouseleave to be better than hover. There's more control.

$("#somecontent").mouseenter(function() {
    $(this).css("background", "#F00").css("border-radius", "3px");
}).mouseleave(function() {
     $(this).css("background", "00F").css("border-radius", "0px");
});
3
  • 7
    Care to elaborate on the "There's more control" statement? As far as I'm aware the .hover() function is just a shorthand for doing precisely what you've coded. Commented Dec 4, 2012 at 16:20
  • 6
    I just like the explicitness of it saying 'mouse entering the element' and 'mouse leaving the element'. However, the graphs on this page would suggest that live mouseenter/mouseleave is the best in terms of optimization. jsperf.com/hover-vs-mouseenter Commented Dec 4, 2012 at 16:40
  • @Jon great answer, very useful thanks. One quick thing, you forgot the "#" hash symbol on the hex color code for the mouseleave function, which is required for that bit of code to work. Commented Jan 26, 2022 at 18:46
19

Try this:

$('#some-content').hover(function(){
    $(this).css({ marginTop: '60px', display: 'inline-block' });
}, function(){
    $(this).css({ //other stuff });
});

or using classes

$('#some-content').hover(function(){
    $(this).toggleClass('newClass');
});

More info here .hover() and .toggleClass()

3

You should put them in a hover event:

var elem = $('#elem');

elem.hover(function () {
    // ... :hover, set styles
}, function () {
    // ... this function is called when the mouse leaves the item, set back the
    //     normal styles
});

However, I completely recommend to put your CSS in classes and use those classes in JS, you should split the languages as much as you can.

2
$("#someObj").hover(function(){
    $(this).css(...);
}:);

http://api.jquery.com/hover/

1
  • This causes the css to stick after you move the mouse away. Commented Mar 18, 2014 at 4:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.