Adaptive vertical scrollbar for jQuery Mobile tab content when navbar is in header

2.6k views Asked by At

In some jQuery mobile tabs I have content that requires a scrollbar.

The navbar that controls them is in the header like in this fiddle.

I have tried to set height: 100%; overflow: auto; on all levels of the tab content, but the scrollbar is still applied to the page as a whole.

How can an overflowing scrollbar be adaptively applied to tabbed content only?

jsFiddle

HTML

<div data-role="page" id="mainPage">
    <div data-role="tabs" id="tabs">
        <div data-role="header" style="overflow:hidden;">
            <h1>I'm a header</h1>
            <a href="#" data-icon="gear" class="ui-btn-right">Options</a>
            <div data-role="navbar">
                <ul>
                    <li><a href="#one" class="ui-btn-active">One</a></li>
                    <li><a href="#two">Two</a></li>
                    <li><a href="#">Three</a></li>
                </ul>
            </div>
        </div>
        <div id="one" class="ui-body-d ui-content tabContent">
            <h1>First tab contents</h1>
        </div>
        <div id="two">
            <ul data-role="listview" data-inset="true">
                <li><a href="#">Acura</a></li>
                <li><a href="#">Audi</a></li>
                <li><a href="#">BMW</a></li>
                <li><a href="#">Cadillac</a></li>
                <li><a href="#">Ferrari</a></li>
            </ul>
        </div>
    </div>
</div>

JS

$(document).ready(function() {
    var $one = $('#one')
    for(var i=0; i<1000; i++){
        $one.append('<br/>content')
    }

})

CSS

body{
    overflow:hidden;
}
.tabContent{
    height:100%;
    overflow:auto;
}
2

There are 2 answers

4
Amin Jafari On BEST ANSWER

do you need something like this? http://jsfiddle.net/VjYq9/3/

#one{
    height:330px;
    overflow-y:scroll;
}

UPDATE: there you go http://jsfiddle.net/VjYq9/5/

$one.height($(window).height()-120); //note that -120 must be the size of the header, so it is the window size minus the size of the header so "#one" would fit the screen!
0
Hugo Lourenço On

There is a CSS only solution, @user1382306 please see below:

.max-height-occupied {
    position: absolute;
    top: 88px; /*your header height*/
    right: 0;
    bottom: 44px; /*your footer height */
    left: 0;
}

And the at your target content div just put it like this:

<div data-role="content" data-theme="b" class="max-height-occupied">