Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Tabs, how to prevent an anchor jump

Tags:

jquery

tabs

hash

I use this script:

(function() {
    var tabContainers = $('div.tabs > div');
    tabContainers.hide().filter(':first').show();
    $(window).bind('hashchange', function () {
        var hash = window.location.hash || '#first';
        tabContainers.hide();
        tabContainers.filter(hash).show();
        $('div.tabs ul.tabNavigation a').removeClass('selected');
        $('a[href=' + hash +']').addClass('selected');
    });
    $(window).trigger("hashchange");
});

This part of code gives me everything that i need. To open the tab from the external page i use "index.php#first" href, for example. It opens the page with tabs with the exact tab opened, but it jumps to the anchor, and i need to show the page from it's top.

Does anybody know, how to prevent anchor jumps for this code?

like image 910
Alexander Von Verzicht Avatar asked Oct 16 '12 11:10

Alexander Von Verzicht


1 Answers

Have you tried doing a return false inside the hashchange function?.

Otherwise, you can avoid using real IDs on your elements. That way it will not scroll.

Update: As you are using jQuery you can also try with: e.preventDefault. The function will need to receive e as parameter

like image 96
Carlos Martinez T Avatar answered Nov 15 '22 08:11

Carlos Martinez T