Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes distance from scroll #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 13 additions & 24 deletions addon/services/scroller.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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'),
Expand Down