Skip to content

Instantly share code, notes, and snippets.

@bezenson
Last active September 15, 2017 14:07
Show Gist options
  • Save bezenson/c12ea210b56901f26961ad6bb6d29b28 to your computer and use it in GitHub Desktop.
Save bezenson/c12ea210b56901f26961ad6bb6d29b28 to your computer and use it in GitHub Desktop.
/* eslint-disable no-param-reassign */
import raf from 'rafl';
import { Tween, Easing } from '@tweenjs/tween.js';
function noop() {}
export default function scrollTo(element, options) {
let { duration, onComplete } = options;
const { left, top } = options;
onComplete = onComplete || noop;
duration = duration || 500;
if (!left && !top) {
return;
}
const to = {};
const coords = {};
if (left) {
to.left = left;
coords.left = element.scrollLeft;
}
if (top) {
to.top = top;
coords.top = element.scrollTop;
}
const tween = new Tween(coords);
let animate = (time) => {
raf(animate);
tween.update(time);
};
tween
.to(to, duration)
.easing(Easing.Quadratic.Out)
.onUpdate(() => {
element.scrollTop = coords.top;
})
.onComplete(() => {
console.log('complete');
animate = () => {};
onComplete();
})
.start();
raf(animate);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment