Not signed in (Sign In)

SkillShare - A place to discuss Web Standards and Web Design topics

Categories

Vanilla 1.1.9 is a product of Lussumo. More Information: Documentation, Community Support.

  1.  permalink
    Wondering if anyone can assist me. I need to display a pair of images that would be selected randomly when one views a page. That is, two images must always go together, but the pair that is picked is random. I am thinking the choice of the second one would be linked to the random selection of the first one but I don't know enough Javascript to write this properly.

    Any ideas?
    •  
      CommentAuthornifkin
    • CommentTimeMar 2nd 2006
     permalink
    Make two arrays, the first set would be your "image one" set, the second would be your "image two" set. Make sure the pairs sync up across the two sets (the first in one matches the first in the second and so on). Then all you should need to do is pick one random number and use it to select from both arrays and you should be all set.

    Some JS towards that end:

    // make two arrays
    var img_one = new Array();
    var img_two = new Array();

    // populate the two arrays so that the contents sync up.
    img_one[0] = 'leftone.jpg';
    img_two[0] = 'rightone.jpg';
    img_one[1] = 'lefttwo.jpg';
    img_two[1] = 'righttwo.jpg';

    // generate a random number limited by the number of options available.
    var n=Math.floor(Math.random() * img_one.length);

    // write out the left image.
    document.write('<img src="'+img_one[n]+'" alt="left image" />');

    // write out the right image.
    document.write('<img src="'+img_two[n]+'" alt="right image" />');


    But the same basic idea applies to however you decide to do this, PHP, JS, whatever.
    • CommentAuthorspyyddir
    • CommentTimeMar 2nd 2006
     permalink
    Seems like something more appropraite for php.
    •  
      CommentAuthornifkin
    • CommentTimeMar 2nd 2006
     permalink
    PHP would be able to automate the process by reading the contents of a directory or something. But in the end it sorta breaks down to that "ask 6 people get 6 different answers" kind of situation.

    Plus, the thread was in the JS category. ;)
  2.  permalink
    Using PHP is not an option here. Anyway, thanks for the help!
Add your comments
    Username Password
  • Format comments as (Help)