jquery 如何使滚动按钮消失时,滚动到底部

bsxbgnwa  于 2023-04-05  发布在  jQuery
关注(0)|答案(1)|浏览(96)

我做了一个scrollable,里面有一个浮动按钮,点击后可以滚动到底部,

<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>

当完全滚动到底部时,如何使此按钮消失?

sulc1iza

sulc1iza1#

HTML:

<button id="scrollDown" onclick="bottom()">Scroll to bottom</button>

这将创建一个按钮,单击该按钮时使用bottom()函数。
要实现按钮的功能,我们可以使用以下Javascript:

// Access button
var mybutton = document.getElementById("scrollDown");
window.onscroll = function() {scrollFunction()};

// Scroll to bottom function
function bottom() {
  if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) { 
    // If scrolled down, make button appear.
    mybutton.style.display = "block"; // Show button
  } else {
    mybutton.style.display = "none"; / Hide button
  }
}

我们可以使用CSS将按钮固定在屏幕的中心:

#scrollDown {
  display: none; // Originally hide the button. Users always begin at the top of the page
  position: fixed; // Make the button fixed.
  bottom: 20px; // Fix to bottom of screen
  right: 30px; // Fix to right of screen
  z-index: 9999; // Make sure it is at the VERY top of the screen
  font-size: 18px; // Font-size. Not required
  border: none; // Remove Border. Not required
  outline: none; // Remove Outline. Not required
  background-color: red; Red Background. Not required
  color: white; // White text. Not required
  cursor: pointer; // Show that the button is a button, by making the cursor a pointer hand.
  padding: 15px; // Padding for CSS. Not required.
  border-radius: 4px; Border-radius for looks. Not required
}

相关问题