views:

173

answers:

2

I was given some great help by redsquare to get this far to change jquery tabs from links within the content, but I have one more issue that I'm looking for support on...

When a user clicks a link to switch to a different tab from within the content, can i get the page to jump to the top of the page?

Within my demo link, scroll to the bottom of the page to click the links and they switch tabs just perfectly, which you can confirm if you scroll up.

So, I'm looking for the switch to also jump the user to the top of the page, so the user doesn't have to "scroll" to the top of the page to begin reading new content.

Here's my demo: http://jsbin.com/etoku3/11

Existing code:

<script type="text/javascript">
    $(document).ready(function() {
        var $tabs = $("#container-1").tabs();
        var changeTab = function(ev){
          ev.preventDefault();
          var tabIndex = this.hash.charAt(this.hash.length-1) -1;
          $tabs.tabs('select', tabIndex);
         };
        $('a.tablink').click(changeTab);
    });
</script> 

Thank you so much!

Evan

+3  A: 

Place this in the changeTab function:

 $('body').scrollTop(0);

Your code:

$(document).ready(function() {

    var $tabs = $("#container-1").tabs();

    var changeTab = function(ev){

      ev.preventDefault();

      var tabIndex = this.hash.charAt(this.hash.length-1) -1;

      $tabs.tabs('select', tabIndex);

      $('html,body').scrollTop(0);  // Scroll to top
     };

    $('a.tablink').click(changeTab);

});

EDIT:

If you want to animate it to the top, use this instead:

$('html,body').animate({scrollTop:0}, 500)
patrick dw
i tried this just now in FireFox and IE7, but it doesn't work. It only appears to work in Chrome. Here is my demo link to show you: http://jsbin.com/upevo3
Evan
Sorry, try `$('html,body').scrollTop(0);`
patrick dw
perfect. thank you patrick!
Evan
You're welcome! :)
patrick dw
patrick. would i be able to get your help on a different question? it's for this question i posted here: http://stackoverflow.com/questions/3012154/jquery-carousel-auto-scroll-with-text-and-image
Evan
A: 

window.location = "#"; Simple, no javascript might-not-be-supported-by-old-browsers scrolling...

Hello71