views:

42

answers:

2

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  A: 

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>
aSeptik
Here is the code, the problem is the a tags link to an anchorhttp://pastie.org/972703
badnaam
see the updates and let me know!
aSeptik
Thanks for the quick response, it still jumps though. The code is updated at the pastie link
badnaam
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.
hollsk
<a href="#" class="null_link"><%= t('home.index.how_works') %></a>taking the anchors out kills jquery ui tabs.
badnaam
A: 

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&amp;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; }
     });
Dan Heberden
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.
Dan Heberden
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.http://pastie.org/972703
badnaam
I don't see any javascript errors
badnaam
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 :)
Dan Heberden
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.http://pastie.org/972703
badnaam
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
Dan Heberden
this is resolved, it's working fine, as expected. final code is in http://pastie.org/972703. Thank you all for your help!!
badnaam