1

How can I add a class to an element that has a fixed position, after it reaches 200px from the bottom of the page, using jquery? This is my to-top page button and I want it to be atached to the footer after its scrooled 200px until the pages end. The fixed position is 50px from the right and botom.

if (pageheight - 200px) {
 $(div1).addClass("top-position");
} else {
 $(div1).removeClass("top-position");
}
Fernando Souza
  • 1,748
  • 1
  • 20
  • 36

1 Answers1

1
$(document).scroll(function() {
    if ($(document).scrollTop()> 200) {
      $(div1).addClass("top-position");
    } else {
      $(div1).removeClass("top-position");
    }
})
Renaldo Balaj
  • 2,390
  • 11
  • 23
  • Is there a way to calculate it from the botom of the page? – Fernando Souza Nov 27 '19 at 02:22
  • 1
    only with a workaround, you can try something like this => First find the full height, minus how much he scrolled on the top $(document).height() - $(window).height() - $(window).scrollTop(); – Renaldo Balaj Nov 27 '19 at 02:25
  • 1
    https://stackoverflow.com/questions/4655273/jquery-window-scrolltop-but-no-window-scrollbottom take a look here – Renaldo Balaj Nov 27 '19 at 02:27
  • Its such a wonderfull piece of code, It could help me a lot so many times before. Could you edit your answer and add this last line please? People must see that. – Fernando Souza Nov 27 '19 at 02:48