My problem was that if I clicked on the destination link from another page, it would go to the destination but not scroll down to the correct portion of the page. After a few hours here was my fix:
note: I'm using ember-cli
, and emblem
and ember 1.8
link in my navigation bar
(in application.emblem
)
li
a#concept-link click=jumpToConcept
| Le Concept
then if I'm already on page I just scroll up. If I'm not already on page then I go to that page with query params concept=true
action
in application controller
scrollToConcept: function() {
Ember.$(document).ready(
Ember.$('html, body').animate({
scrollTop: Ember.$("#concept").offset().top
}, 750)
)
},
//allow clicking of link with specific id to go to that part of page
jumpToConcept : function(){
if (this.get('currentPath') === 'index') {
this.send('scrollToConcept');
} else {
this.transitionToRoute('index', { queryParams: {concept: true}});
}
}
in index controller
I then add the concept query params
export default Ember.Controller.extend(
queryParams: ['concept'],
concept: null
}
I then add a scrollToConcept
event in index view
(the page I just went to) that listens for page loading and checks the concept queryParam
. If concept=true
I then scroll to the concept part of the page.
index view
scrollToConcept: function() {
if (this.controller.get('concept') === 'true') {
Ember.$('html, body').animate({
scrollTop: Ember.$("#concept").offset().top
}, 750);
}
}.on('didInsertElement')
then for normal index links
, I add an action that sets concept=null
so that the concept query param
doesn't show up in the url.
link in nav bar
a click=goToIndex
h3 id={logoClass} Happy Dining
then in application controller
I go the the index route
, set the concept query param
to null (so it doesn't show up in the url) and scroll to the top (just incase it was lower on the page)
actions: {
goToIndex: function() {
this.transitionToRoute('index', { queryParams: {concept: null}});
Ember.$(window).scrollTop(0);
}
}
Hope that helps people in the future!!!