使用jQuery动态将表格页脚移动到滚动div底部

6
我有一个场景,需要将表格底部行的每个th标签移动到滚动div的底部。以下是plnkr链接。我可以使用硬编码来移动它。
$('.sticky-table').find("table tfoot tr.sticky-row th").css('top', 260);

但我想计算260并且做到它。需要帮助。


计算是为了什么?你是指根据有多少行来计算需要移动到底部的距离吗? - zer00ne
你试过使用 .css('bottom', 0); 来定义距离底部的距离吗?这样比定义距离顶部更方便。 - Adriano
不需要使用jQuery来解决这个问题。只需要几行简单的CSS就可以了。我提供了一个展示这种方法的解决方案。 - Brett DeWoody
2个回答

5

您需要计算整个容器的底部位置,然后从中减去页脚、页眉和水平滚动条的高度。这将得到页脚行 th 元素的顶部位置。

$('.sticky-table.sticky-headers').offset().top //top of the container
+ $('.sticky-table.sticky-headers').outerHeight() //height of the container (adding it with top gives you the bottom position of the container)
- $('.sticky-table').find("table tfoot tr.sticky-row th").outerHeight(true) //height of the footer headers
- 11 //Fixed height of the scrollbar

更新了plunker


谢谢。我通过这段代码实现了创建固定表头、列和页脚的表格的目标。 - Ghazanfar Khan

2
这可以通过几行简单的CSS代码实现。这能够消除在jQuery中基于高度和位置进行的复杂计算的需要,并且如果需要,具有响应式的附加优势。目标是将tfoot元素绝对定位在.sticky-table元素的底部。为此,我们可以给.sticky-table设置position:relative;,并为tfoot设置position:absolute; bottom:0;
.sticky-table {
  /* ...existing styles */
  position: relative;
}

.sticky-table tfoot {
  position: absolute;
  bottom: 0;
}    

像这样:

/* Styles go here */

.sticky-table {
  position: relative;
  max-width: 100%;
  max-height: 500px;
  height: 500px;
  overflow: auto;
  border-top: 1px solid #ddd;
  border-bottom: 1px solid #ddd;
  padding: 0 !important;
}

.sticky-table table {
  margin-bottom: 0;
  width: 100%;
  max-width: 100%;
  border-spacing: 0;
}

.sticky-table table tr.sticky-row th,
.sticky-table table tr.sticky-row td {
  background-color: #fff;
  border-top: 0;
  position: relative;
  outline: 1px solid #ddd;
  z-index: 5;
}

.sticky-table table td.sticky-cell,
.sticky-table table th.sticky-cell {
  background-color: #fff;
  outline: 1px solid #ddd;
  position: relative;
  z-index: 10;
}

.sticky-table table tr.sticky-row td.sticky-cell,
.sticky-table table tr.sticky-row th.sticky-cell {
  z-index: 15;
}

.sticky-table tfoot {
  position: absolute;
  bottom: 0;
}

.sticky-table::-webkit-scrollbar {
  width: 0.7em;
  height: 0.7em;
}

.sticky-table::-webkit-scrollbar-track {
  -webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
}

.sticky-table::-webkit-scrollbar-thumb {
  background-color: #b37e7e;
  outline: 1px solid slategrey;
  border-radius: 5px;
}
<div class="row">
  <div class="col-md-12">
    <div class="sticky-table sticky-headers">
      <table class="table table-striped table-striped">
        <thead>
          <tr class="sticky-row">
            <th>Campaign Name</th>
            <th>Ad Sets</th>
            <th>Ads</th>
            <th>Blue</th>
            <th>2000</th>
            <th>Ford</th>
            <th>Escort</th>
            <th>Blue</th>
            <th>2000</th>
            <th>Ford</th>
            <th>Escort</th>
            <th>Blue</th>
            <th>2000</th>
            <th>2000</th>
            <th>Ford</th>
            <th>Escort</th>
            <th>Blue</th>
            <th>2000</th>
            <th>2000</th>
            <th>Ford</th>
            <th>Escort</th>
            <th>Blue</th>
            <th>2000</th>
            <th>2000</th>
            <th>Ford</th>
            <th>Escort</th>
            <th>Blue</th>
            <th>2000</th>
            <th>2000</th>
            <th>Ford</th>
            <th>Escort</th>
            <th>Blue</th>
            <th>2000</th>
            <th>2000</th>
            <th>Ford</th>
            <th>Escort</th>
            <th>Blue</th>
            <th>2000</th>

          </tr>
        </thead>
        <tbody>
          <tr>
            <td class="sticky-cell">Demo Campaign</td>
            <td class="sticky-cell">100</td>
            <td class="sticky-cell">200</td>
            <td>2000</td>
            <td>Ford</td>
            <td>Escort</td>
            <td>Blue</td>
            <td>2000</td>
            <td>Ford</td>
            <td>Escort</td>
            <td>Blue</td>
            <td>2000</td>
            <td>2000</td>
            <td>Ford</td>
            <td>Escort</td>
            <td>Blue</td>
            <td>2000</td>
            <td>2000</td>
            <td>Ford</td>
            <td>Escort</td>
            <td>Blue</td>
            <td>2000</td>
            <td>2000</td>
            <td>Ford</td>
            <td>Escort</td>
            <td>Blue</td>
            <td>2000</td>
            <td>2000</td>
            <td>Ford</td>
            <td>Escort</td>
            <td>Blue</td>
            <td>2000</td>
            <td>2000</td>
            <td>Ford</td>
            <td>Escort</td>
            <td>Blue</td>
            <td>2000</td>
            <td>2000</td>
          </tr>
          <tr>
            <td class="sticky-cell">Demo Campaign</td>
            <td class="sticky-cell">100</td>
            <td class="sticky-cell">200</td>
            <td>Blue</td>
            <td>2000</td>
            <td>Ford</td>
            <td>Escort</td>
            <td>Blue</td>
            <td>2000</td>
            <td>Ford</td>
            <td>Escort</td>
            <td>Blue</td>
            <td>2000</td>
            <td>2000</td>
            <td>Ford</td>
            <td>Escort</td>
            <td>Blue</td>
            <td>2000</td>
            <td>2000</td>
            <td>Ford</td>
            <td>Escort</td>
            <td>Blue</td>
            <td>2000</td>
            <td>2000</td>
            <td>Ford</td>
            <td>Escort</td>
            <td>Blue</td>
            <td>2000</td>
            <td>2000</td>
            <td>Ford</td>
            <td>Escort</td>
            <td>Blue</td>
            <td>2000</td>
            <td>2000</td>
            <td>Ford</td>
            <td>Escort</td>
            <td>Blue</td>
            <td>2000</td>

          </tr>
        </tbody>
        <tfoot>
          <tr class="sticky-row">
            <th class="sticky-cell">Demo Campaign</th>
            <th class="sticky-cell">100</th>
            <th class="sticky-cell">200</th>
            <th>Blue</th>
            <th>2000</th>
            <th>Ford</th>
            <th>Escort</th>
            <th>Blue</th>
            <th>2000</th>
            <th>Ford</th>
            <th>Escort</th>
            <th>Blue</th>
            <th>2000</th>
            <th>2000</th>
            <th>Ford</th>
            <th>Escort</th>
            <th>Blue</th>
            <th>2000</th>
            <th>2000</th>
            <th>Ford</th>
            <th>Escort</th>
            <th>Blue</th>
            <th>2000</th>
            <th>2000</th>
            <th>Ford</th>
            <th>Escort</th>
            <th>Blue</th>
            <th>2000</th>
            <th>2000</th>
            <th>Ford</th>
            <th>Escort</th>
            <th>Blue</th>
            <th>2000</th>
            <th>2000</th>
            <th>Ford</th>
            <th>Escort</th>
            <th>Blue</th>
            <th>2000</th>

          </tr>
        </tfoot>
      </table>
    </div>
  </div>

</div>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接