How can I make a div stick to the top of the screen once it's been scrolled to?

后端 未结 21 1838
Happy的楠姐
Happy的楠姐 2020-11-22 07:12

I would like to create a div, that is situated beneath a block of content but that once the page has been scrolled enough to contact its top boundary, becomes fixed in place

21条回答
  •  我寻月下人不归
    2020-11-22 07:53

    You can add 3 extra rows so when the user scroll back to the top, the div will stick on its old place:

    Here is the code:

    if ($(this).scrollTop() < 200 && $el.css('position') == 'fixed'){
        $('.fixedElement').css({'position': 'relative', 'top': '200px'});
    }
    

提交回复
热议问题