html - 使表对话框内容可滚动

标签 html css dialog html-table

我有一个这样设置的对话框:

<div style="position:fixed; display:block; width:600px; top:0; left:50%;margin-left:-275px;">
  <table style="width:100%;" cellpadding="0" cellspacing="0">
    <tbody>
      <tr>
        <td>
          <div class="modal-header">
            <span class="close">×</span>
            <h2>Modal Header</h2>
          </div>
        </td>
      </tr>
      <tr>
        <td style="background-color:gray;">
          <table>
            <tbody>
              <tr>
                <td>
                  <div class="modal-body">
                    <p>variable length really long stuff</p>
                </td>
              </tr>
            </tbody>
          </table>
        </td>
      </tr>
      <tr>
        <td>
          <div class="modal-footer">
            <h3>Modal Footer</h3>
          </div>
        </td>
      </tr>
    </tbody>
  </table>
</div>

现在,对话框的内容对于页面来说太长了,而且由于位置是固定的,用户永远无法向下滚动到对话框的底部。我希望内表仅在内容长度超过视口(viewport)区域时才可滚动 - 但我始终希望页眉和页脚可见。

Here is a code sample.我怎样才能做到这一点?

最佳答案

如果您可以使用 vh 单位,您可以强制 modal-body 滚动:

.modal-body {
   padding: 2px 16px;
   overflow:auto;
   max-height: calc(100vh - 144px); /* 144px represents the combined height of the modal header and footer */
}

如果您不想使用 vh 单位,那么您可能需要在模式出现时以编程方式切换 body 上的溢出。您可能还想在更改溢出时手动设置 scrollTop 位置,这样页面就不会跳来跳去。

关于html - 使表对话框内容可滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38251102/

相关文章:

python - 将参数作为 HTML 属性传递

css - 管理所有 Assets 缓存的最佳实践(图像、CSS、JS 等)

css - twitter bootstrap 有屏和无屏的区别

jquery - 在网站中查看消息到移动浏览器

android - 如何在 BaseAdapter 中使用 Dialog?

android - 在外部单击时如何使用和关闭 PopupWindow?

html - 在表格单元格中,如何设置背景颜色并将内容放在中间?

javascript - 当父 div 具有 onclick 处理程序时,如何在动态添加的输入框中选择文本?

javascript - 轮播的分类系统?

html - 如何垂直对齐div的文本和图像中间?