-
-
Save benjamincharity/6058688 to your computer and use it in GitHub Desktop.
Smooth scrolling with Zepto.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Enhanced code
Hello guys great work for all of you .
I did some changes to the main script to make it easier and more plug & play .
Check it and tell me your opinion.
Cheers :)
(function ($) {
})(window.Zepto);
// Simple Example according to href
$('.triggerElement').scrollTo();
// Advance Example
$('.triggerElement').scrollTo({
filterAttr: 'data-link',
eventTrigger: 'mouseover',
onFinish: function (triggerElement, scrolledElement) {
alert(triggerElement.attr('class') + '-----' + scrolledElement.attr('id'));
}
});