Dynamic jquery using i++ image fade in

Discussion in 'jQuery' started by shortysbest, May 27, 2010.

  1. #1
    I want to get the effect of this, except for every image that loads. i use i++ with php to assin an id value of 1,2,3,4,5 etc. id="id1", id="id2", etc is what my image gets.
    $(document).ready(function(){
    setTimeout(function () {$("#id1").fadeIn("fast");}, 100);
    });

    Im not sure what the code would be, something like:
    id=0;
    $(document).ready(function(){
    id++
    setTimeout(function () {$("#id" + id).fadeIn("fast");}, 100);
    });

    i know this wouldnt be at all correct, but something along the lines of this
     
    shortysbest, May 27, 2010 IP
  2. swashata

    swashata Member

    Messages:
    86
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #2
    $(document).ready(function() {
           for(var i = 0; i<10; i++) {
                 $("#id" + id).fadeIn("fast");
           }
    });
    
    Code (markup):
    That should work! you can use php to store the maximum amount of i's. Say,
    <?php
    for($i=0;$i<$max_num;$++) {
     //Your PHP Code
    }
    ?>
    <script type="text/javascript">
    var max_i = <?php echo $i; ?>
    $(document).ready(function() {
           for(var i = 0; i<max_i; i++) {
                 $("#id" + id).fadeIn("fast");
           }
    });
    </script>
    Code (markup):
    Hope that helps! Any problem feel free to ask!
     
    swashata, May 27, 2010 IP
  3. shortysbest

    shortysbest Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3

    tried it, doesnt work.

    my php

    <?php 
    $files = glob("shows/*.*");
    $f=0;
    for ($i=1; $i<count($files); $i++)
    { 
    	$num = $files[$i];
    	echo '<a href="'.$num.'"><img id="'.$f++.'"class="photo-container" src="'.$num.'"></a>';
    }
    ?>
    PHP:
    and the script you gave me. (took id out)

    
    $(document).ready(function() {
           for(var i = 0; i<10; i++) {
                 $("#" + id).fadeIn("fast");
           }
    });
    HTML:
     
    shortysbest, May 27, 2010 IP
  4. swashata

    swashata Member

    Messages:
    86
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #4
    Sorry, this would be the code:
    
    <?php
    $files = glob("shows/*.*");
    $f=0;
    for ($i=1; $i<count($files); $i++)
    {
        $num = $files[$i];
        echo '<a href="'.$num.'"><img id="'.$f++.'"class="photo-container" src="'.$num.'"></a>';
    }
    ?>
    <script type='text/javascript'>
    var max_i = <?php echo $i; ?>;
    $(document).ready(function() {
           for(var i = 0; i<max_i; i++) {
                 $("#" + i).fadeIn("fast");
           }
    });
    </script>
    Code (markup):
     
    swashata, May 27, 2010 IP
  5. shortysbest

    shortysbest Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hey thank you very much, that works perfectly. Just one thing, do you know how to make it so for each id it fades in 100 miliseconds after the last. So the result would be like:

    setTimeout(function () {$("#1").fadeIn("fast");}, 100);
    setTimeout(function () {$("#2").fadeIn("fast");}, 200);

    etc up until the last image. Thanks in advance. ANd thanks for the help previously
     
    shortysbest, May 27, 2010 IP