javascript - 表固定头 : Should move when scrolling horizontally

标签 javascript html css

我有一个严重的问题,我无法解决,我希望你能帮助我.. 所以我有一个垂直滚动的固定标题表(下面的 CSS 和 Javascript)但是当我水平滚动时我的标题没有移动..

    window.onscroll = function() {myFunction()};
                var header = document.getElementById('myHeader');
                var sticky = header.offsetTop;
                function myFunction() {
                    if (window.pageYOffset > sticky) {
                        header.classList.add('sticky');
                    } else {
                        header.classList.remove('sticky');
                    }
                }
    .sticky {
                  position: fixed;
                  top: 0;
                  z-index:1000;
                  padding-left:62px;
                  overflow-x: scroll;
                }


    
    
    <table id='table' class='display cell-border'>  
                    <thead class='head' id='myHeader'>
                        <tr>
                            <th class='noSearch'></th>
                            <th class='noSearch'></th>
                            <th>Shell ID</th>
                            <th>Package ID</th>
                            <th>Package Title</th>
                            <th>Product Type</th>
                            <th>Project</th>
                            <th>Current Phase</th>
                            <th>Status</th>
                        </tr>
</thead>
    <tr>
    <td>Shell ID</td>
                            <td>Package ID</td>
                            <td>Package Title</td>
                            <td>Product Type</td>
                            <td>Project</td>
                            <td>Current Phase</td>
                            <td>Status</td>
                    </tr>
          </table>

我希望你能帮助我.. 干杯

最佳答案

试试这个方法:https://codepen.io/anon/pen/vVGeVM

<!------------------------------Css-------------------->
.tableContainer{
  overflow: auto;
  height:100px;
}
td, th{
  width:80px;
  text-align:center;
}
<!------------------------html-------------------------->

<table id='table' class='display cell-border'>  
    <thead class='head' id='myHeader'>
            <tr>
                <th class='noSearch'></th>
                <th class='noSearch'></th>
                <th>Shell ID</th>
                <th>Package ID</th>
                <th>Package Title</th>
                <th>Product Type</th>
                <th>Project</th>
                <th>Current Phase</th>
                <th>Status</th>
             <tr>
 </thead>
 <tbody>
   <tr>
    <td colspan="12">
      <div class="tableContainer">
        <table>
          <tr>
            <td>test</td>
             <td>test</td>
             <td>test</td>
             <td>test</td>
             <td>test</td>
             <td>test</td>
             <td>test</td>
             <td>test</td>
             <td>test</td>
          </tr>
        </table>
      </div>
     </td>
   </tr>
 </tbody>
</table>

关于javascript - 表固定头 : Should move when scrolling horizontally,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52640907/

相关文章:

css动画淡入,但不会淡出

CSS3 不再是 FIRST-CHILD

javascript - 复制并在悬停时显示图像

javascript - 在 JavaScript 中使用 isPrototypeOf 来检查变量是否属于特定类型的任何注意事项?

javascript - jquery Masonry,在div的中心添加一个新的div#container

javascript - 如何动态地将类添加到焦点输入字段的父 div?

javascript - 使用 jQuery 加载 LessCSS

jquery - 单击时在表中求和值

javascript - 根据变量改变 React 中组件的顺序?

html - 添加文本内容后div悬停效果改变