Question

I have found here useful piece of code. I really hope that someone can help me.

Here is the code:

$(function(){
    $('#header_nav').data('size','big');
});

$(window).scroll(function(){
    if($(document).scrollTop() > 0)
    {
        if($('#header_nav').data('size') == 'big')
        {
            $('#header_nav').data('size','small');
            $('#header_nav').stop().animate({
                height:'40px'
            },600);
        }
    }
    else
    {
        if($('#header_nav').data('size') == 'small')
        {
            $('#header_nav').data('size','big');
            $('#header_nav').stop().animate({
                height:'100px'
            },600);
        }  
    }
});

http://jsfiddle.net/JJ8Jc/76/


Question: How to do the same thing only on hover (not scroll)?


Thanks a lot.

Was it helpful?

Solution

It's a lot simpler than you might think.

(1) $('#header_nav').data('size','small');

In regards to (1), the exact snippet of code you pasted is really just adding a data-attribute which I am guessing you don't need as you found the code online.

(2) $('#header_nav').stop().animate({

In regards to (2), .stop() just stops the animate abruptly without allowing it to finish

The key is to target the event of the mouse entering the div shown below:

$("#header_nav").mouseenter(function(){

and then again when it leaves to give you the mouseover effect shown below:

$("#header_nav").mouseleave(function(){

I've stripped the code to make it as simple as possible for you to read (DEMO below)

DEMO

It's pretty self explanatory, on mouseenter/mouseleave 'animate' (gradually reduce/increase) the height of the container.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top