how can I stick the div after scrolling down a little

前端 未结 4 737
温柔的废话
温柔的废话 2020-12-02 10:54

I wanted to stick the 2nd div when we scroll down the page and when the 2nd div meets the top boundary. When it\'s fixed, it should scroll along with the other pages. How ca

4条回答
  •  没有蜡笔的小新
    2020-12-02 11:20

    You can get this effect with jquery

    $(function(){
            // Check the initial Poistion of the Sticky Header
            var stickyHeaderTop = $('#stickyheader').offset().top;
    
            $(window).scroll(function(){
                    if( $(window).scrollTop() > stickyHeaderTop ) {
                            $('#stickyheader').css({position: 'fixed', top: '0px'});
                            $('#stickyalias').css('display', 'block');
                    } else {
                            $('#stickyheader').css({position: 'static', top: '0px'});
                            $('#stickyalias').css('display', 'none');
                    }
            });
      });
    

    DEMO HERE

    NOTE: Don't forget to include jquery library link in your page (assuming you as beginner)

    
    

提交回复
热议问题