April 14, 2016

Bumping refresh rate when scrolling

We can manually call window.equestAnimationFrame from the scroll event in each tick. Tested on Firefox 23.

(function () {
	var lastScrollY = 0;
	var ticking = false;

	var update = function () {
		// do your stuff
		ticking = false;
	};

	var requestTick = function () {
		if (!ticking) {
			window.requestAnimationFrame(update);
			ticking = true;
		}
	};

	var onScroll = function () {
		lastScrollY = window.scrollY;
		requestTick();
	};

	$(window).on("scroll", onScroll);
})();