Question

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/

Was it helpful?

Solution 2

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

          //when page scrolls past 100px

        } else {

          //when page within 100px

        }
    });

hope this helps

OTHER TIPS

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();
    }
});

Add a scroll-Handler using jQuery.
$("html, body").scroll(yourHandler() {});
Then simply check for the scroll-position via
$("html, body").scrollTop();
Determine if that is scrolled as far as you want it to go and then add a css-Class to the navigation bar which adds the fixed-attribute for example or something more complex if you desire.

Don´t forget to remove the class or any other changes you did again if the scrolls back again.

This will stick the navigation top the top of window the moment it reaches the top.Hope it helps.

 var $window = $(window),
           $navigation = $('#navigation'),
           elTop = $navigation.offset().top;

       $window.scroll(function() {
            $navigation.toggleClass('fixed', $window.scrollTop() > elTop);
        });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top