0

I'm trying to push up two buttons vertically displayed in order to put them above the footer content when I'm scrolling to it.

My two buttons are like this:

enter image description here

And I want them to be like that:

enter image description here

Any ways to make it with Javascript?

Thanks a lot !

1 Answers1

1

Since you provided no code at all, here's my solution based on the assumption your buttons are in a parent div and that div has position: fixed and a right / bottom -property set to a certain amount of pixel.

window.onscroll = function (ev) {
  if ((window.innerHeight + window.pageYOffset) >= document.body.offsetHeight - 2) {
    document.getElementById("buttons").style.bottom = "100px";
  } else {
    document.getElementById("buttons").style.bottom = "20px";
  }
}

/*

window.onscroll = function (ev) {
  let footerHeight = document.getElementsByTagName("footer")[0].offsetHeight;
  if ((window.innerHeight + window.pageYOffset) >= (document.body.offsetHeight - footerHeight)) {
    document.getElementById("buttons").style.bottom = "100px";
  } else {
    document.getElementById("buttons").style.bottom = "20px";
  }
}

*/
main {
  height: 1000px;
}
#buttons {
  position: fixed;
  bottom: 20px;
  right: 20px;
}

button {
  background-color: grey;
  border-radius: 50%;
  display: block;
  border: none;
  padding: 15px;
  margin-top: 10px;
}
<main></main>
<div id="buttons">

  <button></button>
  <button></button>

</div>

window.onscroll fires of at every scroll-event. It then checks, if the height of the window (window.innerHeight) and the distance scrolled so far (window.pageYOffset) are greater or equal to the total height of the body (document.body.offsetHeight)(- 2) is added because of an annoying mac-'feature'. For more look at this post). If thats the case, it moves the buttons up 100px instead of the 20px normally. If you dont add the else-statement, your buttons will stay at the position even if you scroll up again.

You can now get a bit creative. If you dont want to hit rock bottom of the page to make the buttons move, change the - 2. So you check for the height of your footer, and substract it from the total body height. Your buttons then start to move once the footer is it. Example of that in the javascript snippet, the part that is commented out.

iwillieyou
  • 59
  • 8
  • why not `21px` or `19px` ? No offense but your answer should've been posted as a comment regardless of the magic number you used (`20px`). – ThS Sep 07 '22 at 21:27
  • I would have loved to comment, but i need one more reputation-point to do so. Also, as the questioner is using position absolute to display his buttons und the buttom right. Im sure he is capable of adjusting the pixels and understands what the 20 px means. But you are right my answer as an answer is quite underwhelming and should have been a comment – iwillieyou Sep 07 '22 at 21:30
  • Yes they have an absolute position. I know how to push them up but I want to do it only when an user scrolls to the footer. – Valentin Grenier Sep 07 '22 at 21:31
  • @iwillieyou Thank you for your kind response. Sorry didn't know you have less reputation score than what is required to comment. My comment was just in an attempt to make this site better for us and those who will see that post one day. Sorry if my comment was somehow offensive (which honestly i truly didn't mean). – ThS Sep 07 '22 at 21:32
  • 1
    No worries, my answer was lazy, i edited it to clarify the effects of `bottom: 20px` have a nice one :) – iwillieyou Sep 07 '22 at 21:34
  • I updated my answer, this should be what you were initially looking for. – iwillieyou Sep 07 '22 at 22:21