具有垂直外层div和内部格滚动水平而且隐藏滚动条(Scrolling with outer div

2019-10-30 20:53发布

我在想申请滚动到我的HTML问题。 这个想法是有一个网格。 这可垂直滚动。 但我也希望有一个网格的一部分水平滚动。

作为奖励,我不希望任何可见的滚动条,我会使用的WebView使用它在Android上。

我与几个组件溢出摆弄,但我通常结束了:

  • 可以水平滚动,而不是只有“数据”格全格。
  • 具有2个独立的垂直滚动条两者“数据”和“标题”的div。

我的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>  

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;
}

而我的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' } );
    });
});​

对于convienence: http://jsfiddle.net/xFPtj/1/

任何提示,我应该怎么办呢?

Answer 1:

我希望我理解正确你的问题,从我的理解,我认为你要像下面这样:

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

我已经隐藏使用溢出招滚动条,它可能会有所一个黑客的考虑,但是它采用纯HTML + CSS(无需JavaScript的),并很容易调节,而工作的跨浏览器。

如果您有兴趣,可以阅读更多关于它在这里 。

不幸的是,我已经有一对夫妇的div在那里的溢出招的,我不认为这是一个重大的缺点,虽然。

对于未来的参考,修改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>    

和修改CSS(JavaScript的不变):

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;
}

你可能想仔细检查CSS,因为我还没有,它可能是一个有点乱。



文章来源: Scrolling with outer div vertically and inner div horizontally but also hiding the scrollbars