Position fixed when scrolled passed certain amount of pixels

前端 未结 5 1800
傲寒
傲寒 2021-02-06 17:10

I\'m looking for a way to position the #header element of my page as \"Fixed\" only after having scrolled downward for about 170 pixels.

Above the header is a banner, so

5条回答
  •  花落未央
    2021-02-06 17:44

    This is the general idea although you may want to fudge around with the css a bit.

    var header = $("#header");
    $(document).scroll(function(e) {
        if($(this).scrollTop() > $("#banner").height()) {
            header.css({"position" : "fixed", "top" : "0"});
        } else {
            header.css("position", "relative");
        }
    });
    

提交回复
热议问题