Skip to content

Instantly share code, notes, and snippets.

@felipefialho
Forked from austinpray/zepto.smoothScroll.js
Last active June 22, 2016 00:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save felipefialho/94adb4f49085fe73bfb8 to your computer and use it in GitHub Desktop.
Save felipefialho/94adb4f49085fe73bfb8 to your computer and use it in GitHub Desktop.
Zepto.js smooth vertical scrolling method. Implementing this code turns all anchor links with the class "scrollTo" into smooth scrolling anchor links. Rework of the foundation.js library method.
// ==================================================
// Smooth Scroll
// ==================================================
function smoothScroll(el, to, duration) {
'use strict';
if (duration < 0) {
return;
}
var difference = to - $(window).scrollTop();
var perTick = difference / duration * 10;
$(this).scrollToTimerCache = setTimeout(function() {
if (!isNaN(parseInt(perTick, 10))) {
window.scrollTo(0, $(window).scrollTop() + perTick);
smoothScroll(el, to, duration - 10);
}
}.bind(this), 10);
}
$('[data-scroll]').on('click', function(e) {
e.preventDefault();
smoothScroll($(window), $($(e.currentTarget).attr('href')).offset().top, 600);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment