Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In your <code>.ready()</code> handler, create a list of items number:</p> <pre><code>var len = posts.length var $ul = $('&lt;ul id="navItems"&gt;'); for (var i=0; i&lt;len; i++) { $('&lt;li data-item="' + i+ '"&gt;' + (i+1) + '&lt;/li&gt;') .click(function(e) { // go to this item when you click // get the item num using $(this).data('item'); }) .appendTo($ul); } $ul.insertAfter($('#prev')); </code></pre> <p>You can style the list with css to make it horizontal:</p> <pre><code>#navItems { list-style: none; display: inline-block; } #navItems li { display: inline-block; width: 15px; height: 15px; } #navItems li { zoom: 1; } // for old IE </code></pre> <p>Here's a <a href="http://jsfiddle.net/didierg/8xnEu/" rel="nofollow">fiddle</a> for you to play with.</p> <hr> <p><strong>Implementing the click</strong></p> <p>Here is a piece of code to implement the scroll when clicking on an "item".</p> <p>It should be a bit easier to save the ID of the corresponding <code>.item</code> in the attribute <code>data-itemid</code>, afterall we do what we want :-)</p> <pre><code>for (var i=0; i&lt;len; i++) { // save the item id and not just the index $('&lt;li data-itemid="' + posts[i].id + '"&gt;' + (i+1) + '&lt;/li&gt;') .click(function(e) { // get the data from data-itemid attibute var itemid = $(this).data('itemid'); // use your scrollToPosition method // with our 'itemid' we can get the element using the ID selector scrollToPosition($('#' + itemid)); }) .appendTo($ul); } } </code></pre> <p>I have updated the fiddle as well.</p> <hr> <p><strong>A few note about your code</strong></p> <ul> <li><p>You have two <code>ready</code> handlers actually in your code. One explicit at the top <code>$(document).ready()</code> and <code>$(function() {...})</code>. They actually both work the <code>ready</code> event so you should only keep one. It works with two but it makes your code less manageable.</p></li> <li><p>Always do this: declare first your variables with <code>var</code>, then your local functions and then do what you have to do. Respecting as much as possible this canvas will help you read your code because you expect things to be there or there.</p></li> <li><p>Prefix your jquery variable with <code>$</code>: <code>var $posts = $('.item');</code>. When you then see a variable, you know what it contains (or supposed to) by its name.</p></li> <li><p>jQuery is about chaining.<br/></p></li> </ul> <p>Instead of doing this:</p> <pre><code>var next = $('#next'); next.click(function(e) {...}); </code></pre> <p>Do this:</p> <pre><code>var $next = $('#next').click(...).addClass(...).show(); var $prev = $('#prev').click(...).toggle((position &gt; 0)); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload