From 99e393fb1ebb5bdbfe8f83890888fbef151dfa4b Mon Sep 17 00:00:00 2001 From: jeroensak Date: Thu, 19 Jul 2018 16:44:37 +0200 Subject: [PATCH] add requestAnimationFrame add requestAnimationFrame to optimize smooth animations. Also fallback for browsers without support. --- src/scroll-animation.js | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/scroll-animation.js b/src/scroll-animation.js index 262b322..65735d8 100644 --- a/src/scroll-animation.js +++ b/src/scroll-animation.js @@ -122,12 +122,23 @@ export default class ScrollAnimation extends Component { animate(animation, callback) { this.delayedAnimationTimeout = setTimeout(() => { this.animating = true; - this.setState({ - classes: `animated ${animation}`, - style: { - animationDuration: `${this.props.duration}s` - } - }); + if (window.requestAnimationFrame) { + window.requestAnimationFrame(() => { + this.setState({ + classes: `animated ${animation}`, + style: { + animationDuration: `${this.props.duration}s` + } + }); + }); + } else { + this.setState({ + classes: `animated ${animation}`, + style: { + animationDuration: `${this.props.duration}s` + } + }); + } this.callbackTimeout = setTimeout(callback, this.props.duration * 1000); }, this.props.delay); }