2

I have the following HTML as an example:

<div id="header">
<div class="pagination"></div>
</div>

and the following jQuery which should hide the pagination by default but then fade it in when a user hovers the header and then fades it back out when they move off the header:

            $(".pagination").hide();
            $("#header").mousemove(function()
            {
                $(".pagination").fadeIn(1500);
            });
            $("#header").mouseleave(function()
            {
                $(".pagination").fadeOut(1500);
            });

The problem I have is that it will run through the code the same number of times a user hovers the header so for example if I hovered 5 times in a row the pagination would fade in and out 5 times. This is not the function I want, rather a simple fade in and out when a user is hovering the header.

Can anyone help? Thanks.

2
  • As an aside, you could chain those as well $("header").mousemove(...).mouseleave(...); Commented May 16, 2011 at 17:34
  • +1 : the question is readable and clear! :) Commented May 16, 2011 at 17:48

2 Answers 2

4

Edited: Added some code to avoid repeated fading in and out.

     var running = false;

     $("#header").hover(function()
        {
            if(!running){
               $(".pagination").fadeIn(1500);
               running = true;
            }
        }, function()
        {
            $(".pagination").fadeOut(1500, function(){
                running = false;
            });
        });

Now, the hover effect only kicks in if it is not already running.

Sign up to request clarification or add additional context in comments.

1 Comment

Still fades the pagination in x number of times though!
2

well than instead of fadeIn - fadeOut just use .fadeTo like in my

JSFiddle DEMO

And to stop the issue you are talking about use .stop()

Good Luck!

2 Comments

That works :) Seem like stop() was the magic bit of code I was looking for. Cheers
@Cameron not exactly! use .stop() with fadeIn or fadeOut and you'll get another issue! try and see! :) Take care. ..and thanks

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.