Scrolling with outer div vertically and inner div

2019-08-27 11:47发布

问题:

I'm having problems trying to apply scrolling to my HTML. The idea is to have a grid. Which can be scrolled vertically. But I also want to have a part of that grid be scrolled horizontally.

As a bonus I don't want any of the scrollbars visible, I will using it on Android using a WebView.

I'm fiddling with the overflow on several components, but I usually end up with:

  • able to scroll the whole grid horizontally, instead of only the 'Data' div.
  • have 2 seperate vertical scroll bars for both 'data' and 'header' divs.

My HTML:

<button id='scroll_up'>scroll up</button>
<button id='scroll_down'>scroll down</button>
<button id='scroll_left'>scroll left</button>
<button id='scroll_right'>scroll right</button>
<div id="grid">
    <div id="header">
        <a id='row_header'>Some Header</a>
        <a class="row">1</a>
        <a class="row">2</a>
        <a class="row">3</a>
        <a class="row">4</a>
        <a class="row">5</a>
        <a class="row">6</a>
        <a class="row">7</a>
        <a class="row">8</a>
        <a class="row">9</a>
        <a class="row">10</a>
        <a class="row">11</a>
        <a class="row">12</a>
        <a class="row">13</a>
        <a class="row">14</a>
        <a class="row">15</a>
        <a class="row">16</a>
        <a class="row">17</a>
        <a class="row">18</a>
        <a class="row">19</a>
        <a class="row">20</a>
    </div>
    <div id="data">
        <div id="column_header">
            <a class="header">h1</a>
            <a class="header">h2</a>
            <a class="header">h3</a>
            <a class="header">h4</a>
            <a class="header">h5</a>
            <a class="header">h6</a>
            <a class="header">h7</a>
            <a class="header">h8</a>
            <a class="header">h9</a>
            <a class="header">h10</a>
            <a class="header">h11</a>
            <a class="header">h12</a>
        </div>
                    <div class="row_data">
            <a class="data">data 1</a>
            <a class="data">data 2</a>
            <a class="data">data 3</a>
            <a class="data">data 4</a>
            <a class="data">data 5</a>
            <a class="data">data 6</a>
            <a class="data">data 7</a>
            <a class="data">data 8</a>
            <a class="data">data 9</a>
            <a class="data">data 10</a>
            <a class="data">data 11</a>
            <a class="data">data 12</a>
        </div>
        <div class="row_data">
            <a class="data">data 1</a>
            <a class="data">data 2</a>
            <a class="data">data 3</a>
            <a class="data">data 4</a>
            <a class="data">data 5</a>
            <a class="data">data 6</a>
            <a class="data">data 7</a>
            <a class="data">data 8</a>
            <a class="data">data 9</a>
            <a class="data">data 10</a>
            <a class="data">data 11</a>
            <a class="data">data 12</a>
        </div>
        <div class="row_data">
            <a class="data">data 1</a>
            <a class="data">data 2</a>
            <a class="data">data 3</a>
            <a class="data">data 4</a>
            <a class="data">data 5</a>
            <a class="data">data 6</a>
            <a class="data">data 7</a>
            <a class="data">data 8</a>
            <a class="data">data 9</a>
            <a class="data">data 10</a>
            <a class="data">data 11</a>
            <a class="data">data 12</a>
        </div>
        <div class="row_data">
            <a class="data">data 1</a>
            <a class="data">data 2</a>
            <a class="data">data 3</a>
            <a class="data">data 4</a>
            <a class="data">data 5</a>
            <a class="data">data 6</a>
            <a class="data">data 7</a>
            <a class="data">data 8</a>
            <a class="data">data 9</a>
            <a class="data">data 10</a>
            <a class="data">data 11</a>
            <a class="data">data 12</a>
        </div>
        <div class="row_data">
            <a class="data">data 1</a>
            <a class="data">data 2</a>
            <a class="data">data 3</a>
            <a class="data">data 4</a>
            <a class="data">data 5</a>
            <a class="data">data 6</a>
            <a class="data">data 7</a>
            <a class="data">data 8</a>
            <a class="data">data 9</a>
            <a class="data">data 10</a>
            <a class="data">data 11</a>
            <a class="data">data 12</a>
        </div>
        <div class="row_data">
            <a class="data">data 1</a>
            <a class="data">data 2</a>
            <a class="data">data 3</a>
            <a class="data">data 4</a>
            <a class="data">data 5</a>
            <a class="data">data 6</a>
            <a class="data">data 7</a>
            <a class="data">data 8</a>
            <a class="data">data 9</a>
            <a class="data">data 10</a>
            <a class="data">data 11</a>
            <a class="data">data 12</a>
        </div>
        <div class="row_data">
            <a class="data">data 1</a>
            <a class="data">data 2</a>
            <a class="data">data 3</a>
            <a class="data">data 4</a>
            <a class="data">data 5</a>
            <a class="data">data 6</a>
            <a class="data">data 7</a>
            <a class="data">data 8</a>
            <a class="data">data 9</a>
            <a class="data">data 10</a>
            <a class="data">data 11</a>
            <a class="data">data 12</a>
        </div>
        <div class="row_data">
            <a class="data">data 1</a>
            <a class="data">data 2</a>
            <a class="data">data 3</a>
            <a class="data">data 4</a>
            <a class="data">data 5</a>
            <a class="data">data 6</a>
            <a class="data">data 7</a>
            <a class="data">data 8</a>
            <a class="data">data 9</a>
            <a class="data">data 10</a>
            <a class="data">data 11</a>
            <a class="data">data 12</a>
        </div>
        <div class="row_data">
            <a class="data">data 1</a>
            <a class="data">data 2</a>
            <a class="data">data 3</a>
            <a class="data">data 4</a>
            <a class="data">data 5</a>
            <a class="data">data 6</a>
            <a class="data">data 7</a>
            <a class="data">data 8</a>
            <a class="data">data 9</a>
            <a class="data">data 10</a>
            <a class="data">data 11</a>
            <a class="data">data 12</a>
        </div>  
        <div class="row_data">
            <a class="data">data 1</a>
            <a class="data">data 2</a>
            <a class="data">data 3</a>
            <a class="data">data 4</a>
            <a class="data">data 5</a>
            <a class="data">data 6</a>
            <a class="data">data 7</a>
            <a class="data">data 8</a>
            <a class="data">data 9</a>
            <a class="data">data 10</a>
            <a class="data">data 11</a>
            <a class="data">data 12</a>
        </div>
        <div class="row_data">
            <a class="data">data 1</a>
            <a class="data">data 2</a>
            <a class="data">data 3</a>
            <a class="data">data 4</a>
            <a class="data">data 5</a>
            <a class="data">data 6</a>
            <a class="data">data 7</a>
            <a class="data">data 8</a>
            <a class="data">data 9</a>
            <a class="data">data 10</a>
            <a class="data">data 11</a>
            <a class="data">data 12</a>
        </div>  
        <div class="row_data">
            <a class="data">data 1</a>
            <a class="data">data 2</a>
            <a class="data">data 3</a>
            <a class="data">data 4</a>
            <a class="data">data 5</a>
            <a class="data">data 6</a>
            <a class="data">data 7</a>
            <a class="data">data 8</a>
            <a class="data">data 9</a>
            <a class="data">data 10</a>
            <a class="data">data 11</a>
            <a class="data">data 12</a>
        </div>
        <div class="row_data">
            <a class="data">data 1</a>
            <a class="data">data 2</a>
            <a class="data">data 3</a>
            <a class="data">data 4</a>
            <a class="data">data 5</a>
            <a class="data">data 6</a>
            <a class="data">data 7</a>
            <a class="data">data 8</a>
            <a class="data">data 9</a>
            <a class="data">data 10</a>
            <a class="data">data 11</a>
            <a class="data">data 12</a>
        </div>
        <div class="row_data">
            <a class="data">data 1</a>
            <a class="data">data 2</a>
            <a class="data">data 3</a>
            <a class="data">data 4</a>
            <a class="data">data 5</a>
            <a class="data">data 6</a>
            <a class="data">data 7</a>
            <a class="data">data 8</a>
            <a class="data">data 9</a>
            <a class="data">data 10</a>
            <a class="data">data 11</a>
            <a class="data">data 12</a>
        </div>
        <div class="row_data">
            <a class="data">data 1</a>
            <a class="data">data 2</a>
            <a class="data">data 3</a>
            <a class="data">data 4</a>
            <a class="data">data 5</a>
            <a class="data">data 6</a>
            <a class="data">data 7</a>
            <a class="data">data 8</a>
            <a class="data">data 9</a>
            <a class="data">data 10</a>
            <a class="data">data 11</a>
            <a class="data">data 12</a>
        </div>
        <div class="row_data">
            <a class="data">data 1</a>
            <a class="data">data 2</a>
            <a class="data">data 3</a>
            <a class="data">data 4</a>
            <a class="data">data 5</a>
            <a class="data">data 6</a>
            <a class="data">data 7</a>
            <a class="data">data 8</a>
            <a class="data">data 9</a>
            <a class="data">data 10</a>
            <a class="data">data 11</a>
            <a class="data">data 12</a>
        </div>
        <div class="row_data">
            <a class="data">data 1</a>
            <a class="data">data 2</a>
            <a class="data">data 3</a>
            <a class="data">data 4</a>
            <a class="data">data 5</a>
            <a class="data">data 6</a>
            <a class="data">data 7</a>
            <a class="data">data 8</a>
            <a class="data">data 9</a>
            <a class="data">data 10</a>
            <a class="data">data 11</a>
            <a class="data">data 12</a>
        </div>
        <div class="row_data">
            <a class="data">data 1</a>
            <a class="data">data 2</a>
            <a class="data">data 3</a>
            <a class="data">data 4</a>
            <a class="data">data 5</a>
            <a class="data">data 6</a>
            <a class="data">data 7</a>
            <a class="data">data 8</a>
            <a class="data">data 9</a>
            <a class="data">data 10</a>
            <a class="data">data 11</a>
            <a class="data">data 12</a>
        </div>
        <div class="row_data">
            <a class="data">data 1</a>
            <a class="data">data 2</a>
            <a class="data">data 3</a>
            <a class="data">data 4</a>
            <a class="data">data 5</a>
            <a class="data">data 6</a>
            <a class="data">data 7</a>
            <a class="data">data 8</a>
            <a class="data">data 9</a>
            <a class="data">data 10</a>
            <a class="data">data 11</a>
            <a class="data">data 12</a>
        </div>
        <div class="row_data">
            <a class="data">data 1</a>
            <a class="data">data 2</a>
            <a class="data">data 3</a>
            <a class="data">data 4</a>
            <a class="data">data 5</a>
            <a class="data">data 6</a>
            <a class="data">data 7</a>
            <a class="data">data 8</a>
            <a class="data">data 9</a>
            <a class="data">data 10</a>
            <a class="data">data 11</a>
            <a class="data">data 12</a>
        </div>          
    </div>
</div>  

The CSS:

body {
    font: 18px arial,sans-serif;        
}
#grid {
    display: block;
    position: absolute;
    top: 100px;
    left: 0px;

    width: 100%;
    height: 517px;
    margin: 0;
}
#header {
    display: inline-block;
    width: 25%; 
    height: 100%;
    float: left;
    overflow: auto;
}

#data {
    float: left;
    display: inline-block;
    width: 75%;
    height: 100%;
    overflow: auto;

}

.row, #row_header {
    display: block;
    padding-left: 8px;
    height: 50px;
    border: 1px solid black;

    text-align: center;
    line-height: 50px;
}

.row_data, #column_header {
    display: inline-block;
    white-space: nowrap;
    width: 100%;
}

.data, .header {
    display: inline-block;  
    border: 1px solid black;    
    width: 20%;         
}

#row_header, .header {
    height: 30px;
    line-height: 30px;
}

#row_header, #column_header {
    text-align: center;
}

.data, .row {
    height: 50px;
    text-align: center;
    line-height: 50px;
}

And my JS:

$(document).ready(function() {        
    $('#scroll_up').click(function() {
        var height = $('#grid').height();
        $('#grid').scrollTo( '-=' + height + 'px', { axis:'y' } );
    });

    $('#scroll_down').click(function() {
        var height = $('#grid').height();
        $('#grid').scrollTo( '+=' + height + 'px', { axis:'y' } );
    });

    $('#scroll_left').click(function() {
        var width = $('#data').width();
        $('#data').scrollTo( '-=' + width + 'px', { axis:'x' } );
    });

    $('#scroll_right').click(function() {
        var width = $('#data').width();
        $('#data').scrollTo( '+=' + width + 'px', { axis:'x' } );
    });
});​

For convienence: http://jsfiddle.net/xFPtj/1/

Any tips for what I should do?

回答1:

I hope I've understood your question correctly, from what I understand I think you want something like the following:

http://jsfiddle.net/hGTw3/2/

I've hidden the scrollbars using an overflow trick, it could be considered somewhat of a hack but it uses pure HTML + CSS (no JavaScript needed) and is easily adjustable whilst working cross browser.

If you're curious, can read more about it here.

Unfortunately, I've had to include a couple of div's in there for the overflow trick, I don't see this as a major drawback though.

For future reference, amended HTML:

<div class="buttons">
    <button id='scroll_up'>scroll up</button>
    <button id='scroll_down'>scroll down</button>
    <button id='scroll_left'>scroll left</button>
    <button id='scroll_right'>scroll right</button>
</div>
<div id="grid">
    <div id="header-container">
    <div id="header">
        <a id='row_header'>Some Header</a>
        <a class="row">1</a>
        <a class="row">2</a>
        <a class="row">3</a>
        <a class="row">4</a>
        <a class="row">5</a>
        <a class="row">6</a>
        <a class="row">7</a>
        <a class="row">8</a>
        <a class="row">9</a>
        <a class="row">10</a>
        <a class="row">11</a>
        <a class="row">12</a>
        <a class="row">13</a>
        <a class="row">14</a>
        <a class="row">15</a>
        <a class="row">16</a>
        <a class="row">17</a>
        <a class="row">18</a>
        <a class="row">19</a>
        <a class="row">20</a>
    </div>
</div>
    <div id="data-container">
    <div id="data">
        <div id="column_header">
            <a class="header">h1</a>
            <a class="header">h2</a>
            <a class="header">h3</a>
            <a class="header">h4</a>
            <a class="header">h5</a>
            <a class="header">h6</a>
            <a class="header">h7</a>
            <a class="header">h8</a>
            <a class="header">h9</a>
            <a class="header">h10</a>
            <a class="header">h11</a>
            <a class="header">h12</a>
        </div>
                    <div class="row_data">
            <a class="data">data 1</a>
            <a class="data">data 2</a>
            <a class="data">data 3</a>
            <a class="data">data 4</a>
            <a class="data">data 5</a>
            <a class="data">data 6</a>
            <a class="data">data 7</a>
            <a class="data">data 8</a>
            <a class="data">data 9</a>
            <a class="data">data 10</a>
            <a class="data">data 11</a>
            <a class="data">data 12</a>
        </div>
        <div class="row_data">
            <a class="data">data 1</a>
            <a class="data">data 2</a>
            <a class="data">data 3</a>
            <a class="data">data 4</a>
            <a class="data">data 5</a>
            <a class="data">data 6</a>
            <a class="data">data 7</a>
            <a class="data">data 8</a>
            <a class="data">data 9</a>
            <a class="data">data 10</a>
            <a class="data">data 11</a>
            <a class="data">data 12</a>
        </div>
        <div class="row_data">
            <a class="data">data 1</a>
            <a class="data">data 2</a>
            <a class="data">data 3</a>
            <a class="data">data 4</a>
            <a class="data">data 5</a>
            <a class="data">data 6</a>
            <a class="data">data 7</a>
            <a class="data">data 8</a>
            <a class="data">data 9</a>
            <a class="data">data 10</a>
            <a class="data">data 11</a>
            <a class="data">data 12</a>
        </div>
        <div class="row_data">
            <a class="data">data 1</a>
            <a class="data">data 2</a>
            <a class="data">data 3</a>
            <a class="data">data 4</a>
            <a class="data">data 5</a>
            <a class="data">data 6</a>
            <a class="data">data 7</a>
            <a class="data">data 8</a>
            <a class="data">data 9</a>
            <a class="data">data 10</a>
            <a class="data">data 11</a>
            <a class="data">data 12</a>
        </div>
        <div class="row_data">
            <a class="data">data 1</a>
            <a class="data">data 2</a>
            <a class="data">data 3</a>
            <a class="data">data 4</a>
            <a class="data">data 5</a>
            <a class="data">data 6</a>
            <a class="data">data 7</a>
            <a class="data">data 8</a>
            <a class="data">data 9</a>
            <a class="data">data 10</a>
            <a class="data">data 11</a>
            <a class="data">data 12</a>
        </div>
        <div class="row_data">
            <a class="data">data 1</a>
            <a class="data">data 2</a>
            <a class="data">data 3</a>
            <a class="data">data 4</a>
            <a class="data">data 5</a>
            <a class="data">data 6</a>
            <a class="data">data 7</a>
            <a class="data">data 8</a>
            <a class="data">data 9</a>
            <a class="data">data 10</a>
            <a class="data">data 11</a>
            <a class="data">data 12</a>
        </div>
        <div class="row_data">
            <a class="data">data 1</a>
            <a class="data">data 2</a>
            <a class="data">data 3</a>
            <a class="data">data 4</a>
            <a class="data">data 5</a>
            <a class="data">data 6</a>
            <a class="data">data 7</a>
            <a class="data">data 8</a>
            <a class="data">data 9</a>
            <a class="data">data 10</a>
            <a class="data">data 11</a>
            <a class="data">data 12</a>
        </div>
        <div class="row_data">
            <a class="data">data 1</a>
            <a class="data">data 2</a>
            <a class="data">data 3</a>
            <a class="data">data 4</a>
            <a class="data">data 5</a>
            <a class="data">data 6</a>
            <a class="data">data 7</a>
            <a class="data">data 8</a>
            <a class="data">data 9</a>
            <a class="data">data 10</a>
            <a class="data">data 11</a>
            <a class="data">data 12</a>
        </div>
        <div class="row_data">
            <a class="data">data 1</a>
            <a class="data">data 2</a>
            <a class="data">data 3</a>
            <a class="data">data 4</a>
            <a class="data">data 5</a>
            <a class="data">data 6</a>
            <a class="data">data 7</a>
            <a class="data">data 8</a>
            <a class="data">data 9</a>
            <a class="data">data 10</a>
            <a class="data">data 11</a>
            <a class="data">data 12</a>
        </div>    
        <div class="row_data">
            <a class="data">data 1</a>
            <a class="data">data 2</a>
            <a class="data">data 3</a>
            <a class="data">data 4</a>
            <a class="data">data 5</a>
            <a class="data">data 6</a>
            <a class="data">data 7</a>
            <a class="data">data 8</a>
            <a class="data">data 9</a>
            <a class="data">data 10</a>
            <a class="data">data 11</a>
            <a class="data">data 12</a>
        </div>
        <div class="row_data">
            <a class="data">data 1</a>
            <a class="data">data 2</a>
            <a class="data">data 3</a>
            <a class="data">data 4</a>
            <a class="data">data 5</a>
            <a class="data">data 6</a>
            <a class="data">data 7</a>
            <a class="data">data 8</a>
            <a class="data">data 9</a>
            <a class="data">data 10</a>
            <a class="data">data 11</a>
            <a class="data">data 12</a>
        </div>    
        <div class="row_data">
            <a class="data">data 1</a>
            <a class="data">data 2</a>
            <a class="data">data 3</a>
            <a class="data">data 4</a>
            <a class="data">data 5</a>
            <a class="data">data 6</a>
            <a class="data">data 7</a>
            <a class="data">data 8</a>
            <a class="data">data 9</a>
            <a class="data">data 10</a>
            <a class="data">data 11</a>
            <a class="data">data 12</a>
        </div>
        <div class="row_data">
            <a class="data">data 1</a>
            <a class="data">data 2</a>
            <a class="data">data 3</a>
            <a class="data">data 4</a>
            <a class="data">data 5</a>
            <a class="data">data 6</a>
            <a class="data">data 7</a>
            <a class="data">data 8</a>
            <a class="data">data 9</a>
            <a class="data">data 10</a>
            <a class="data">data 11</a>
            <a class="data">data 12</a>
        </div>
        <div class="row_data">
            <a class="data">data 1</a>
            <a class="data">data 2</a>
            <a class="data">data 3</a>
            <a class="data">data 4</a>
            <a class="data">data 5</a>
            <a class="data">data 6</a>
            <a class="data">data 7</a>
            <a class="data">data 8</a>
            <a class="data">data 9</a>
            <a class="data">data 10</a>
            <a class="data">data 11</a>
            <a class="data">data 12</a>
        </div>
        <div class="row_data">
            <a class="data">data 1</a>
            <a class="data">data 2</a>
            <a class="data">data 3</a>
            <a class="data">data 4</a>
            <a class="data">data 5</a>
            <a class="data">data 6</a>
            <a class="data">data 7</a>
            <a class="data">data 8</a>
            <a class="data">data 9</a>
            <a class="data">data 10</a>
            <a class="data">data 11</a>
            <a class="data">data 12</a>
        </div>
        <div class="row_data">
            <a class="data">data 1</a>
            <a class="data">data 2</a>
            <a class="data">data 3</a>
            <a class="data">data 4</a>
            <a class="data">data 5</a>
            <a class="data">data 6</a>
            <a class="data">data 7</a>
            <a class="data">data 8</a>
            <a class="data">data 9</a>
            <a class="data">data 10</a>
            <a class="data">data 11</a>
            <a class="data">data 12</a>
        </div>
        <div class="row_data">
            <a class="data">data 1</a>
            <a class="data">data 2</a>
            <a class="data">data 3</a>
            <a class="data">data 4</a>
            <a class="data">data 5</a>
            <a class="data">data 6</a>
            <a class="data">data 7</a>
            <a class="data">data 8</a>
            <a class="data">data 9</a>
            <a class="data">data 10</a>
            <a class="data">data 11</a>
            <a class="data">data 12</a>
        </div>
        <div class="row_data">
            <a class="data">data 1</a>
            <a class="data">data 2</a>
            <a class="data">data 3</a>
            <a class="data">data 4</a>
            <a class="data">data 5</a>
            <a class="data">data 6</a>
            <a class="data">data 7</a>
            <a class="data">data 8</a>
            <a class="data">data 9</a>
            <a class="data">data 10</a>
            <a class="data">data 11</a>
            <a class="data">data 12</a>
        </div>
        <div class="row_data">
            <a class="data">data 1</a>
            <a class="data">data 2</a>
            <a class="data">data 3</a>
            <a class="data">data 4</a>
            <a class="data">data 5</a>
            <a class="data">data 6</a>
            <a class="data">data 7</a>
            <a class="data">data 8</a>
            <a class="data">data 9</a>
            <a class="data">data 10</a>
            <a class="data">data 11</a>
            <a class="data">data 12</a>
        </div>
        <div class="row_data">
            <a class="data">data 1</a>
            <a class="data">data 2</a>
            <a class="data">data 3</a>
            <a class="data">data 4</a>
            <a class="data">data 5</a>
            <a class="data">data 6</a>
            <a class="data">data 7</a>
            <a class="data">data 8</a>
            <a class="data">data 9</a>
            <a class="data">data 10</a>
            <a class="data">data 11</a>
            <a class="data">data 12</a>
        </div>            
    </div>
</div>
</div>    

And amended CSS (the JavaScript is unchanged):

body {
    font: 18px arial,sans-serif;
    overflow: hidden;
}
#grid {
    clear: left;
    display: block;
    position: absolute;
    right: -16px;
    top: 100px;
    left: 0px;
    bottom: 0;
    overflow-y: auto;
    margin: 0;
}
#header-container {
    float: left;
    overflow: hidden;
    width: 25%;    
}
#header {
    margin: 16px 0 0 0;
    overflow: auto;
    width: 100%;
}

#data-container {
    float: left;
    overflow: hidden;
    width: 75%;
}
#data {
    margin: 16px 0 -16px 10px;
    overflow: auto;
    width: 100%;
}

.row, #row_header {
    display: block;
    padding-left: 8px;
    height: 50px;
    border: 1px solid black;

    text-align: center;
    line-height: 50px;
}

.row_data, #column_header {
    display: inline-block;
    white-space: nowrap;
    width: 100%;
}

.data, .header {
    display: inline-block;    
    border: 1px solid black;    
    width: 20%;            
}

#row_header, .header {
    height: 30px;
    line-height: 30px;
}

#row_header, #column_header {
    text-align: center;
}

.data, .row {
    height: 50px;
    text-align: center;
    line-height: 50px;
}

You might want to double check the CSS as I haven't, it may be a little messy.