Last active
June 13, 2018 18:52
-
-
Save ronco/965f7154b7ef787ef9f2ce6e9d4983cd to your computer and use it in GitHub Desktop.
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 Ember from 'ember'; | |
export default Ember.Route.extend({ | |
goToElement(selector, offset, target) { | |
if (selector == null) { | |
return; | |
} | |
offset = offset != null ? offset : staticConstants.headerHeight; | |
let $elem; | |
try { | |
$elem = Ember.$(`body ${selector}`); | |
} catch(err) { /* eat error */ } | |
if ($elem && $elem.offset && ($elem.offset() != null)) { | |
let duration; | |
if (ENV.environment === 'test') { | |
duration = 0; | |
} else { | |
duration = 500; // ms | |
} | |
const easing = 'swing'; | |
const scrollTop = $elem.offset().top - offset; | |
Ember.$('html, body').animate({ scrollTop }, duration, easing); | |
} else { | |
if (target != null) { | |
Ember.set(target, 'scrollTo', null); | |
} | |
} | |
} | |
}); |
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 Ember from 'ember'; | |
export default Ember.Mixin.create({ | |
scrollOffset: 0, | |
queryParams: [ | |
{ | |
scrollTo: 'st' | |
} | |
], | |
scrollTo: null, | |
_scheduleScrollTo() { | |
const selector = this.get('scrollTo'); | |
if (selector == null) { | |
return; | |
} | |
Ember.run.schedule('afterRender', () => { | |
this.send('goToElement', selector, this.get('scrollOffset'), this); | |
}); | |
}, | |
_goToScrollTo: Ember.observer('scrollTo', function() { | |
Ember.run.once(this, '_scheduleScrollTo'); | |
}), | |
reset() { | |
this._super(...arguments); | |
if (this.get('scrollTo') != null) { | |
this._goToScrollTo(); | |
} else { | |
window.scrollTo(0, 0); | |
} | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment