Last active
September 15, 2017 14:07
-
-
Save bezenson/c12ea210b56901f26961ad6bb6d29b28 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* 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