防止粘滞表标题在滚动条上向上移动

时间:2020-07-09 02:47:00

标签: html css reactjs sass

我目前有一个支持元素溢出的滚动表。我需要在用户滚动时使标头保持稳定,但是由于某些原因,标头在粘贴之前会在10px上向上移动。

GIF Demo

HTML

<div className='tasks-container'>
    <table className='task-table'>
        <tbody>
            <tr className='tasks-header'>
                <th>Store</th>
                <th>Product</th>
                <th>Size</th>
                <th>Profile</th>
                <th>Proxies</th>
                <th>Status</th>
                <th>Actions</th>
             </tr>
             {this.renderTaskData()}
        </tbody>
   </table>
</div>

this.renderTaskData()可以通过ReactJS在td内部使用tr来简单地渲染表元素。

SASS

@import '../../styles/fonts.scss';
@import '../../styles/colors.scss';

.tasks-container {
  height: 80vh;
}

.task-table {
  margin-left: 85px;
  margin-top: 30px;
  width: calc(100% - 107px);
  user-select: none;
  border-spacing: 0 10px;

  display: block;
  overflow-y: auto;
  overflow-x: hidden;
  height: 100%;

  tbody {
    display: table;
    width: 100%;
  }

  &::-webkit-scrollbar {
    display: none;
  }

  -ms-overflow-style: none;
}

.tasks-header {
  font-family: 'Muli', sans-serif;
  font-size: 12px;
  font-weight: $extra-bold;
  color: $text-light;
  letter-spacing: 1px;
  text-align: left;

  th {
    padding-left: 36px;
    position: -webkit-sticky;
    position: sticky;
    top: 0;
    z-index: 5;
    background-color: $background;
    padding-bottom: 10px;
  }
}

td {
  color: $text-grey;
  font-family: 'Muli', sans-serif;
  font-size: 12px;
  font-weight: $extra-bold;
  letter-spacing: 1px;
  text-align: left;
  background-color: $input-box;
  height: 45px;
  padding-left: 36px;
  white-space: nowrap;

  &:first-child {
    border-top-left-radius: 7px;
    border-bottom-left-radius: 7px;
  }

  &:last-child {
    border-top-right-radius: 7px;
    border-bottom-right-radius: 7px;
  }

  p {
    display: inline-block;
    margin-left: 10px;
  }
}

.task-container {
  transition: 0.3s;

  &:hover {
    filter: brightness(115%);
  }
}

.Idle {
  div {
    background-color: $status-yellow;
    height: 8px;
    width: 8px;
    border-radius: 50%;
    display: inline-block;
  }

  p {
    color: $status-yellow;
  }
}

我认为问题是border-spacing类下的task-table,因为当我删除它并添加border-spacing: 0border-collapse: collapse时,行丢失了间距,但是滚动问题解决了。​​

如何在保持边框间距的同时解决此问题?

0 个答案:

没有答案