简体   繁体   English

滚动时在顶部固定某些类的表行

[英]Fix table row with certain class at top when scrolling

I have a HTML table made with Javascript. 我有一个用Javascript制作的HTML表。 http://jsfiddle.net/T9Bhm/3179/ http://jsfiddle.net/T9Bhm/3179/

I want to be able to scroll and keep a certain row at the top (not a header, a row). 我希望能够滚动并在顶部保留特定的行(而不是标题,而是行)。 I have tried this : 我已经试过了:

 $(window).bind('scroll', function () {
     console.log('menuTop')
     if ($(window).scrollTop() > 10) {
         $('.menuTop').addClass('fixed');
     } else {
         $('.menuTop').removeClass('fixed');
     }
 });

This doesn't seem to work. 这似乎不起作用。 I wan tthe numbers and letters to stay at the top when on scroll (the ones classed as 'menuTop'). 当滚动时,我希望数字和字母保持在顶部(归类为“ menuTop”的那些)。 But on scroll they all seem to fold in to one on the left hand side. 但是在滚动时,它们似乎都在左侧折叠成一个。 The 'H' being on top. “ H”在最前面。

Any ideas ? 有任何想法吗 ?

There's no simple way to do this purely with HTML/CSS. 没有简单的方法可以完全使用HTML / CSS做到这一点。 It requires a JavaScript solution. 它需要一个JavaScript解决方案。 The basic methodology would be to clone the header cells using <div> or other block elements, and position the clones absolutely or fixed. 基本方法是使用<div>或其他块元素克隆标头单元,并完全或固定定位克隆。

There are a number of plugins out there that can do this work for you. 有一个数量插件能为你做这项工作。 If you roll your own, you should try to write some code and post here only when you get stuck with a specific, reproducible error. 如果自己动手,则应该尝试编写一些代码,然后仅在遇到特定的,可重现的错误时才在此处发布。

I think you something like this, if yes then you need to add class in 10th tr . 我认为您是这样的,如果是的话,那么您需要在10th tr添加类。

.fixed{
    display: table;
    left: 0;
    position: fixed;
    top: 0;
    width: 824px;
}

jsfiddle ink jsfiddle墨水

Hope it will helps you. 希望对您有帮助。

Need to move your fixed to the row, instead of the cells and you need to have top and left style attributes set instead of letting it determine because each browser will set fixed items with user-agent defaults. 需要将固定内容移动到行而不是单元格,并且需要设置顶部和左侧样式属性,而不是让它确定,因为每个浏览器都会使用用户代理默认值设置固定项。

As well, a table width expands from content filling the cells, the cells on that row will not stay the same width unless you detect the "column-sibling" from calculated row-cell widths with JS and set each row-cell width in that row. 同样,表格宽度从填充单元格的内容扩展而来,该行的单元格将不会保持相同的宽度,除非您使用JS从计算出的行单元格宽度中检测到“ column-sibling”,并在其中设置每个行单元格宽度行。

I have managed to solve it with the help of all the other answers. 我设法在所有其他答案的帮助下解决了这个问题。

Updated fiddle : http://jsfiddle.net/T9Bhm/3183/ 更新的小提琴: http : //jsfiddle.net/T9Bhm/3183/

With this though, as mention by 'Twintails', I have to explicitly set the columns width, which is a pain if I'm honest. 但是,正如“ Twintails”所提到的,我必须显式设置列的宽度,如果老实说,这很麻烦。

The way I did it was instead of applying the fixed position to each cell in the row, I gave the class to the whole row. 我这样做的方法不是将固定位置应用于行中的每个单元,而是将类分配给整个行。 Then set these attributes in the css : 然后在css中设置这些属性:

.fixed{    
     display: table;
    left: 0;
    position: fixed;
    top: 0;
    width: 95.72%;

}

Another thing that should never be done, is set the width in the css the way I have done here. 永远不应该做的另一件事是按照我在这里所做的方法在CSS中设置宽度。 However, it helps me in my situation. 但是,这对我有帮助。

Hopefully this helps someone else :) 希望这可以帮助其他人:)

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

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