jquery - 设置表格组件以使用整个视口(viewport)高度显示

标签 jquery css

我尝试在我的组件中分别使用 height: 100vhminHeight:"100vh" 但它不起作用。它仍然显示整个表格。我有最多可以显示的行,但我希望表格通过使表格主体成为唯一滚动条来避免导致页面滚动条。

我可以通过设置一个确定的高度和隐藏的溢出来实现这一点,但确定的高度是一个问题。并非每个人的视口(viewport)大小都相同,虽然它对少数人来说是完美的,但对某些人来说太小或对其他人来说太长。因此,我希望此表读取窗口高度并使用它来定义隐藏的溢出应该何时开始滚动。

我确实在 jQuery 中找到了这个

$(document).ready(function() {
    function setHeight() {
       windowHeight = $(window).innerHeight();
       $('.sidebar').css('min-height', windowHeight);
    };
    setHeight();

    $(window).resize(function() {
       setHeight();
    });
});

问题是,这似乎不适用于我的 table 。

我正在添加 codepen有一个 Playground 。

编辑:为了澄清起见,这是我的问题的可视化

enter image description here

旁注:这实际上是在 React 应用程序中,所以在它工作之后,我仍然需要在 ReactJS 中获取它,但我想,让它在 jQuery 中工作可能会更快更容易。但是,如果您碰巧在 React 中有解决方案,请分享。

$(document).ready(function() {
  function setHeight() {
    windowHeight = $(window).innerHeight();
    $('.dynamicHeight').css('min-height', windowHeight);
  };
  setHeight();
  
  $(window).resize(function() {
    setHeight();
  });
});

// If you don't care about changing the height when the window resizes then you can use the following simplified version instead:

// $(document).ready(function() {
//   windowHeight = $(window).innerHeight();
//   $('.sidebar').css('min-height', windowHeight);
// });
body {
  background-color: #ddd;
  font-size: 1.5em;
  font-family: Georgia, serif;
}

.sidebar {
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  float: left;
  width: 33%;
  padding: 2em;
  background-color: #f8ca00;
}
.dynamicHeight{
	overflow-x:hidden;
	height:100%;
	display: inline-block;
	width:100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<div class='sidebar'>
  <table class="dynamicHeight">
    <thead>
      <tr>
        <th>Header</th>
      </tr>  
    </thead>
    <tbody>
      <tr><td>Body Row</td></tr>
      <tr><td>Body Row</td></tr> 
      <tr><td>Body Row</td></tr>
      <tr><td>Body Row</td></tr> 
      <tr><td>Body Row</td></tr>
      <tr><td>Body Row</td></tr> 
      <tr><td>Body Row</td></tr>
      <tr><td>Body Row</td></tr> 
      <tr><td>Body Row</td></tr>
      <tr><td>Body Row</td></tr> 
      <tr><td>Body Row</td></tr>
      <tr><td>Body Row</td></tr> 
      <tr><td>Body Row</td></tr>
      <tr><td>Body Row</td></tr> 
      <tr><td>Body Row</td></tr>
      <tr><td>Body Row</td></tr> 
      <tr><td>Body Row</td></tr>
      <tr><td>Body Row</td></tr> 
      <tr><td>Body Row</td></tr>
      <tr><td>Body Row</td></tr> 
      <tr><td>Body Row</td></tr>
      <tr><td>Body Row</td></tr> 
      <tr><td>Body Row</td></tr>
      <tr><td>Body Row</td></tr> 
      <tr><td>Body Row</td></tr>
      <tr><td>Body Row</td></tr> 
      <tr><td>Body Row</td></tr>
      <tr><td>Body Row</td></tr> 
      <tr><td>Body Row</td></tr>
      <tr><td>Body Row</td></tr> 
      <tr><td>Body Row</td></tr>
      <tr><td>Body Row</td></tr> 
      <tr><td>Body Row</td></tr>
      <tr><td>Body Row</td></tr> 
      <tr><td>Body Row</td></tr>
      <tr><td>Body Row</td></tr> 
      <tr><td>Body Row</td></tr>
      <tr><td>Body Row</td></tr> 
      <tr><td>Body Row</td></tr>
      <tr><td>Body Row</td></tr> 
      <tr><td>Body Row</td></tr>
      <tr><td>Body Row</td></tr> 
      <tr><td>Body Row</td></tr>
      <tr><td>Body Row</td></tr> 
      <tr><td>Body Row</td></tr>
      <tr><td>Body Row</td></tr> 
      <tr><td>Body Row</td></tr>
      <tr><td>Body Row</td></tr>    
    </tbody>
    
  </table>  
</div>

最佳答案

我设置了 .sidebar 的高度,而不是 .dynamicHeight。我还将 min-height 更改为 height,因为如果内容很长,min-height 会让高度超出窗口。此外,innerHeight 仅返回一个数字,您需要向其添加 px。在 .sidebar

上使用了一些 overflow-y:auto

Javascript

$(document).ready(function() {
  function setHeight() {
    windowHeight = $(window).innerHeight();
    $('.dynamicHeight, .sidebar').css('height', windowHeight + 'px');
  };
  setHeight();

  $(window).resize(function() {
    setHeight();
  });
});

CSS

.sidebar {
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  float: left;
  width: 33%;
  padding: 2em;
  background-color: #f8ca00;
  overflow-y:auto;
}

Codepen

关于jquery - 设置表格组件以使用整个视口(viewport)高度显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46330999/

相关文章:

javascript - 点击下面的一个div元素?

javascript - 如何遍历 DOM 来获取特定元素

android - 水平两行菜单在 Android 中不起作用

css - 两种颜色边框

html - 为什么我的整个下拉列表没有显示?

javascript - Jquery 验证不适用于动态控件

javascript - 将多个具有相同结构的html标签设置为下一个div的高度

javascript - 在 javascript 中用于日期验证的正则表达式

jquery - 创建一个 css 类数组以使用通配符添加/删除它们

css - 用百分比保持/缩放 DIV 比率