【问题标题】:.outerHeight & .outerWidth grows exponentially, when setting table cell height & width.outerHeight & .outerWidth 在设置表格单元格高度和宽度时呈指数增长
【发布时间】:2016-08-09 06:46:08
【问题描述】:
  • 另一位 Web 开发人员想要冻结最左侧的列并允许滚动表格。他找到了这段代码并在他客户的网站上实现了。该代码完成了它的工作并按预期工作。

  • 不过,他添加了一些 @media 查询以使表格更具响应性。这样做时,它搞砸了桌子。它使左列不与表格的其余部分对齐。

@media screen and (max-width: 699px) and (min-width: 520px) {
  .exportTable th{
    min-width: 170px !important;
  }
  .exportTable td{
    min-width:170px !important;
    max-width:170px !important;
  }
}

所以我在 window.resize() 中添加了函数调用。

$(document).ready(function () {
  app_handle_listing_horisontal_scroll($('#exportTable-listing'));
  app_handle_listing_horisontal_scroll($('#exportTable-listing2'));
  $(window).resize(function() {
    app_handle_listing_horisontal_scroll($('#exportTable-listing'));
    app_handle_listing_horisontal_scroll($('#exportTable-listing2'));
  });
});
  • 代码在调整窗口大小时被调用,但是outerHeight 和outerWidth 呈指数增长,导致了严重问题。

设置高度时会出现问题:

$(this).css('height', current_row_height);

和宽度/边距:

$(this).css('position','absolute')
              .css('margin-left','-'+table_collumns_margin[index]+'px')
              .css('width',table_collumns_width[index])

这是fiddle

只需抓住窗口并来回调整大小,表格单元格的高度/宽度将呈指数增长,导致严重问题。

This fiddle 演示了当您点击@media 查询时,左列将如何错位。 (在查看此小提琴时,重要的是要命中 @media 查询。)

我该如何解决这些问题?

【问题讨论】:

  • 你想完成什么?为防止它调整列大小,只需从 $(window).resize(function(){} 中删除您的代码
  • 该代码旨在冻结左列,它通过设置左列的宽度和高度来匹配表格的其余部分。代码完全符合预期,但是结合媒体查询,这些值不再使表格看起来不错。因此需要重新运行代码,刷新页面即可实现目标。但是,刷新窗口调整大小是不好的做法。所以我把代码放到resize中,这样代码会重新调整左列的高度和宽度以匹配表格,不希望的效果是那些高度和宽度值会增长。
  • 将js代码从resize中取出来解决指数增长的问题。但是,当 at 媒体查询被击中时,左列不再与表匹配。刷新重新加载js的页面可以修复它。所以我们需要让代码在resize中运行,让表格响应js。
  • 这不是想要的效果吗? jsfiddle.net/zzu0ojac/11左栏被冻结,页面仍然调整大小没有问题。
  • 不,一旦@media 查询被点击,左列就不会与表格的其余部分正确对齐。代码需要在窗口调整大小时运行。我在我的问题中说了所有这些......

标签: javascript jquery css


【解决方案1】:

在我看来,在等式中添加 javascript 会使它过于复杂。我相信你想要的可以只用 CSS 来完成。

这是如何工作的,如果浏览器的最大宽度为 500 像素,则删除粘性列。如果没有,请显示粘性列。

除非你需要支持 IE 9 之前的浏览器,否则试试这个大小:

https://jsfiddle.net/BmLpV/284/

CSS:

.zui-table {
    border: none;
    border-right: solid 1px #DDEFEF;
    border-collapse: separate;
    border-spacing: 0;
    font: normal 13px Arial, sans-serif;
}
.zui-table thead th {
    background-color: #DDEFEF;
    border: none;
    color: #336B6B;
    padding: 10px;
    text-align: left;
    text-shadow: 1px 1px 1px #fff;
    white-space: nowrap;
}
.zui-table tbody td {
    border-bottom: solid 1px #DDEFEF;
    color: #333;
    padding: 10px;
    text-shadow: 1px 1px 1px #fff;
    white-space: nowrap;
}
.zui-wrapper {
    position: relative;
}
.zui-scroller {
    margin-left: 141px;
    overflow-x: scroll;
    overflow-y: visible;
    padding-bottom: 5px;
    width: 300px;
}
.zui-table .zui-sticky-col {
    border-left: solid 1px #DDEFEF;
    border-right: solid 1px #DDEFEF;
    left: 0;
    position: absolute;
    top: auto;
    width: 120px;
}
@media (max-width: 500px) {
  .zui-table .zui-sticky-col {
      border-left: solid 1px #DDEFEF;
      border-right: solid 1px #DDEFEF;
      position: relative;
      top: auto;
      width: auto;
  }
  .zui-scroller {
    margin-left: 0;
  }
}

HTML:

<div class="zui-wrapper">
    <div class="zui-scroller">
        <table class="zui-table">
            <thead>
                <tr>
                    <th class="zui-sticky-col">Name</th>
                    <th>Number</th>
                    <th>Position</th>
                    <th>Height</th>
                    <th>Born</th>
                    <th>Salary</th>
                    <th>Prior to NBA/Country</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td class="zui-sticky-col">DeMarcus Cousins</td>
                    <td>15</td>
                    <td>C</td>
                    <td>6'11"</td>
                    <td>08-13-1990</td>
                    <td>$4,917,000</td>
                    <td>Kentucky/USA</td>
                </tr>
                <tr>
                    <td class="zui-sticky-col">Isaiah Thomas</td>
                    <td>22</td>
                    <td>PG</td>
                    <td>5'9"</td>
                    <td>02-07-1989</td>
                    <td>$473,604</td>
                    <td>Washington/USA</td>
                </tr>
                <tr>
                    <td class="zui-sticky-col">Ben McLemore</td>
                    <td>16</td>
                    <td>SG</td>
                    <td>6'5"</td>
                    <td>02-11-1993</td>
                    <td>$2,895,960</td>
                    <td>Kansas/USA</td>
                </tr>
                <tr>
                    <td class="zui-sticky-col">Marcus Thornton</td>
                    <td>23</td>
                    <td>SG</td>
                    <td>6'4"</td>
                    <td>05-05-1987</td>
                    <td>$7,000,000</td>
                    <td>Louisiana State/USA</td>
                </tr>
                <tr>
                    <td class="zui-sticky-col">Jason Thompson</td>
                    <td>34</td>
                    <td>PF</td>
                    <td>6'11"</td>
                    <td>06-21-1986</td>
                    <td>$3,001,000</td>
                    <td>Rider/USA</td>
                </tr>
            </tbody>
        </table>
    </div>
</div>

编辑:要支持动态高度,您可以执行以下操作:

$(function(){
  $('.zui-table tr').each(function(i, row) {
    var height = $('.zui-sticky-col').eq(i).outerHeight() - $('.zui-sticky-col').eq(i).height();
    $('.zui-sticky-col').eq(i).height($(row).height() - height);
  });
})

为了获得更好的性能,您只能访问该元素一次:

$(function(){
  $('.zui-table tr').each(function(i, row) {
    var el = $('.zui-sticky-col').eq(i);
    var height = el.outerHeight() - el.height();
    el.height($(row).height() - height);
  });
})

https://jsfiddle.net/BmLpV/289/

【讨论】:

  • 不幸的是,当您有一个更大的单元格时,该行的其余部分会错位:jsfiddle.net/BmLpV/285 这就是开发人员使用 JS 的原因。
  • 一段时间后,我设法将此代码合并到网站中。我喜欢这个解决方案。但是最终我想知道为什么他们的原始代码会出现这样的问题。如果你能给我一些关于发生了什么的信息。我可以标记已回答的问题。 Ty 为您提供所有帮助。
  • 老实说,我可能没有时间评估为什么旧代码不起作用。如您所见,解决问题的旧方法相当冗长。如果我有时间,我会尝试查看它并发表评论。感谢理解。
猜你喜欢
  • 1970-01-01
  • 2023-03-23
  • 2016-08-14
  • 2011-06-19
  • 2013-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多