Allan
Allan

Reputation: 11

vertical scrolling menu in JavaScript

Is there any way to create a vertical scrolling menu that when you click on a link the whole menu will shift up or down? It's really hard to explain. The best example I could find of what I'm trying to do is the old xbox nxe dashboard.

http://www.youtube.com/watch?v=Q2PyNpbteuU

Where if your links are (HOME - ABOUT - CONTACT) and you click contact; that link will now be centered in the list and about will be on top and home underneath.

Home

About

Contact (than you click on contact)

-

About

Contact

Home (And now it would look like this)

-

Is this possible? Using HTML5? CSS? Javascript?

Upvotes: 1

Views: 2135

Answers (1)

rafaelcastrocouto
rafaelcastrocouto

Reputation: 12161

Here you will find a horizontal and a vertical implementation I did with jquery:

http://codepen.io/rafaelcastrocouto/pen/kuAzl

The HTML code of the vertical menu:

<nav id="vmenu">
  <section>
    <div class="active"><a>First</a></div>
    <div><a>Second</a></div>
    <div><a>One more</a></div>
    <div><a>XBox</a></div>
    <div><a>Menu</a></div>
    <div><a>Last</a></div>
  </section>
</nav>

The CSS:

#vmenu {
  border: 1px solid green;
  overflow: hidden;
  position: relative;
  padding: 5%;
}
#vmenu section {
  position: relative;
  margin-top: 10%;
  transition: top 0.5s;
}
#vmenu section div {
  float: left;
  display: inline-block;
  padding: 0; margin: 0;
  transform: scale(1);
  transition: transform 0.5s;
}
#vmenu section div.active { 
  transform: scale(1.2);
}
#vmenu section div a {
  text-align: center;
  background: #ddd;
  box-shadow: 0 0 1em #444;
  border-radius: 1em;
  display: block;
  width: 60%; height: 60%;
  padding: 10%;
  margin: 10%;
}
#vmenu section div.active a { 
  background: #ccc;
  box-shadow: 0 0 1em;  
}

And the JS:

var size = 200;
  var count = $('#vmenu section').get(0).children.length;
  $('#vmenu').height(2 * size).width(size);  
  $('#vmenu section').height(size * count);
  $('#vmenu section div').height(size).width(size).on('click', function(){
    $('#vmenu section div').removeClass('active')
    $(this).addClass('active');
    var c = this.parentNode.children;
    var i = Array.prototype.indexOf.call(c, this);
    $('#vmenu section').css('top', i * size * -1);
  });

Upvotes: 1

Related Questions