Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a one page menu in Ember?

I'm wondering how can I make a menu like in this website with Ember ?

The page is split in different sections and we can scroll to go to each section, a click on the menu make the page scroll to the wanted section.

I'm not sure if I should have different routes in the router for this behavior, I would guess it's not the case as when we change route the view is remove from the DOM.
Then, how should I build the anchor link to each section ?

The best solution will automatically updates route when we scroll the page but any solution to handle the link and URL recognition will be fine.

like image 278
Adrien Coquio Avatar asked Nov 12 '22 08:11

Adrien Coquio


1 Answers

Some may argue otherwise, but Ember may be a little overkill for a website landing page like you've shown. Ember is meant more for robust web apps that have multiple views and data they need to be connected with.

First off, if you look at their script, they're using jQuery to animate the body's scrollTop position to the respective div and setting window.location.hash to the hash of the menu element's href which also happens to be the ID of the <section/> the body scrolls to:

$(document).on('click', '#nav a, .clients-capabilities a', function(){
    var target = $(this);
    var hash = this.hash;
    var destination = $(hash).offset().top;

    stopAnimatedScroll();

    $('#nav li').removeClass('on');
    target.parent().addClass('on');

     $('html, body').stop().animate({ 
        scrollTop: destination
    }, 400, function() { window.location.hash = hash; });
    return false;
});

Secondly, they are not doing anything special to load to a specific position on page load. If you load any page on the web with a hash, the browser will look for an element with that ID and load at that position. For example, http://emberjs.com/#download.

Even if you still want to use Ember for this, you'd probably end up doing something similar with a single view loaded from the / route so I wouldn't even worry about Ember until your site becomes a full fledged web app.

like image 127
Corey Gwin Avatar answered Dec 24 '22 20:12

Corey Gwin