Top Related Projects
Simple & lightweight (<4kb gzipped) vanilla JavaScript library to create smooth & beautiful animations when you scroll.
Animate on scroll library
Reveal CSS animation as you scroll down a page
Animate elements as they scroll into view.
Waypoints is a library that makes it easy to execute a function whenever you scroll to an element.
The javascript library for magical scroll interactions.
Quick Overview
Headroom.js is a lightweight JavaScript library that allows for hiding and showing a fixed header on scroll. It provides a smooth user experience by automatically hiding the header when scrolling down and revealing it when scrolling up, thus maximizing screen real estate without sacrificing quick access to navigation.
Pros
- Lightweight and dependency-free
- Highly customizable with various options and callbacks
- Supports both vanilla JavaScript and as a jQuery plugin
- Responsive and works well on mobile devices
Cons
- Limited to header functionality only
- May require additional styling to integrate seamlessly with some designs
- Potential performance impact on pages with many elements
- Not actively maintained (last update was in 2017)
Code Examples
- Basic usage:
// Create a new Headroom instance
var myElement = document.querySelector("header");
var headroom = new Headroom(myElement);
headroom.init();
- With custom options:
var myElement = document.querySelector("header");
var headroom = new Headroom(myElement, {
tolerance: 5,
offset : 205,
classes : {
initial : "animated",
pinned : "slideDown",
unpinned : "slideUp"
}
});
headroom.init();
- Using as a jQuery plugin:
$("header").headroom({
tolerance: 5,
offset : 205,
classes : {
initial : "animated",
pinned : "slideDown",
unpinned : "slideUp"
}
});
Getting Started
- Include the Headroom.js library in your HTML:
<script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.12.0/headroom.min.js"></script>
- Add the following JavaScript to initialize Headroom:
document.addEventListener("DOMContentLoaded", function() {
var myElement = document.querySelector("header");
var headroom = new Headroom(myElement);
headroom.init();
});
- Style your header appropriately, ensuring it has a fixed position:
header {
position: fixed;
z-index: 10;
top: 0;
left: 0;
right: 0;
}
Competitor Comparisons
Simple & lightweight (<4kb gzipped) vanilla JavaScript library to create smooth & beautiful animations when you scroll.
Pros of lax.js
- More versatile, allowing for a wide range of scroll-based animations
- Supports multiple elements and complex animations simultaneously
- Lightweight and easy to implement with minimal setup
Cons of lax.js
- May require more configuration for simple header hiding/showing
- Could potentially impact performance with complex animations on many elements
- Less focused on specific header functionality compared to Headroom.js
Code Comparison
lax.js:
lax.addDriver('scrollY', function () {
return window.scrollY
})
lax.addElements('.selector', {
scrollY: {
translateY: [
["elInY", "elOutY"],
[0, 'screenHeight']
]
}
})
Headroom.js:
var header = document.querySelector("header");
var headroom = new Headroom(header);
headroom.init();
The code comparison shows that lax.js requires more setup but offers more customization, while Headroom.js is simpler to implement for its specific use case of header manipulation.
Animate on scroll library
Pros of AOS
- More versatile, offering various animation effects for scroll-based animations
- Easier to implement for non-developers, with simple data attributes
- Supports mobile devices and responsive design out of the box
Cons of AOS
- Larger file size, which may impact page load times
- Less focused on specific header functionality compared to Headroom.js
- May require more configuration for complex use cases
Code Comparison
Headroom.js:
var myElement = document.querySelector("header");
var headroom = new Headroom(myElement);
headroom.init();
AOS:
<div data-aos="fade-up" data-aos-duration="3000">
This element will fade up
</div>
AOS.init();
Headroom.js focuses on header behavior, while AOS provides general scroll-based animations. Headroom.js requires JavaScript initialization for each element, whereas AOS uses data attributes in HTML and a single JavaScript call to initialize all animations.
Reveal CSS animation as you scroll down a page
Pros of WOW
- Focuses on scroll-based animations, providing a wide range of animation effects
- Easy to implement with CSS classes and data attributes
- Lightweight and doesn't require jQuery
Cons of WOW
- Limited to scroll-based animations, less versatile than Headroom.js
- Requires additional CSS for animations (e.g., Animate.css)
- Less actively maintained compared to Headroom.js
Code Comparison
WOW:
new WOW().init();
<div class="wow fadeInUp" data-wow-duration="1s" data-wow-delay="0.5s">
Animated content
</div>
Headroom:
var header = document.querySelector("header");
var headroom = new Headroom(header);
headroom.init();
WOW is primarily used for scroll-based animations, while Headroom.js is designed for creating hide/show effects for headers based on scroll direction. WOW relies on CSS classes and data attributes for configuration, whereas Headroom.js uses JavaScript for more dynamic control.
Headroom.js offers more flexibility in terms of customization and can be applied to various UI elements, not just animations. It's also more actively maintained and updated.
Both libraries are lightweight and easy to implement, but they serve different purposes. Choose WOW for simple scroll-based animations and Headroom.js for more versatile header behaviors and scroll-based UI interactions.
Animate elements as they scroll into view.
Pros of ScrollReveal
- More versatile, allowing for animations on scroll for various elements, not just headers
- Offers a wider range of customization options for animations and timing
- Provides an easy-to-use API for creating complex scroll-based animations
Cons of ScrollReveal
- Larger file size, which may impact page load times
- May be overkill for simple header animations, where Headroom.js is more focused
- Requires more setup and configuration for basic use cases
Code Comparison
ScrollReveal:
ScrollReveal().reveal('.headline', {
delay: 200,
distance: '50px',
origin: 'bottom',
duration: 1000
});
Headroom.js:
var header = document.querySelector("header");
var headroom = new Headroom(header);
headroom.init();
ScrollReveal offers more detailed control over animations, while Headroom.js provides a simpler setup for header-specific behavior. ScrollReveal is better suited for complex, multi-element animations, whereas Headroom.js excels in creating efficient, performance-focused header interactions with minimal code.
Waypoints is a library that makes it easy to execute a function whenever you scroll to an element.
Pros of Waypoints
- More versatile, allowing for multiple waypoints and custom callbacks
- Supports horizontal scrolling in addition to vertical
- Offers a wider range of options for triggering events
Cons of Waypoints
- Larger file size, potentially impacting page load times
- More complex setup and configuration required
- May have higher performance overhead for multiple waypoints
Code Comparison
Headroom.js:
var header = document.querySelector("header");
var headroom = new Headroom(header);
headroom.init();
Waypoints:
var waypoint = new Waypoint({
element: document.getElementById('element'),
handler: function(direction) {
console.log('Scrolled to waypoint!')
}
})
Both libraries aim to enhance scroll-based interactions, but Waypoints offers more flexibility at the cost of complexity. Headroom.js is specifically designed for hiding/showing elements based on scroll direction, while Waypoints allows for more diverse scroll-based triggers. The code comparison shows that Headroom.js has a simpler setup, while Waypoints requires more configuration but provides greater control over the triggered actions.
The javascript library for magical scroll interactions.
Pros of ScrollMagic
- More comprehensive animation capabilities, including pin and scene management
- Supports complex scrolling-based animations and transitions
- Integrates well with popular animation libraries like GSAP
Cons of ScrollMagic
- Steeper learning curve due to more complex API
- Larger file size, which may impact page load times
- May be overkill for simple header show/hide functionality
Code Comparison
ScrollMagic:
var controller = new ScrollMagic.Controller();
var scene = new ScrollMagic.Scene({
triggerElement: "#trigger",
duration: 300
})
.setTween("#animate", {scale: 0.5})
.addTo(controller);
Headroom.js:
var myElement = document.querySelector("header");
var headroom = new Headroom(myElement);
headroom.init();
ScrollMagic offers more control over complex animations tied to scroll events, while Headroom.js provides a simpler API focused specifically on header behavior. ScrollMagic is better suited for projects requiring intricate scroll-based animations, whereas Headroom.js excels at efficiently managing header visibility with minimal setup.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Headroom.js
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.
Installation
Headroom.js is available on npm. To install:
npm install headroom.js --save
# or...
yarn add headroom.js
A a universal build (suitable for script tags, CommonJS, and AMD) is available from unpkg.com:
Documentation
For complete documentation please visit the headroom.js website.
Quick start
After installing headroom.js
. The following JS will create and initialise a headroom instance:
import Headroom from "headroom.js";
// select your header or whatever element you wish
const header = document.querySelector("header");
const headroom = new Headroom(header);
headroom.init();
Then you can add the following CSS to your page:
.headroom {
will-change: transform;
transition: transform 200ms linear;
}
.headroom--pinned {
transform: translateY(0%);
}
.headroom--unpinned {
transform: translateY(-100%);
}
You should now see your header slide in and out in response to the user's scroll.
Contributions & Issues
Contributions are welcome. Please clearly explain the purpose of the PR and follow the current code style.
Issues can be resolved quickest if they are descriptive and include both a reduced test case and a set of steps to reproduce.
Contributing Guide
Setup
The following steps will get you setup to contribute changes to this repo:
- Fork the repo (click the Fork button at the top right of this page)
- Clone your fork locally
git clone https://github.com/<your_github_username>/headroom.js.git
cd headroom.js
- Install dependencies. This repo uses
npm
, so you should too.
npm install
Building
To build the project:
npm run build
To start a watcher for building the project and running tests:
npm start
Testing
To run the test suite in headless mode:
npm test
License
Licensed under the MIT License.
Top Related Projects
Simple & lightweight (<4kb gzipped) vanilla JavaScript library to create smooth & beautiful animations when you scroll.
Animate on scroll library
Reveal CSS animation as you scroll down a page
Animate elements as they scroll into view.
Waypoints is a library that makes it easy to execute a function whenever you scroll to an element.
The javascript library for magical scroll interactions.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot