简体   繁体   English

停止相对div滚动超过特定点

[英]Stop relative divs scrolling past a certain point

How can I stop relative divs scrolling past a certain point? 如何停止相对div滚动到特定点?

Here's a fiddle to help you understand 这是一个小提琴,可以帮助您理解

Basically, I want to be able to scroll as normal, the only difference being is I don't want to see anything behind the header tag, ie as it stands when you scroll, you can see the divs through the header tag, I want it so when it scrolls, the cut off point is the bottom of the header tag so when scrolling you won't see anything past the header line. 基本上,我希望能够像往常一样滚动,唯一的区别是我不希望在header标记后面看到任何东西,即当滚动时它保持原状,您可以通过header标记看到div,我想因此,在滚动时,截止点是标题标签的底部,因此在滚动时,看不到标题行以外的任何内容。

Hope that makes sense. 希望有道理。

Here's the header css 这是标题CSS

#header {
  height:40px;
  width:100%;
  background:transparent;
  position:fixed;
  border:1px solid black;
  top:0;
}

您可以为标题设置(非transparentbackground-color ,或在标题下方创建一个新的滚动区域,并产生overflow: scroll/auto

You use jquery scrollTop for this 您为此使用jquery scrollTop

 $(window).scroll(function(){
    if($(window).scrollTop() >= 229){
      alert("in if");
      $('#header').css({position:'relative'});
    }else{
      alert("in else");
      $('#header').css({position:'fixed'});
    }
});

Fiddle 小提琴

just modify the css for #header as follows : 只需修改#header的CSS即可,如下所示:

background: white;

This happens because you have made the background transparent. 发生这种情况是因为您已使background透明。

for scrolling you can add following jQuery: 为了滚动,您可以添加以下jQuery:

var windw = this;

$.fn.followTo = function ( pos ) {
    var $this = this,
        $window = $(windw);

    $window.scroll(function(e){
        if ($window.scrollTop() > pos) {
            $this.css({
                position: 'absolute',
                top: pos
            });
        } else {
            $this.css({
                position: 'fixed',
                top: 0
            });
        }
    });
};

$('#header').followTo(250);

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

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