changing div height on scroll

后端 未结 2 1964
-上瘾入骨i
-上瘾入骨i 2021-01-13 12:50

What I want is a div at the top (header) that will be at maximum height (50px) when you first load the page, and when you\'re scrolling down the page I want the height to sm

2条回答
  •  盖世英雄少女心
    2021-01-13 13:35

    You can do this without jQuery.

    var header = document.getElementById("header_parent");
    
    window.onscroll = function(e) {
        if(window.scrollY) {
            if(window.pageYOffset > 50) {
                header.style.height = 30;
            } else {
                header.style.height = 50;
            }
        }
    }
    

    You could also toggle class names inside the if block instead of setting the height explicitly.

提交回复
热议问题