Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display Element only when on Scroll

I have a Navaigation bar that i want to get fixed at the top only when the user scrolls the page to about 100px.

Which is the best way to achieve this?

http://play.mink7.com/sophiance/

like image 632
Harsha M V Avatar asked Jun 02 '26 08:06

Harsha M V


2 Answers

Working DEMO here...http://jsfiddle.net/eFCK3/1/

HTML

<div id="header-small">Header</div>
<div>
    <p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p><p>test</p>
</div>  

CSS

#header-small{
    display:none;background:red;padding:2%;position:fixed;top:0px;width:960%;    
}

$JQUERY

$(window).scroll(function() {
    if ($(this).scrollTop()>100) {
        $('#header-small').fadeIn();
    } else {
        $('#header-small').fadeOut();
    }
});
like image 119
yeyene Avatar answered Jun 05 '26 01:06

yeyene


 $(document).scroll(function () {
        var y = $(this).scrollTop();
        if (y > 100) {

          //when page scrolls past 100px

        } else {

          //when page within 100px

        }
    });

hope this helps

like image 38
shammelburg Avatar answered Jun 05 '26 01:06

shammelburg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!