changing div height on scroll

后端 未结 2 1941
-上瘾入骨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

    I've added to #body_parent height to see the scroll, you can delete that row with height after you create the site.

    Here is jsfiddle

    $(window).on('scroll', function () {
        var scrollTop = $(window).scrollTop();
        if (scrollTop > 50) {
            $('#header_parent').stop().animate({height: "30px"},200);
        }
        else {
             $('#header_parent').stop().animate({height: "50px"},200);   
        }
    });
    * {
        margin:0 auto;
        padding:0;
    }
    
    #header_parent {
        max-width:1250px;
        min-width:750px;
        height:50px;
        background:#000;
        position:fixed;
    }
    
    #body_parent {
         height:1200px;   
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <body>
        <!-- Header -->
        <div id="header_parent">
            
        </div>
        
        <!-- Body (homepage) -->
        <div id="body_parent">
            
        </div>
        
        <!-- Footer -->
        <div id="footer_parent">
            
        </div>
        
    </body>

    Or HTML

    <html>
        <head>
            <link rel="stylesheet" type="text/css" href="css/main.css">
        </head>
    
        <body>
            <!-- Header -->
            <div id="header_parent">
    
            </div>
    
            <!-- Body (homepage) -->
            <div id="body_parent">
    
            </div>
    
            <!-- Footer -->
            <div id="footer_parent">
    
            </div>
    
         <!-- jQuery library -->
         <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
         <!-- Your jQuery/Javascript code -->
         <script  type="text/javascript">
         $(window).on('scroll', function () {
            var scrollTop = $(window).scrollTop();
            if (scrollTop > 50) {
                $('#header_parent').stop().animate({height: "30px"},200);
            }
            else {
                 $('#header_parent').stop().animate({height: "50px"},200);   
            }
         });
         </script>
        </body>
    </html>
    

    And if you want to set smoothness replace 200 with your number, 200 mean duration in miliseconds.

    $('#header_parent').stop().animate({height: "50px"},200); 
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题