Home >Web Front-end >CSS Tutorial >How to Show a Div After Scrolling Down 800px and Hide It on Scroll Up?

How to Show a Div After Scrolling Down 800px and Hide It on Scroll Up?

Barbara Streisand
Barbara StreisandOriginal
2024-12-10 09:49:14669browse

How to Show a Div After Scrolling Down 800px and Hide It on Scroll Up?

Show Div on Scroll Down After 800px

Question:

How can I make a hidden div appear after scrolling down 800px from the top of the page? When scrolling up and the height is less than 800px, the div should disappear.

HTML:

<div class="bottomMenu">
  <!-- content -->
</div>

CSS:

.bottomMenu {
    width: 100%;
    height: 60px;
    border-top: 1px solid #000;
    position: fixed;
    bottom: 0px;
    z-index: 100;
    opacity: 0;
}

jQuery Variant for Scrolling 800px:

This jQuery code will show the div after scrolling down 800px:

$(document).scroll(function() {
  var y = $(this).scrollTop();
  if (y > 800) {
    $('.bottomMenu').fadeIn();
  } else {
    $('.bottomMenu').fadeOut();
  }
});

Scroll Event Variant for Hiding When Scrolling Up:

To hide the div when scrolling up and the height is less than 800px, use this code:

$(document).scroll(function() {
  var height = $(window).scrollTop();

  if (height > 800) {
    $('.bottomMenu').css({
      display: 'block',
      opacity: 1
    });
  } else {
    $('.bottomMenu').css({
      display: 'none',
      opacity: 0
    });
  }
});

The above is the detailed content of How to Show a Div After Scrolling Down 800px and Hide It on Scroll Up?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn