From 685bff9b8ab72011f4b465b5706e308f5ea3658c Mon Sep 17 00:00:00 2001 From: Jake LeBoeuf Date: Tue, 1 May 2018 10:56:15 -0400 Subject: [PATCH] fixes distance from scroll --- addon/services/scroller.js | 37 +++++++++++++------------------------ 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/addon/services/scroller.js b/addon/services/scroller.js index 7e715a8..78687d4 100644 --- a/addon/services/scroller.js +++ b/addon/services/scroller.js @@ -1,27 +1,24 @@ import Em from 'ember'; const DURATION = 750; -const EASING = 'swing'; -const OFFSET = 0; +const EASING = 'swing'; +const OFFSET = 0; const { RSVP } = Em; export default Em.Service.extend({ - // ----- Static properties ----- duration: DURATION, - easing: EASING, - offset: OFFSET, - + easing: EASING, + offset: OFFSET, // ----- Computed properties ----- scrollable: Em.computed(function() { return Em.$('html, body'); }), - // ----- Methods ----- - getJQueryElement (target) { + getJQueryElement(target) { const jQueryElement = Em.$(target); if (!jQueryElement) { @@ -32,29 +29,21 @@ export default Em.Service.extend({ return jQueryElement; }, - getScrollableTop () { - // because the target elements top is calculated relative to the document, - // and if the scrollable container is not the document, - // we need to normalize the target elements top based on the top and current scrolled position of the scrollable - if (this.get('scrollable').offset().top) { - return this.get('scrollable').scrollTop() - this.get('scrollable').offset().top; - } else { - return 0; - } + getVerticalCoord(target, offset = 0) { + const jQueryElement = this.getJQueryElement(target); + return jQueryElement.offset().top + offset; }, - getVerticalCoord (target, offset = 0) { - const jQueryElement = this.getJQueryElement(target); - return this.getScrollableTop() + jQueryElement.offset().top + offset; - }, + scrollVertical(target, opts = {}) { + var top = + -this.get('scrollable').offset().top + + this.getVerticalCoord(target, opts.offset); - scrollVertical (target, opts = {}) { return new RSVP.Promise((resolve, reject) => { - this.get('scrollable') .animate( { - scrollTop: this.getVerticalCoord(target, opts.offset) + scrollTop: top }, opts.duration || this.get('duration'), opts.easing || this.get('easing'),