Last active
October 22, 2018 12:26
-
-
Save olecksamdr/b7ffe8c06cf7edcef1aa8079591d8ac1 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
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; |
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
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