Skip to content

Instantly share code, notes, and snippets.

@serverwentdown
Last active August 29, 2015 14:12
Show Gist options
  • Save serverwentdown/32b12ba7ecfb38940b0c to your computer and use it in GitHub Desktop.
Save serverwentdown/32b12ba7ecfb38940b0c to your computer and use it in GitHub Desktop.
Responsive floating nav.
// .site-header 24 to 18
// You *should* prefix this before using it.
var start = 439; // Starting scrollY
var end = 463; // Ending scrollY
var from = 24; // Initial value (before start)
var to = 18; // Final value (after end)
var perpx = (to - from) / (end - start);
var element = document.querySelector(".site-header");
function set(n) { // Function to set the style
element.style.fontSize = n + "px";
}
var scrollY = 0;
var ticking = false;
var update = function update() {
var now = scrollY;
if (now < start) {
set(from);
}
else if (now > end) {
set(to);
}
else {
set((now - start) * perpx + from);
}
ticking = false;
};
var requestTick = function requestTick() {
if (!ticking) {
window.requestAnimFrame(update);
ticking = true;
}
};
var onScroll = function onScroll() {
scrollY = window.scrollY;
requestTick();
};
window.addEventListener("scroll", onScroll, false);
@serverwentdown
Copy link
Author

Remember to use hardware accel!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment