Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scroll then snap to top

Just wondering if anyone has an idea as to how I might re-create a nav bar style that I saw a while ago, I just found the site I saw it on, but am not sure how they might have gotten there. Basically want it to scroll with the page then lock to the top...

http://lesscss.org/

like image 395
mediarts Avatar asked Feb 26 '11 01:02

mediarts


People also ask

What is scroll snap?

The scroll-snap-type CSS property sets how strictly snap points are enforced on the scroll container in case there is one.

What is scroll margin top?

The scroll-margin-top property is used to set all the scroll margins to the top of an element at once. The value specified for the scroll-margin-top determines how much of the page that is primarily outside the support should remain visible.

How do you use scroll behavior?

Definition and UsageThe scroll-behavior property specifies whether to smoothly animate the scroll position, instead of a straight jump, when the user clicks on a link within a scrollable box.


2 Answers

Just do a quick "view source" on http://lesscss.org/ and you'll see this:

window.onscroll = function () {
    if (!docked && (menu.offsetTop - scrollTop() < 0)) {
        menu.style.top = 0;
        menu.style.position = 'fixed'; 
        menu.className = 'docked'; 
        docked = true;
    } else if (docked && scrollTop() <= init) { 
        menu.style.position = 'absolute'; 
        menu.style.top = init + 'px';
        menu.className = menu.className.replace('docked', ''); 
        docked = false;  
    }
};

They're binding to the onscroll event for the window, this event is triggered when the window scrolls. The docked flag is set to true when the menu is "locked" to the top of the page, the menu is set to position:fixed at the same time that that flag is set to true. The rest is just some simple "are we about to scroll the menu off the page" and "are we about back where we started" position checking logic.

You have to be careful with onscroll events though, they can fire a lot in rapid succession so your handler needs to be pretty quick and should precompute as much as possible.

In jQuery, it would look pretty much the same:

$(window).scroll(function() {
    // Pretty much the same as what's on lesscss.org
});

You see this sort of thing quite often with the "floating almost fixed position vertical toolbar" things such as those on cracked.com.

like image 168
mu is too short Avatar answered Sep 30 '22 01:09

mu is too short


mu is too short answer is working, I'm just posting this to give you the jquery script!

var docked = false;
var menu = $('#menu');
var init = menu.offset().top;

$(window).scroll(function() 
{       
        if (!docked && (menu.offset().top - $("body").scrollTop() < 0)) 
        {
            menu.css({
                position : "fixed",
                top: 0,
            });
            docked = true;
        } 
        else if(docked && $("body").scrollTop() <= init)
        {
            menu.css({
                position : "absolute",
                top: init + 'px',
            });

            docked = false;
        }
});
like image 32
jurrieb Avatar answered Sep 30 '22 02:09

jurrieb