1

At the moment I have a website with 5 pages and the main navigation is being displayed using an unordered list. What I'm trying to achieve is for the "active" page to be displayed first in the list.

For example, if I have a these pages: "Home | About | Contact | Blog". When you're on the contact page, this should move to the top of the list, so the menu would than look like this: "Contact | Home | About | Blog".

What's causing me problems is that the menu is being included through php, so there is just one menu for all the pages and through PHP I have made it so that it adds an "active" class to the current page menu item.

I am not really sure what the best solution for this would and would really appreciate any help.

Edit:

This is the navigation page that's being included into each page:

            <nav class="large-nav">
              <ul>
                <li <?php echo ($activePage == 'ourStory') ? "class='active'" : ""; ?> ><a href="our_story.php" title="">OUR STORY</a></li>
                <li <?php echo ($activePage == 'services') ? "class='active'" : ""; ?> ><a href="services.php" title="">SERVICES</a></li>
                <li <?php echo ($activePage == 'projects') ? "class='active'" : ""; ?> ><a href="projects.php" title="">PROJECTS</a></li>
                <li <?php echo ($activePage == 'contact') ? "class='active'" : ""; ?> ><a href="contact.php" title="">CONTACT</a></li>
                <li <?php echo ($activePage == 'upload') ? "class='active'" : ""; ?> ><a style="margin-right: 0;" href="upload.php#top" title="">UPLOAD</a></li>
              </ul>
            </nav>
Bonzai
  • 159
  • 1
  • 10
  • are these attributes stored in unordered list ? cant you place your active page one right on top of unordered list , I mean the top most index ? – Surya Prakash Patel Apr 24 '16 at 17:22
  • Why don't you post the code for adding the "active" class? It will be easier to modify in order to answer your question. – BeS Apr 24 '16 at 17:25
  • Sorry, I added the navigation page that's being included. – Bonzai Apr 24 '16 at 17:37

3 Answers3

4

Here is a pure CSS way to do it using Flexbox ordering.

enter image description here

ul {
  …
  display: flex;
  flex-direction: column;
}

li[class=active] {
  order: -1;
  …
}

Demo: http://codepen.io/antibland/pen/ZWjdqL

Support: IE10+, Firefox, Chrome

Andy Hoffman
  • 18,436
  • 4
  • 42
  • 61
0

Jsfiddle link

jQuery:

$(function(){
    $("li").on("click", function(){
        $('ul').prepend($(this))
    }) 
})

HTML

<ul>
    <li>Home</li>
    <li>Contact</li>
    <li>About</li>
    <li>Blog</li>
</ul>
Murad Hasan
  • 9,565
  • 2
  • 21
  • 42
  • What do you mean I need that one ? Yeah its simpler; I remember being astonish when I first understood the true functioning of `append()/prepend()` – Baldráni Apr 24 '16 at 17:56
  • So you can do that one without any help. what you really want?? If you really mean that the included PHP file can't change from jQuery then its not good thinking. The first time PHP include the page and the active class, now you have to control the functionalities through jQuery like sorting done. – Murad Hasan Apr 24 '16 at 18:00
  • Im not the op dude. By the way your answer can't help him since the page is reloaded (I guess there is no Ajax call in it). – Baldráni Apr 24 '16 at 18:02
  • oops... Sorry. I misunderstood. – Murad Hasan Apr 24 '16 at 18:05
0

You could do something like this :

$('.large-nav li').each(function() { 
    if($(this).find('a').attr('href') == location)
    {
        $('.large-nav ul').prepend($(this))
    }
})
Baldráni
  • 5,332
  • 7
  • 51
  • 79