简体   繁体   English

使用JS滚动时设置固定的表头

[英]To set table headers fixed while scrolling using JS

In explorer I am able to keep the thead part of my table fixed while scrolling using css expressions. 在资源管理器中,使用css表达式滚动时,我可以保持表的thead部分固定。 Below is the css code snippet that does it : 以下是执行此操作的CSS代码段:

.standardTable thead tr {
position: relative;
top: expression(offsetParent.scrollTop);
}

But this same doesn't work in chrome as expressions are deprecated for chrome. 但是,由于chrome不赞成使用表达式,因此这在chrome中不起作用。 I need help in changing the "top" property of the above class using javascript or jquery. 我需要使用javascript或jquery更改上述类的“ top”属性的帮助。

Is it possible to just change the top property in the above css using javascript or jquery. 是否可以使用javascript或jquery更改上述CSS中的top属性。

I have gone through many examples but none of them seem to be working perfectly fine. 我已经看过很多例子,但是似乎没有一个例子做得很好。

(PS :I am looking for single line of code that changes the top property of the above code) (PS:我正在寻找一行代码来更改上面代码的top属性)

Can it be done? 能做到吗

.standardTable thead tr {
    position:fixed;
    background-color: #fff;
}

Example here : http://jsfiddle.net/hweb/4wZAU/ 此处的示例: http : //jsfiddle.net/hweb/4wZAU/

<style>
#ministres thead {
    display:block;
}
#ministres tbody {
    display:block;
    height:5em; /* 5 times the equivalent of a text "size". */
    overflow-y:scroll;
}

    #ministres thead tr th:nth-child(1) { /* column 1 ! */
        width:5em;
    }
    #ministres thead tr th:nth-child(2) { /* column 2 */
        width:10em;
    }
    #ministres tbody tr:first-child td:nth-child(1) { /* column 1 ! */
        width:5em;
    }
    #ministres tbody tr:first-child td:nth-child(2) { /* column 2 */
        width:10em;
    }
</style>
<table id="ministres" border="1">
    <thead>
        <tr><th>Nom</th><th>Ministère</th></tr>
    </thead>
    <tbody>
        <tr><td>JM Ayrault</td><td>Premier</td></tr>
        <tr><td>L Fabius</td><td>A. Etrangères</td></tr>
        <tr><td>V Peillon</td><td>Education</td></tr>
        <tr><td>C Taubira</td><td>Justice</td></tr>
        <tr><td>P Moscovici</td><td>Economie</td></tr>
        <tr><td>M Valls</td><td>Intérieur</td></tr>
    </tbody>
</table>

Exemple here : http://jsfiddle.net/hweb/7eGT9/ 此处的示例: http : //jsfiddle.net/hweb/7eGT9/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM