2

I have multiple links that show/hide divs throughout (using slideDown()). Each of the links are

<a href="#">Link</a>

Everytime you click on the link, it jumps back to the top of the screen while it animates. I'm assuming it's because of the #. Is there any way in jQuery to focus on the animation rather than jump to the top of the screen each time? Thanks

4 Answers 4

5

In the onClick handler, disable the default action of the anchor element ...

$('a').click(function(e) {
e.preventDefault();
// ANIMATION STUFF GOES HERE...
});

2

You will need to return false in the function you have bound to the link's click event.

3
  • That is wrong, he will not have to. He can do so. But if he wants the event to bubble on, he should only call preventDefault and not return false from the function.
    – Tom Bartel
    Feb 22, 2010 at 13:03
  • So if what I suggested works, and the question didn't mention event bubbling, how is it wrong?
    – Mark Bell
    Feb 22, 2010 at 13:38
  • @Axsuul event handlers can cascade... e.g. when you have DOM objects inside each others. If you return false from event handler, the event is considered consumed and the other guys don't get to see it, otherwise it propagates to the parent objects Feb 22, 2010 at 19:39
0

Use return false at the end of the click function.

$(function(){
    $("#an1").click(function(){
         // your animation code
         return false;
    });
});

<a id="an1" href="#">click me</a>
0

In the event handler function, use e.preventDefault();, assuming e is the event object. Optionally, return false from the handler function.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.