繁体   English   中英

滚动时固定标题

[英]Fixed header while scroll

我在页面中间有一个表格的标题,但是由于页面很大,所以我想在向下滚动页面时将标题固定在浏览器的顶部。

所以我的问题是:如何设置标题为正常,直到用户向下滚动并且标题的顶部边框接触到浏览器边框,无论用户向下滚动多远,标题都应固定在该位置?

让我解释一下如何做到这一点。

脚步

  1. 找到表格标题,并保存其position
  2. 将侦听器添加到窗口的scroll事件。
  3. 根据您的表格标题位置检查窗口滚动
    1. 如果位置<窗口滚动-添加一个类以修复表头
    2. 否则,将css重置为正常标头。

我张贴了一个小提琴, 您可以在这里找到

代码样例

的HTML

<div class='lots_of_stuff_in_here'> ... </div>
<table>
    <thead id='my_fixable_table_header'>
        <tr>
            <th>My awsesome header number 1</th>
            <th>My awsesome header number 2</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Content</td>
            <td>Content</td>
        </tr>
        // much more content
    </tbody>
</table>

Java脚本

// Just so you get the idea behind the code

var myHeader = $('#my_fixable_table_header');
myHeader.data( 'position', myHeader.position() );
$(window).scroll(function(){
    var hPos = myHeader.data('position'), scroll = getScroll();
    if ( hPos.top < scroll.top ){
        myHeader.addClass('fixed');
    }
    else {
        myHeader.removeClass('fixed');
    }
});

function getScroll () {
    var b = document.body;
    var e = document.documentElement;
    return {
        left: parseFloat( window.pageXOffset || b.scrollLeft || e.scrollLeft ),
        top: parseFloat( window.pageYOffset || b.scrollTop || e.scrollTop )
    };
}

您正在寻找一个水平定向的“粘滞盒”,该粘滞盒可在滚动时跟随您进入页面。

这是一个演练,解释了如何为侧边栏创建此效果: http : //css-tricks.com/scrollfollow-sidebar/

我修改了代码以使用跨页面宽度的通用示例:

HTML:

<div class="wrapper">
  <div class="head">HEAD</div>
  <div class="header">Table Header</div>
  <div class="content">Content</div>
  <div class="footer">Footer</div>
</div>​

CSS:

.wrapper {
  border:1px solid red;
}
.head{
  height: 100px;
  background: gray;
}
.header {
  background:red;
  height:100px;
  left:0;
  right:0;
  top:0px;
  margin-top:100px;
  position:absolute;
}

.content {
   background:green;
   height:1000px;
}

.footer {
   background:blue;
   height:100px;
}

jQuery的:

$(function() {

    var $sidebar = $(".header"),
        $window = $(window),
        offset = $sidebar.offset(),
        topPadding = 0;

    $window.scroll(function() {
        if ($window.scrollTop() > offset.top) {
            $sidebar.stop().animate({
                top: $window.scrollTop() - offset.top + topPadding
            });
        } else {
            $sidebar.stop().animate({
                top: 0
            });
        }
    });

});​

当您滚动到其最初显示的位置时,这将使标题块产生动画效果。

jsFiddle在这里

您可以尝试将标题下面的底部元素括入一个div中,然后将类添加到该div中,将溢出设置为auto

希望您看起来像这个Header Fix Demo

的HTML

<div class="wrapper">
<div class="header">Header Fix</div>
<div class="content">Content</div>
<div class="footer">Footer</div>
</div>

的CSS

.wrapper {
  border:1px solid red;
}

.header {
  background:red;
  height:100px;
  position:fixed;
  left:0;
  right:0;
  top:0;

}

.content {
   background:green;
   height:1000px;
}

.footer {
   background:blue;
   height:100px;
}

为了使任何块元素的位置保持固定,您需要在style的display属性中使用absolute或fixed属性,但不要忘记给足够的空间并中断顶部元素,否则它将进入标题部分。

$(window).scroll(function() {
 if ($(this).scrollTop() > 100){  
    $('header').addClass("sticky");
  }
  else{
    $('header').removeClass("sticky");
  }
});

CSS:

header.sticky {
  font-size: 24px;
  line-height: 48px;
  height: 48px;
  background: #efc47D;
  text-align: left;
  padding-left: 20px;
}

暂无
暂无

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

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