Position fixed when scrolled passed certain amount of pixels

前端 未结 5 1805
傲寒
傲寒 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条回答
  •  猫巷女王i
    2021-02-06 17:44

    Here's a slightly more concise version:

    var header = $('#header'),
        bannerHeight = $('#banner').height(),
        win = $(window);
    
    win.scroll(function() {
        header.css({ top: Math.max(Number(win.scrollTop() - bannerHeight), 0) });
    });
    

提交回复
热议问题