Give your pages some headroom. Hide your header until you need it.
What's it all about?
Headroom.js is a lightweight, high-performance JS widget (with no dependencies!) that allows you to react to the user's scroll. The header on this site is a living example, it slides out of view when scrolling down and slides back in when scrolling up.
Why use it?
Fixed headers are a popular approach for keeping the primary navigation in close proximity to the user. This can reduce the effort required for a user to quickly navigate a site, but they are not without problems…
Large screens are usually landscape-oriented, meaning less vertical than horizontal space. A fixed header can therefore occupy a significant portion of the content area. Small screens are typically used in a portrait orientation. Whilst this results in more vertical space, because of the overall height of the screen a meaningfully-sized header can still be quite imposing.
Headroom.js allows you to bring elements into view when appropriate, and give focus to your content the rest of the time.
How does it work?
At it's most basic headroom.js simply adds and removes CSS classes from an element in response to a scroll event. This means you must supply your own CSS styles separately. The classes that are used in headroom.js that are added and removed are:
<header class="headroom">
<header class="headroom headroom--unpinned">
<header class="headroom headroom--pinned">
Relying on CSS classes affords headroom.js incredible flexibility. The choice of what to do when scrolling up or down is now entirely yours - anything you can do with CSS you can do in response to the user's scroll.
Usage
Using headroom.js is really simple. It has a pure JS API, plus an optional jQuery/Zepto plugin and AngularJS directive.
Install with npm
npm install headroom.js --save
Install with bower
bower install https://npmcdn.com/headroom.js/bower.zip --save
Using Headroom.js with a CDN
A universal build (suitable for script tags, CommonJS, and AMD) is available from npmcdn.com:
https://npmcdn.com/headroom.js
With pure JS
Include the headroom.js
script in your page, and then:
var myElement = document.querySelector("header");
var headroom = new Headroom(myElement);
headroom.init();
With jQuery/Zepto
Include the headroom.js
and jQuery.headroom.js
scripts in your page, and then:
$("header").headroom();
The plugin also offers a data-* API if you prefer a declarative approach.
<header data-headroom>
Note: Zepto's additional data module is required for compatibility.
With AngularJS
Include the headroom.js
and angular.headroom.js
scripts in your page, and include the Headroom module
angular.module('app', [
'headroom'
]);
And then use the directive in your markup:
<header headroom></header>
<headroom></headroom>
<headroom tolerance='0' offset='0' scroller=".my-scroller" classes="{pinned:'headroom--pinned',unpinned:'headroom--unpinned',initial:'headroom'}"></headroom>
Note: in AngularJS, you cannot pass a DOM element as a directive attribute. Instead, you have to provide a selector that can be passed to angular.element. If you use default AngularJS jQLite selector engine, here are the compliant selectors.
Options
Headroom.js can also accept an options object to alter the way it behaves. You can see the default options by inspecting Headroom.options
. The structure of an options object is as follows:
{
offset : 0,
tolerance : 0,
tolerance : {
down : 0,
up : 0
},
scroller : element,
classes : {
initial : "headroom",
pinned : "headroom--pinned",
unpinned : "headroom--unpinned",
top : "headroom--top",
notTop : "headroom--not-top",
bottom : "headroom--bottom",
notBottom : "headroom--not-bottom"
},
onPin : function() {},
onUnpin : function() {},
onTop : function() {},
onNotTop : function() {}
onBottom : function() {},
onNotBottom : function() {}
}
Examples
Head over to the headroom.js playroom if you want see some example usages. There you can tweak all of headroom's options and apply different CSS effects in an interactive demo.
Browser support
Headroom.js is dependent on the following browser APIs:
All of these APIs are capable of being polyfilled, so headroom.js can work with less-capable browsers if desired. Check the linked resources above to determine if you must polyfill to achieve your desired level of browser support.
Contributions & Issues
Contributions are welcome. Please clearly explain the purpose of the PR and follow the current style.
Issues can be resolved quickest if they are descriptive and include both a reduced test case and a set of steps to reproduce.
License
Licensed under the MIT License.