Search code examples
csshtmlfixed

How to make a fixed positioned div until some point?


I have a div with applied property position: fixed;. I need to stop this property on some height of screen scroll. Any ideas? I just need the css code only.


Solution

  • You can do it with jQuery pretty easily:

    $(window).scroll(function(){
       if ($(window).scrollTop() >= target) {  // change target to number
          $("#el").css('position', 'relative');
       }
    });
    

    or pure JavaScript:

    window.onscroll = function(){
    
       if(window.scrollY >= target) { // change target to number
          document.getElementById('el').style.position = 'relative';
       }
    
    };