【问题标题】:Scrolling a div from an outer div从外部 div 滚动 div
【发布时间】:2012-11-30 21:17:47
【问题描述】:

查看下面的小 html 结构示例以了解上下文。查看此fiddle 并查看问题示例。

小提琴用户的简短说明:

  1. 向左滚动 - 垂直滚动条不可见
  2. 向右滚动 - 垂直可见
  3. 我希望垂直滚动条始终可见
  4. 要求 - 标题必须保持固定(滚动时可见)

详细解释:

我有一个带有固定标题和可滚动正文的表格。其复杂性需要两张表。这都很好。在我的事业中,我也有可调整大小的列。 table-layout: fixed 让它工作。这是出现问题。

为了使滚动体正常工作,我有一个 div 用于包装 .rows 表以进行滚动。这很有效,直到网格,特别是 .rows 表,在 x 方向上溢出。在这种情况下,只有当网格一直向右滚动时,垂直滚动条才可见。因为滚动条在.row-wrapper div 上。 .grid div 隐藏了溢出。我希望滚动条位于 .grid-canvas div 上,这样即使滚动到左侧也能看到它。

<div class=grid>
    <div class=grid-canvas>
        <table class=header></table>
        <div class=row-wrapper>
            <table class=rows></table>
        </div>
    </div>
</div>

旁注:如果您将表格显示设置为阻止,则不需要包装 div,除非您想支持 IE8,我就是这样做的。也许有办法解决这个问题,但另一个问题是另一个问题。

【问题讨论】:

  • 这是一个很好的例子,说明你如何完成它imaputz.com/cssStuff/bigFourVersion.html
  • 这与我得到的基本相同。但问题仍然存在。溢出设置在 tbody 上,因此滚动发生在 tbody 上。当表格宽度大于容器宽度时,您会看到相同的问题。除非您一直向右滚动,否则垂直滚动条不可见。 fiddle

标签: javascript html css


【解决方案1】:

在没有运气的情况下进行了数小时的黑客攻击后,我决定剖析一些现有的库,看看它们是如何实现这一点的。不幸的是,我非常失望。他们都在使用javascript。

这样做的好处是它只需要少量代码,但这并不意味着它会表现良好。为了使其尽可能好地执行,我会在 x 方向需要时更新标题。由于在我的情况下这通常是一个小区域,它应该足够好。

百倍魅力!

fiddle

这是基本的:

HTML

<div class=grid>
    <div class=grid-canvas>
        <div class=header-wrapper>
            <table class=header></table>
        </div>
        <div class=row-wrapper>
          <table class=rows></table>
        </div>
    </div>
</div>

Javascript

$headerDiv = $('.header-wrapper');
$rowDiv = $('.row-wrapper');
$rowDiv.scroll(function(e) {
    $headerDiv.css({
        left: -$rowDiv[0].scrollLeft + 'px'
    });
});​

CSS

.grid {
  height: 500px;
  width: 350px;
}

.grid-canvas {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.header-wrapper {
  position: absolute;
  top: 0;
  width: auto;
  background-color: white;
  z-index: 1;
}

.row-wrapper {
  position: absolute;
  top: 0;
  height: 100%;
  width: 100%;
  box-sizing: border-box;
  -moz-box-sizing: border-box;
  overflow: auto;
  padding-top: 18px;
  background-color: lightgreen;
}

th, td {
  width: 80px;
  min-width: 80px;
  max-width: 80px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

【讨论】:

  • 为了让这个在 IE 中正常工作,我不得不再做一些改变。我对 IE 的回答的问题是滚动条将隐藏在标题 div 下。这在我使用的其他浏览器中并不明显,因为滚动条是透明的并且它仍然位于标题的顶部。这是fiddle
【解决方案2】:

这个呢,如果你想使用 javascript/jQuery 呢?

$(function(){
    $("table").stickyTableHeaders();
});

/*! Copyright (c) 2011 by Jonas Mosbech - https://github.com/jmosbech/StickyTableHeaders
    MIT license info: https://github.com/jmosbech/StickyTableHeaders/blob/master/license.txt */

;(function ($, window, undefined) {
    'use strict';

    var pluginName = 'stickyTableHeaders';
    var defaults = {
            fixedOffset: 0
        };

    function Plugin (el, options) {
        // To avoid scope issues, use 'base' instead of 'this'
        // to reference this class from internal events and functions.
        var base = this;

        // Access to jQuery and DOM versions of element
        base.$el = $(el);
        base.el = el;

        // Cache DOM refs for performance reasons
        base.$window = $(window);
        base.$clonedHeader = null;
        base.$originalHeader = null;

        // Keep track of state
        base.isCloneVisible = false;
        base.leftOffset = null;
        base.topOffset = null;

        base.init = function () {
            base.options = $.extend({}, defaults, options);

            base.$el.each(function () {
                var $this = $(this);

                // remove padding on <table> to fix issue #7
                $this.css('padding', 0);

                $this.wrap('<div class="divTableWithFloatingHeader"></div>');

                base.$originalHeader = $('thead:first', this);
                base.$clonedHeader = base.$originalHeader.clone();

                base.$clonedHeader.addClass('tableFloatingHeader');
                base.$clonedHeader.css({
                    'position': 'fixed',
                    'top': 0,
                    'z-index': 1, // #18: opacity bug
                    'display': 'none'
                });

                base.$originalHeader.addClass('tableFloatingHeaderOriginal');

                base.$originalHeader.after(base.$clonedHeader);

                // enabling support for jquery.tablesorter plugin
                // forward clicks on clone to original
                $('th', base.$clonedHeader).click(function (e) {
                    var index = $('th', base.$clonedHeader).index(this);
                    $('th', base.$originalHeader).eq(index).click();
                });
                $this.bind('sortEnd', base.updateWidth);
            });

            base.updateWidth();
            base.toggleHeaders();

            base.$window.scroll(base.toggleHeaders);
            base.$window.resize(base.toggleHeaders);
            base.$window.resize(base.updateWidth);
        };

        base.toggleHeaders = function () {
            base.$el.each(function () {
                var $this = $(this);

                var newTopOffset = isNaN(base.options.fixedOffset) ?
                    base.options.fixedOffset.height() : base.options.fixedOffset;

                var offset = $this.offset();
                var scrollTop = base.$window.scrollTop() + newTopOffset;
                var scrollLeft = base.$window.scrollLeft();

                if ((scrollTop > offset.top) && (scrollTop < offset.top + $this.height())) {
                    var newLeft = offset.left - scrollLeft;
                    if (base.isCloneVisible && (newLeft === base.leftOffset) && (newTopOffset === base.topOffset)) {
                        return;
                    }

                    base.$clonedHeader.css({
                        'top': newTopOffset,
                        'margin-top': 0,
                        'left': newLeft,
                        'display': 'block'
                    });
                    base.$originalHeader.css('visibility', 'hidden');
                    base.isCloneVisible = true;
                    base.leftOffset = newLeft;
                    base.topOffset = newTopOffset;
                }
                else if (base.isCloneVisible) {
                    base.$clonedHeader.css('display', 'none');
                    base.$originalHeader.css('visibility', 'visible');
                    base.isCloneVisible = false;
                }
            });
        };

        base.updateWidth = function () {
            // Copy cell widths and classes from original header
            $('th', base.$clonedHeader).each(function (index) {
                var $this = $(this);
                var $origCell = $('th', base.$originalHeader).eq(index);
                this.className = $origCell.attr('class') || '';
                $this.css('width', $origCell.width());
            });

            // Copy row width from whole table
            base.$clonedHeader.css('width', base.$originalHeader.width());
        };

        // Run initializer
        base.init();
    }

    // A really lightweight plugin wrapper around the constructor,
    // preventing against multiple instantiations
    $.fn[pluginName] = function ( options ) {
        return this.each(function () {
            if (!$.data(this, 'plugin_' + pluginName)) {
                $.data(this, 'plugin_' + pluginName, new Plugin( this, options ));
            }
        });
    };

})(jQuery, window);

live demo

对不起,我忘记了我是从哪里得到它的。但特别感谢他/她。还有作者,即 Jonas Mosbech。 希望,它会帮助你。谢谢。 !!

【讨论】:

    【解决方案3】:

    我编辑了你的 CSS,猜猜这就是你想要的:

    table {
      table-layout: fixed;
    }
    
    th, td {
      width: 80px;
      min-width: 80px;
      max-width: 80px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    
    .grid {
      height: 500px;
      width: 350px;
    
      overflow-x: auto;
      overflow-y: scroll;
    }
    
    .grid-canvas {
      position: relative;
      width: 100%;
      height: 100%;
    }
    
    .header {
      position: absolute;
      top: 0;
      background-color: white;
      z-index: 10;
    }
    
    .row-wrapper {
      position: absolute;
      top: 0;
      width: auto;
      height: auto;
      /* assumed height of the header */
      padding-top: 18px;
      box-sizing: border-box;
      -moz-box-sizing: border-box;
      background-color: lightgreen;
    }
    

    【讨论】:

    • 在这种情况下,标题网格不再固定。即当您向下滚动时,标题不再可见。
    • @user1447420:即使您垂直或水平滚动,标题也应该保持固定,这是不可能的解决方案。
    • 我认为您在发送此答案后添加了第 4 部分。当我回答时,它是 3 条规则。
    • 是的,我做到了。为了清楚起见,我添加了第 4 部分。看到您的回复后。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多