Last active
August 29, 2015 14:09
-
-
Save prisme/0678562c4af604e0ac2d to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
* | |
* Works with elements or jquery | |
* | |
* scrollTo(element, duration, offset); | |
* | |
* Update 'scroller' variable with custom scrolling element if not document.body | |
* | |
*/ | |
define([ | |
'gsap' | |
], function() { | |
var dummy = document.createElement('div'); | |
var isWebkit = 'WebkitAppearance' in document.documentElement.style; | |
var scroller = isWebkit ? document.body : document.documentElement; | |
return function( element, duration, offset ){ | |
var minDuration = 0.2; | |
var offset = offset || 0; | |
var element = element instanceof jQuery ? element.get(0) : element; | |
var distance = Math.abs( element.offsetTop - scroller.scrollTop + offset ); | |
var duration = duration || (minDuration + distance / 3000).toFixed(2); | |
TweenLite.set(dummy, { x: scroller.scrollTop }); | |
TweenLite.to(dummy, duration, { | |
x: element.offsetTop + offset, | |
ease: Power1.easeInOut, | |
onUpdate: function() { | |
scroller.scrollTop = (dummy._gsTransform.x).toFixed(2); | |
} | |
}); | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment