0

The problem is every time a tab is activated, the cursor jumps to the top of the page, since the tab link points to a div and the page scrolls up to the top of the div. This creates a jumpy effect if the user has scrolled down a bit, while reading tab content.

Is there anyway to prevent this?

1
  • Welcome to SO. Can you show some code? Commented May 22, 2010 at 19:30

2 Answers 2

1

UPDATED to fit posted code!

$j('.null_link').live('click', function(e){
e.preventDefault();
return false;
});

$('ul.tabs li a').click(function(e) {
    e.preventDefault();
});

Assuming something like this:

<ul class="tabs">
<li><a href="#" >TAB A</a></li>
<li><a href="#" >TAB B</a></li>
<li><a href="#" >TAB C</a></li>
</ul>

NOTE:

you can also prevent the jump effect by doing this:

<li><a href="javascript:;" >TAB A</a></li>
Sign up to request clarification or add additional context in comments.

4 Comments

Here is the code, the problem is the a tags link to an anchor pastie.org/972703
Thanks for the quick response, it still jumps though. The code is updated at the pastie link
Don't use a named anchor on your hrefs, do it the way aSeptik posted - leaving all the anchor links as '#'. Otherwise you're giving the browser an actual location to jump to that it'll always try to respect.
<a href="#" class="null_link"><%= t('home.index.how_works') %></a> taking the anchors out kills jquery ui tabs.
0

There is something else wrong, because the ui tabs don't have that behavior by default. Here in an implementation of UI tabs with ui 1.8 and jquery 1.4.2 that has no alterations to its call besides $('#selector').tabs(); http://www.horsezone.com.au/index.php?a=28&b=153

Are you getting any javascript errors when you're running the page? I would suspect something is halting the script, thus "return false" to the A element never happens and the anchor fires.

This is NOT standard jquery-ui tab behavior.

On a side note, as for binding to the click, this would be the 'documented' way:

   $j("#tabs").tabs(
       { fx: { opacity: 'toggle' },
          select: function(e) { e.preventDefault(); return false; }
     });

6 Comments

Re: the other comments about removing the pound-sign or named anchors: - - This is how jQuery UI tabs work. The href value of the a element is used to specify the id of the DIV that will be selected. Internally, the link is returned false, thus it doesn't actually fire the anchor. ** badnaam, did you find if you have any javascript errors when you select tabs? Firefox you can choose the Error Console from the Tools menu and see generated errors.
OK, i tried that, and I don't know what happened, but now the tabs are all messed up, looks as if, I can't even click on tabs-2 and the content of tabs-2 is shown by default. pastie.org/972703
Remove the select: function() bit, it's technically unnecessary - it was just a side point. Try your example, but remove the accordian and its javascript initialization (reduce the tabs-2 div to just some text) and see if your problem goes away - then report back :)
Sorry, I take that back. typo, so now, it all seems to be working, even without any e.preventdefault, little baffled to be honest. the only thing I changed was take the <div> <span class="intro_sign_up"><%= link_to t('txt.sign_up'), new_user_path, :class => "special_link_big" %></span> </div>. out of the tabs div. pastie.org/972703
The rendered HTML must have been invalid, throwing off the browser... glad it's working! And yeah, the e.preventDefault is unneeded - .tabs() handles all that for ya
|

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.