Last active
July 27, 2017 14:29
-
-
Save mgussekloo/088288ebf796fa2a3cd83cb7ed77145c to your computer and use it in GitHub Desktop.
Vue directive, for scrolling to the bottom of an element after updating. Using TweenJS/Tween.js
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 Vue from 'vue'; | |
| import TWEEN from 'tween.js'; | |
| var scrollDownAnimation = false; | |
| var scrollDownFunction = function(el) { | |
| var scrolledToBottom = el.scrollHeight - el.scrollTop === el.clientHeight; | |
| if (scrolledToBottom) return; | |
| if (scrollDownAnimation) { | |
| scrollDownAnimation.stop(); | |
| } | |
| scrollDownAnimation = new TWEEN.Tween({ tweeningNumber: el.scrollTop }) | |
| .easing(TWEEN.Easing.Exponential.Out) | |
| .to({ tweeningNumber: el.scrollHeight - el.clientHeight }, 1000) | |
| .onUpdate(function () { | |
| el.scrollTop = this.tweeningNumber.toFixed(0) | |
| }) | |
| .onComplete(function() { | |
| scrollDownAnimation = false; | |
| }) | |
| .start(); | |
| function animate () { | |
| if (TWEEN.update()) { | |
| requestAnimationFrame(animate); | |
| } | |
| } | |
| animate() | |
| } | |
| Vue.directive('scroll-down', function(el, binding) { | |
| window.setTimeout(function() { | |
| scrollDownFunction(el); | |
| }, 100); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment