Skip to content

Instantly share code, notes, and snippets.

@Yunkou
Forked from benjamincharity/zepto.smoothScroll.js
Created September 14, 2016 10:16
Show Gist options
  • Save Yunkou/9c797bdb6ebca15ada760d37df433cea to your computer and use it in GitHub Desktop.
Save Yunkou/9c797bdb6ebca15ada760d37df433cea to your computer and use it in GitHub Desktop.
Smooth scrolling with Zepto.js
function smoothScroll(el, to, duration) {
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);
}
$('.scrollTo').on('click', function(e) {
e.preventDefault();
smoothScroll($(window), $($(e.currentTarget).attr('href')).offset().top, 200);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment