外部div垂直滚动和内部div水平滚动,但也隐藏滚动条

时间:2013-01-04 15:51:00

标签: jquery html css scroll

我在尝试将滚动应用于HTML时遇到问题。 想法是有一个网格。哪个可以垂直滚动。 但我也希望水平滚动该网格的部分

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

我正在摆弄几个组件的溢出,但我通常最终得到:

  • 能够水平滚动整个网格,而不只是“数据”div。
  • 为“数据”和“标题”div设置了2个单独的垂直滚动条。

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

为了便利:http://jsfiddle.net/xFPtj/1/

我应该做什么的提示?

1 个答案:

答案 0 :(得分:2)

我希望我能正确理解你的问题,根据我的理解,我认为你想要的东西如下:

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

我使用溢出技巧隐藏了滚动条,它可能被认为是一种黑客,但它使用纯HTML + CSS(不需要JavaScript),并且可以在跨浏览器工作时轻松调整。

如果您感到好奇,可以阅读更多相关信息here

不幸的是,我不得不在其中包含几个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,因为我没有,它可能有点乱。