SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Dec 2008
    Location
    Brussels
    Posts
    377
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Jquery loop through items separately

    Hi,

    I want to travel through a for loop of list elements with JQuery, to separately let every list element float downwards.


    If I do this without a for loop, it works perfectly:
    Code JavaScript:
    <script type="text/javascript">
    $(document).ready(function() {
     
     $('#menu li').animate({ "opacity": "0", "marginTop": "-300px" },0);
     
     $('#thebutton').click(function(){ 
      var items = $('#menu > li');
         $(items[0]).delay().animate({ "opacity": "1", "marginTop": "2px" }, 400);
         $(items[1]).delay(200).animate({ "opacity": "1", "marginTop": "2px" }, 400);
         $(items[2]).delay(400).animate({ "opacity": "1", "marginTop": "2px" }, 400);
         $(items[3]).delay(600).animate({ "opacity": "1", "marginTop": "2px" }, 400);
         $(items[4]).delay(800).animate({ "opacity": "1", "marginTop": "2px" }, 400);
         $(items[5]).delay(1000).animate({ "opacity": "1", "marginTop": "2px" }, 400);
     $('#thebutton').animate({ "opacity": "0", "marginTop": "-300px" }, 500);
     });
     
    $('#backbutton').click(function(){
         $('#menu > li').animate({ "opacity": "0", "marginTop": "-300px" }, 700);
         $('#thebutton').animate({ "opacity": "1", "marginTop": "5px" }, 500);
    });
     
    });
    </script>

    But if I do this with a for loop, all li elements go downwards directly... :s
    Someone has experience with this? I guess there's a better solution to reduce code with a for loop.
    Code JavaScript:
    <script type="text/javascript">
    $(document).ready(function() {
     
     $('#menu li').animate({ "opacity": "0", "marginTop": "-300px" },0);
     
     $('#thebutton').click(function(){ 
      var items = $('#menu > li');
      for (i=0;i<=items.length;i++){
         $(items[i]).delay(300).animate({ "opacity": "1", "marginTop": "2px" }, 1700);
      }
     $('#thebutton').animate({ "opacity": "0", "marginTop": "-300px" }, 500);
     });
     
    $('#backbutton').click(function(){
         $('#menu > li').animate({ "opacity": "0", "marginTop": "-300px" }, 700);
         $('#thebutton').animate({ "opacity": "1", "marginTop": "5px" }, 500);
    });
     
     
    });
    </script>


    Here's the html where the javascript needs to be added:
    Code HTML4Strict:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
    <head>
    <meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1" />
     
    	<title>De Wondere Wereld van JQuery</title>
     
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/jquery-ui.min.js"></script> 
    <style>
     
    li, ul, body, p, html { margin: 0px; padding: 0px; }
     
    body { font-family: Verdana, Arial; font-size: 3mm; }
     
    ul { width: 100px; margin: 50px; background-color: 999999; }
     
    li { display: inline; float: left;
    width: 50px; margin: 1px; padding: 2px;
    border: solid 1px #333333; background-color: #666666; text-align: center; 
    }
     
    #thebutton { width: 50px; margin: 5px; border: solid 1px #333333; padding: 2px; background-color: #666666; text-align: center; }
    #menu { position: absolute; top: 0px; left: 0px; }
     
    .breek { display: block; clear: left; }
     
    </style>
     
    </head>
    <body>
     
    <div id="thebutton">open</div>
     
    <ul id="menu">
    <li>1 </li><li>2 </li>
    <li>3 </li>
    <li>4 </li>
    <li>5 </li>
    <li id="backbutton">close</li>
    </ul>
     
    </body>
    </html>

  2. #2
    SitePoint Addict
    Join Date
    Dec 2008
    Location
    Brussels
    Posts
    377
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Found it, this was the solution:
    Code JavaScript:
      for (var i=0;i<items.length;i++) {
      $(items[i]).delay(i*200).animate({ "opacity": "1", "marginTop": "2px" }, 400);

  3. #3
    SitePoint Addict
    Join Date
    Dec 2008
    Location
    Brussels
    Posts
    377
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can make nice stuff with that. I made a demo page with downloadable code for a liquid menu in JQuery. http://bulevardi.be/?content=javascript&example=exjq1


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •