Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show div after scrolling 100px from the top of the page

I found this, but this does it 100px before the bottom of the page. I need it 100px from the top of the page. I know how to implement it, I've done other jquery animations, just not what needs to be in this one.

$(window).scroll(function(){
  if($(window).scrollTop() + 100 > $(document).height() - $(window).height() ){

    alert("at bottom");

  }
});

And also, I need to know how to reverse this so the div disappears when the user scroll back up before the 100px.

This will be used for a navigation bar.

Edit2> This worked also:

$(window).scroll(function(){
  if($(window).scrollTop() > 100){
      $("#div").fadeIn("slow");
  }
});
$(window).scroll(function(){
  if($(window).scrollTop() < 100){
      $("#div").fadeOut("fast");
  }
});
like image 495
DiscoveryOV Avatar asked Nov 02 '12 12:11

DiscoveryOV


People also ask

How can I make a div stick to the top of the screen once it's been scrolled to in react?

Just replace #sticky_div's_name_here with the name of your div, i.e. if your div was <div id="example"> you would put #example { position: sticky; top: 0; } .

How do I display the content while scrolling in HTML?

First wrap whatever your text or content that you want to show on scroll, in one div so that you can show hide the div depending upon the scroll. Write two classes for your target div. Hope it will solve your problem.

How do I scroll a div to the top?

Use the scrollTo() Method to Scroll to Top of Div in JavaScript. The scrollTo() method takes parameters to reset the viewport measurements. Usually, the current state of the viewport will have its position on the x-axis and y-axis.


1 Answers

Try this:

$(window).scroll(function() {
    if ($(window).scrollTop() > 100) {
        // > 100px from top - show div
    }
    else {
        // <= 100px from top - hide div
    }
});
like image 165
Rory McCrossan Avatar answered Sep 29 '22 23:09

Rory McCrossan