Skip to content

Instantly share code, notes, and snippets.

@pearofducks
Forked from benjamincharity/zepto.smoothScroll.js
Last active September 11, 2017 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pearofducks/257832974ea0ed0e63abcfd9172b74ed to your computer and use it in GitHub Desktop.
Save pearofducks/257832974ea0ed0e63abcfd9172b74ed to your computer and use it in GitHub Desktop.
(Actual) smooth scrolling with Zepto.js - with easing
function easeInOutQuart( t ) {
const t1 = t - 1
return t < 0.5 ? 8 * t * t * t * t : 1 - 8 * t1 * t1 * t1 * t1
}
function smoothScroll(el, to, duration) {
var initial = $(window).scrollTop()
var dest = to - initial
var start = null
function step (timestamp) {
if (!start) start = timestamp
var progress = timestamp - start
var percentage = progress / duration
var tick = (easeInOutQuart(percentage) * dest) + initial
window.scrollTo(0, tick)
if (progress < duration) { window.requestAnimationFrame(step) }
else { return }
}
window.requestAnimationFrame(step)
}
$('.scrollTo').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