views:

126

answers:

3

My javascript is not up to scratch at the moment and I am stumped with this!

I need to create an animated list with javascript like this one - http://www.fiveminuteargument.com/blog/scrolling-list.

What I want is to take a list like so

<ul>
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li> 
    <li>Item 4</li>
    <li>Item 5</li>
    <li>Item 6</li>
</ul>

And display two at once, then display them in a loop, 2 at a time.

Even pseudo code would help to get me started.

+3  A: 

With the html you included in your message, you can run the following.

$(document).ready(function(){
    //hide all the list items
    $("ul li").hide();
    //call the function initially
    show_list_item();
});

function show_list_item(){
    //fade in the first hidden item. When done, run the following function
    $("ul li:hidden").first().fadeIn("slow", function(){
       //if there are no more hidden list items (all were shown), hide them all
       if($("ul li:hidden").length == 0){
          $("ul li").hide();
       }
       //call this function again - this will run in a continuous loop
       show_list_item();
    });
}
yuval
Thanks yuval - I get $("ul li:hidden") is not a function error when this runs. I'm using 1.3.2 though, so not sure why.
Chris
did you include jQuery? That's the first thing that comes to mind when you get a not-a-function with the dollar sign. Do you have any other JS libraries included? Try testing this in a page with only jquery included, this code, and the html included. It should work without a problem with 1.3.2. Also make sure you wrap your js with `<script type="text/javascript">...</script>` or include it in a separate file. Let me know how it goes.
yuval
Hmmm, same error - I put the plain html file up here: http://jonesonter.notomato.com.au/wp-content/themes/jonesonter/test.html.
Chris
The proper selector is `$('ul li:hidden :first')` not `$('ul li:hidden').first()`
czarchaic
Now I get a warning - unknown class or pseudo class element :hidden.
Chris
`$('ul li:hidden :first')` has an extra space. It should be `$('ul li:hidden:first')`.
DaveS
Nearly there - if you look at the original link you'll see that they scroll up so only 2 items are visible at any one time. I am starting to see how it could be done though. I think I need to rotate them in a loop and then make a container that has hidden overflow to limit the view to 2 at a time.
Chris
Right! What would make more sense is to just fade in 2, hide them, fade in the next two, hide them etcetc in a loop.
Chris
A: 

Just a modification to Yuval's code, to get the 'two at a time' behaviour working:

$(document).ready(function(){
    //hide all the list items
    $("ul li").hide();
    //call the function initially
    show_list_item();
});

function show_list_item(){
    //fade in the first hidden item. When done, run the following function
    $("ul li:hidden:first").fadeIn("slow", function(){
       //if there are no more hidden list items (all were shown), hide them all
       if($("ul li:hidden").length == 0){
      $("ul li").hide();
       }
    });
    $("ul li:hidden:first").fadeIn("slow", function(){
       //if there are no more hidden list items (all were shown), hide them all
       if($("ul li:hidden").length == 0){
      $("ul li").hide();
       }
       //call this function again - this will run in a continuous loop
       show_list_item();
    });
}
DaveS
A: 

Thanks for this article.

Ali KARA