繁体   English   中英

滚动滚动条时使广告保持在顶部可见

[英]Keep thead visible on top while scrolling the scrollbar

滚动时,我想保持表格的固定状态不变(可见)。 我花了很多时间寻找合适的解决方案,并发现了许多jquery插件,但是都无法正常工作。

我有逻辑,但是我对javascript和jquery不太满意。 我的逻辑是

我将获得我的thead的位置和scrolltop的位置。 现在,当我们移动滚动条时,我将与其一起移动thead(相同的距离),以便它始终位于顶部。

请帮助我以代码形式输入。 这将是一个很大的帮助。 或从任何建议开始也受到赞赏。

仅使用CSS,您可以为tbody设置以下属性

tbody {
    height:300px;   /*set a height*/
    overflow:auto;
    display:block;
}

DEMO

注意:这是假设您说"while scrolling"是指滚动表而不是文档。


如果您实际的意思是使标题为“ sticky”,则可以添加以下jQuery代码:

var $thead = $('thead');
var stickyThead = $thead.offset().top;
var theadWidth = $thead.width();

$(window).scroll(function () {
    if ($(window).scrollTop() > stickyThead) {
        $thead.css({
            position: 'fixed',
            top: '0px',
            width: theadWidth
        });
    } else {
        $thead.css({
            position: 'static',
            top: '0px'
        });
    }
});

DEMO

暂无
暂无

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

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