代码之家  ›  专栏  ›  技术社区  ›  psoares

表格顶部和底部的水平滚动条

  •  130
  • psoares  · 技术社区  · 14 年前

    我有一个很大的 table 在我的页面上。所以我决定把一个水平滚动条放在桌子的底部。但我希望这个滚动条也在桌面上。

    我在模板中看到的是:

    <div style="overflow:auto; width:100%; height:130%">
    <table id="data" style="width:100%">
    

    是否可以只使用HTML和CSS?

    12 回复  |  直到 6 年前
        1
  •  203
  •   Cody Guldner    11 年前

    要模拟元素顶部的第二个水平滚动条,请放置“dummy” div 在有水平滚动的元素上面,刚好足够高到一个滚动条。然后为虚拟元素和实际元素附加“scroll”事件的处理程序,以便在移动任一滚动条时使另一个元素保持同步。虚拟元素看起来像是实际元素上方的第二个水平滚动条。

    举个活生生的例子 this fiddle

    下面是代码 :

    HTML:

    <div class="wrapper1">
      <div class="div1"></div>
    </div>
    <div class="wrapper2">
      <div class="div2">
        <!-- Content Here -->
      </div>
    </div>
    

    CSS:

    .wrapper1, .wrapper2 {
      width: 300px;
      overflow-x: scroll;
      overflow-y:hidden;
    }
    
    .wrapper1 {height: 20px; }
    .wrapper2 {height: 200px; }
    
    .div1 {
      width:1000px;
      height: 20px;
    }
    
    .div2 {
      width:1000px;
      height: 200px;
      background-color: #88FF88;
      overflow: auto;
    }
    

    JS:

    $(function(){
      $(".wrapper1").scroll(function(){
        $(".wrapper2").scrollLeft($(".wrapper1").scrollLeft());
      });
      $(".wrapper2").scroll(function(){
        $(".wrapper1").scrollLeft($(".wrapper2").scrollLeft());
      });
    });
    
        2
  •  35
  •   robsch YaakovHatam    8 年前

    尝试使用 jquery.doubleScroll plugin

    jQuery:

    $(document).ready(function(){
      $('#double-scroll').doubleScroll();
    });
    

    CSS

    #double-scroll{
      width: 400px;
    }
    

    HTML

    <div id="double-scroll">
      <table id="very-wide-element">
        <tbody>
          <tr>
            <td></td>
          </tr>
        </tbody>
      </table>
    </div>
    
        3
  •  17
  •   simo    10 年前

    第一个伟大的答案是@stanleyh 如果有人想知道如何制作动态宽度的双滚动容器

    CSS

    .wrapper1, .wrapper2 { width: 100%; overflow-x: scroll; overflow-y: hidden; }
    .wrapper1 { height: 20px; }
    .div1 { height: 20px; }
    .div2 { overflow: none; }
    

    JS

    $(function () {
        $('.wrapper1').on('scroll', function (e) {
            $('.wrapper2').scrollLeft($('.wrapper1').scrollLeft());
        }); 
        $('.wrapper2').on('scroll', function (e) {
            $('.wrapper1').scrollLeft($('.wrapper2').scrollLeft());
        });
    });
    $(window).on('load', function (e) {
        $('.div1').width($('table').width());
        $('.div2').width($('table').width());
    });
    

    HTML

    <div class="wrapper1">
        <div class="div1"></div>
    </div>
    <div class="wrapper2">
        <div class="div2">
            <table>
                <tbody>
                    <tr>
                        <td>table cell</td>
                        <td>table cell</td>
                        <!-- ... -->
                        <td>table cell</td>
                        <td>table cell</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
    

    演示

    http://jsfiddle.net/simo/67xSL/

        4
  •  10
  •   avianey    10 年前

    您可以使用jquery插件来完成以下工作:

    这个插件将为您处理所有的逻辑…

        5
  •  9
  •   rap-2-h    6 年前

    无jquery(2017)

    因为您可能不需要jquery,下面是一个基于@stanleyh答案的普通JS版本:

    var wrapper1 = document.getElementById('wrapper1');
    var wrapper2 = document.getElementById('wrapper2');
    wrapper1.onscroll = function() {
      wrapper2.scrollLeft = wrapper1.scrollLeft;
    };
    wrapper2.onscroll = function() {
      wrapper1.scrollLeft = wrapper2.scrollLeft;
    };
    #wrapper1, #wrapper2{width: 300px; border: none 0px RED;
    overflow-x: scroll; overflow-y:hidden;}
    #wrapper1{height: 20px; }
    #wrapper2{height: 100px; }
    #div1 {width:1000px; height: 20px; }
    #div2 {width:1000px; height: 100px; background-color: #88FF88;
    overflow: auto;}
    <div id="wrapper1">
        <div id="div1">
        </div>
    </div>
    <div id="wrapper2">
        <div id="div2">
        aaaa bbbb cccc dddd aaaa bbbb cccc 
        dddd aaaa bbbb cccc dddd aaaa bbbb 
        cccc dddd aaaa bbbb cccc dddd aaaa 
        bbbb cccc dddd aaaa bbbb cccc dddd
        </div>
    </div>
        6
  •  3
  •   przno    10 年前

    基于@stanleyh解决方案,我创建了一个 AngularJS directive 演示 jsFiddle .

    易于使用:

    <div data-double-scroll-bar-horizontal> {{content}} or static content </div>
    

    不需要jquery。

        7
  •  2
  •   Kijewski Jim    8 年前

    链接滚动条是有效的,但在书写的过程中,它会创建一个循环,在大多数浏览器中,如果您单击较轻滚动条的一部分并按住它(拖动滚动条时不这样做),滚动速度会变慢。

    我用旗把它修好了:

    $(function() {
        x = 1;
        $(".wrapper1").scroll(function() {
            if (x == 1) {
                x = 0;
                $(".wrapper2")
                    .scrollLeft($(".wrapper1").scrollLeft());
            } else {
                x = 1;
            }
        });
    
    
        $(".wrapper2").scroll(function() {
            if (x == 1) {
                x = 0;
                $(".wrapper1")
                    .scrollLeft($(".wrapper2").scrollLeft());
            } else {
                x = 1;
            }
        });
    });
    
        8
  •  1
  •   punkrockbuddyholly    14 年前

    据我所知,HTML和CSS不可能做到这一点。

        9
  •  1
  •   MattC Holger    10 年前

    扩展Stanleyh的答案,并尝试找到所需的最小值,以下是我实现的:

    javascript(从诸如$(document.ready()之类的地方调用一次):

    function doubleScroll(){
            $(".topScrollVisible").scroll(function(){
                $(".tableWrapper")
                    .scrollLeft($(".topScrollVisible").scrollLeft());
            });
            $(".tableWrapper").scroll(function(){
                $(".topScrollVisible")
                    .scrollLeft($(".tableWrapper").scrollLeft());
            });
    }
    

    HTML(请注意,宽度将更改滚动条长度):

    <div class="topScrollVisible" style="overflow-x:scroll">
        <div class="topScrollTableLength" style="width:1520px; height:20px">
        </div>
    </div>
    <div class="tableWrapper" style="overflow:auto; height:100%;">
        <table id="myTable" style="width:1470px" class="myTableClass">
    ...
        </table>
    

    就是这样。

        10
  •  1
  •   Jakub Bares    6 年前

    在普通的javascript/angular中,您可以这样做:

    scroll() {
        let scroller = document.querySelector('.above-scroller');
        let table = document.querySelector('.table');
        table.scrollTo(scroller.scrollLeft,0);
      }
    

    HTML:

    <div class="above-scroller" (scroll)="scroll()">
      <div class="scroller"></div>
    </div>
    <div class="table" >
      <table></table>
    </div>
    

    CSS:

    .above-scroller  {
       overflow-x: scroll;
       overflow-y:hidden;
       height: 20px;
       width: 1200px
     }
    
    .scroller {
      width:4500px;
      height: 20px;
    }
    
    .table {
      width:100%;
      height: 100%;
      overflow: auto;
    }
    
        11
  •  0
  •   diyism    11 年前

    如果您在WebKit浏览器或移动浏览器上使用iscroll.js,可以尝试:

    $('#pageWrapper>div:last-child').css('top', "0px");
    
        12
  •  -1
  •   Vlado    8 年前

    滚动方向有问题:rtl。插件似乎不支持它。这是小提琴:jsfiddle.net/qsn7tupc/3

    请注意,它在Chrome中工作正常,但在所有其他流行的浏览器中,顶部的滚动条方向保持为左。