Shrink header height by scrollTop value

前端 未结 1 1256
暗喜
暗喜 2021-01-29 03:31

Howdey!

Simple problem: I\'ve a fixed header, the height shrinks down based on the window scrollTop-value to the half of its height.

What I have so

相关标签:
1条回答
  • 2021-01-29 04:20

    Nothing wrong with the if statement. You need to add an else statement for when the initial scroll state is at the bottom. E.g.:

        var header = $('#header'),
        headerH = header.height();
    
    $(window).scroll(function() {
        if ($(this).scrollTop() <= headerH / 2) {
            header.css({
                height: -($(this).scrollTop() - headerH)
            });
        } else {
          header.css({
                height: headerH / 2
            });
        }
    }).scroll();
    
    0 讨论(0)
提交回复
热议问题