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') } )
The hover() is an inbuilt method in jQuery which is used to specify two functions to start when mouse pointer move over the selected element. Syntax: $(selector). hover(Function_in, Function_out);
The hover() method specifies two functions to run when the mouse pointer hovers over the selected elements. This method triggers both the mouseenter and mouseleave events. Note: If only one function is specified, it will be run for both the mouseenter and mouseleave events.
hover() is deprecated #66.
The :hover selector is used to select elements when you mouse over them. Tip: The :hover selector can be used on all elements, not only on links. Tip: Use the :link selector to style links to unvisited pages, the :visited selector to style links to visited pages, and the :active selector to style the active link.
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"); });
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With