I made a scrollable with a floating button inside, which makes it scroll to bottom when clicked,
<style> box { padding: 8px 20px; border: 1px solid #222; position: sticky; bottom:10px; left:30px; box-shadow: -3px 3px 3px #999; } </style> <hr> <div id="utt" style="overflow-y: scroll; height:75%;"> <p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p><p>a</p> <a href="#" onclick="scroll" class="toBotetom"><box><b>Scroll to bottom</b></box></a> </div> <hr> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $(document).ready(function scroll() { $("a.toBotetom").click(function scroll() { let elm = $("#utt") elm.animate({ scrollTop: elm[0].scrollHeight }, 500); }); }); </script>
How can I make this button disappear when the is completely scrolled to the bottom?
Advertisement
Answer
Heading ##add this html:
<button id="scrollDown" onclick="bottom()">Scroll to bottom</button>
Then add this to Javascript:
var mybutton = document.getElementById("scrollDown"); window.onscroll = function() {scrollFunction()}; function bottom() { if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) { mybutton.style.display = "block"; } else { mybutton.style.display = "none"; } } // When the user clicks on the button, scroll to the bottom of the document function bottom() { document.body.scrollTop = 0; document.documentElement.scrollTop = 5555550; }
Now for the CSS:
#scrollDown { display: none; position: fixed; bottom: 20px; right: 30px; z-index: 99; font-size: 18px; border: none; outline: none; background-color: red; color: white; cursor: pointer; padding: 15px; border-radius: 4px; }