Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Jquery UI Tabs scroll horizontally if there are too many tabs

It appears as multiple line by default.

Now the image above is an example of " too many tabs ", It appears as multiple line by default.

But I want to make it in a single line and horizontally scrollable, either adding two arrows before the beginning tab and after the last tab, or scroll automatically are OK.

like image 617
Aloong Avatar asked Jul 12 '11 06:07

Aloong


5 Answers

I have a different approach to this issue, as I think a scroll for the tabs is counter-intuitive. I created a plugin to do a dropdown when the tabs break to a second line.

https://github.com/jasonday/plusTabs

like image 87
Jason Avatar answered Nov 18 '22 07:11

Jason


I was recently looking for a solution to this but none of the other plugins / examples seemed to work with jQuery 1.9+, I also think that Jason's answer of creating a 'more' tab and displaying any extra tabs as a drop down provides the best UI experience, and so I expanded on his answer and created a jQuery plugin for 1.9+ that extends jQuery UI tabs, if the total width of all of the tabs exceeds the width of the tabs container then the additional tabs are grouped together in a dropdown.

enter image description here

You can see the JSFiddle Demo to see it in action. Try resizing your browser window to see it working.

Or you can view the full plugin code at JSFiddle.

Initialising 'Overflow Tabs' is as simple as this:

$("#tabs").tabs({
    overflowTabs: true,
    tabPadding: 23, 
    containerPadding: 40,
    dropdownSize: 50
});

tabPadding is the number of pixels of padding around the text in a tab.

containerPadding is the padding of the container.

dropdownSize is the pixel size of the dropdown selector button

I have tested this on the latest versions of Chrome, Firefox, and IE. If you spot any issues or can improve this then feel free to fork it and go ahead.

Now also available on GitHub.

like image 29
Paul Blundell Avatar answered Nov 18 '22 06:11

Paul Blundell


None of the plugins listed here quite worked for me (most are outdated and not compatible with jQuery 2.0+) but I eventually found this: https://github.com/joshreed/jQuery-ScrollTabs ...worth adding to the list.

like image 5
Digs Avatar answered Nov 18 '22 06:11

Digs


I've just made a very simple plugin for this. Basically you have to add one fixed length div and a moveable one to the tab navigator.

Plugin code:

(function ($) {
var settings = {
    barheight: 38
}    

$.fn.scrollabletab = function (options) {

    var ops = $.extend(settings, options);

    var ul = this.children('ul').first();
    var ulHtmlOld = ul.html();
    var tabBarWidth = $(this).width()-60;
    ul.wrapInner('<div class="fixedContainer" style="height: ' + ops.barheight + 'px; width: ' + tabBarWidth + 'px; overflow: hidden; float: left;"><div class="moveableContainer" style="height: ' + ops.barheight + 'px; width: 5000px; position: relative; left: 0px;"></div></div>');
    ul.append('<div style="width: 20px; float: left; height: ' + (ops.barheight - 2) + 'px; margin-left: 5px; margin-right: 0;"></div>');
    var leftArrow = ul.children().last();
    leftArrow.button({ icons: { secondary: "ui-icon ui-icon-carat-1-w" } });
    leftArrow.children('.ui-icon-carat-1-w').first().css('left', '2px');        

    ul.append('<div style="width: 20px; float: left; height: ' + (ops.barheight - 2) + 'px; margin-left: 1px; margin-right: 0;"></div>');
    var rightArrow = ul.children().last();
    rightArrow.button({ icons: { secondary: "ui-icon ui-icon-carat-1-e" } });
    rightArrow.children('.ui-icon-carat-1-e').first().css('left', '2px');        

    var moveable = ul.find('.moveableContainer').first();
    leftArrow.click(function () {
        var offset = tabBarWidth / 6;
        var currentPosition = moveable.css('left').replace('px', '') / 1;

        if (currentPosition + offset >= 0) {
            moveable.stop().animate({ left: '0' }, 'slow');
        }
        else {
            moveable.stop().animate({ left: currentPosition + offset + 'px' }, 'slow');
        }
    });

    rightArrow.click(function () {
        var offset = tabBarWidth / 6;
        var currentPosition = moveable.css('left').replace('px', '') / 1;
        var tabsRealWidth = 0;
        ul.find('li').each(function (index, element) {
            tabsRealWidth += $(element).width();
            tabsRealWidth += ($(element).css('margin-right').replace('px', '') / 1);
        });

        tabsRealWidth *= -1;

        if (currentPosition - tabBarWidth > tabsRealWidth) {
            moveable.stop().animate({ left: currentPosition - offset + 'px' }, 'slow');
        }
    });


    return this;
}; })(jQuery);

Check it out at http://jsfiddle.net/Bua2d/

like image 4
André Lourenço Avatar answered Nov 18 '22 07:11

André Lourenço


Here is a plugin for that: http://jquery.aamirafridi.com/jst/

like image 3
Simeon Avatar answered Nov 18 '22 06:11

Simeon