Skip to content

Instantly share code, notes, and snippets.

@olecksamdr
Last active October 22, 2018 12:26
Show Gist options
  • Save olecksamdr/b7ffe8c06cf7edcef1aa8079591d8ac1 to your computer and use it in GitHub Desktop.
Save olecksamdr/b7ffe8c06cf7edcef1aa8079591d8ac1 to your computer and use it in GitHub Desktop.
const quad = n => n * n;
const animate = ({
timing = quad,
duration,
draw,
}) => {
const start = performance.now();
const anim = (time) => {
// timeFraction [0, 1]
let timeFraction = (time - start) / duration;
if (timeFraction > 1) timeFraction = 1;
const progress = timing(timeFraction);
draw(progress);
if (timeFraction < 1) {
requestAnimationFrame(anim);
}
};
requestAnimationFrame(anim);
};
export default animate;
import { string } from 'prop-types';
import { compose, setPropTypes, withHandlers } from 'recompose';
import animate from 'utils/animate';
import { ScrollDownButton } from './style';
const enhancer = compose(
setPropTypes({
elementId: string.isRequired,
}),
withHandlers({
onClick: ({ elementId }) => (event) => {
event.preventDefault();
const element = document.getElementById(elementId);
const top = element.getBoundingClientRect().top;
const startY = window.pageYOffset;
animate({
duration: 800,
draw: (progress) => window.scrollTo(0, startY + top * progress),
});
}
})
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment