Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fade in a div on hover/mouseover using jquery?

I have a div that is set to display:hidden. I want this div to be set to display:block when a certain element (#navbar li a) is hovered over. Here is my javascript.

$('document').ready(function(){
    $("#navbar li a").onmouseover(function{
        $("#navbar ul").css("display","block"); 
    }); 
}); 

I know that $("#navbar li a") is targeting the proper element, as I have tested this. Is there anything wrong with my javascript code?

edit: this is a dropdown menu. #navbar ul is a nested list.

like image 777
Sam D20 Avatar asked Jul 19 '13 20:07

Sam D20


2 Answers

Use .hover

$('document').ready(function(){
    $("#navbar li a").hover(function(){
        $("#navbar ul").css("display","block"); 
    }); 
}); 

If you would like a fade in effect then just use .fadeIn

DEMO

$(function() {
$('#div1').hover(function() { 
    $('#div2').fadeIn(); 
}, function() { 
    $('#div2').fadeOut(); 
});
});

For completeness here's a CSS only method:

(FYI this using this method won't fade it as per say, just make it appear on hover and then disappear when not on hover.)

DEMO

#div2 {
    width: 200px;
    height: 200px;
    background: red;
    display: none;
}

#div1:hover ~ #div2 {
    display: block;    
}
like image 140
Kevin Lynch Avatar answered Sep 28 '22 01:09

Kevin Lynch


There is no "onmouseover"

The right syntaxsis is:

$("#navbar li a").on("mouseover", function(){
    $("#navbar ul").show() //Can use just show here
})
like image 33
drip Avatar answered Sep 27 '22 23:09

drip