Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question script for randomizing images

    hi all. i was wondering if there was a way to code in javascript an image to be randomly alternated between other images. for instance, http://www.lscubed.com is my personal site. I would like the "intro" page's image to be alternated between three other images (same design, different color) everytime a visitor goes to that page. kinda like having an image randomly chosen everytime a visitor views the page. any suggestions would be great!!

    MB

  • #2
    New Coder TripperTreats's Avatar
    Join Date
    Oct 2006
    Posts
    92
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    var imageArray = new Array();
    imageArray[0] = path of image 1;
    imageArray[1] = path of image 2;
    imageArray[2] = path of image 3;
    
    var randomIndex = Math.floor(Math.random()*3);
    document.body.style.background = 'url(imageArray[randomIndex])';
    Psychedelic digital art at www.trippertreats.com.

    "And in the end, the love you take
    is equal to the love you make
    ."

  • #3
    New to the CF scene
    Join Date
    Jan 2007
    Location
    Islamabad
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    suppose u have 3 images with names image_01.jpg, image_02.jpg and image_02.jpg
    and the ID of image control in which u want to display images randomly is imgRandom now write javascript on ur page as:

    <script language="javascript" type="text/javascript">
    var img = '1';

    function change_image()
    {
    var img = document.getElementById('imgRandom');
    if (img == '1')
    {
    img = '2';
    img.src = 'image_01.jpg';
    }
    else if if (img == '1')
    {
    img = '3';
    img.src = 'image_02.jpg';
    }
    else if (img == '3')
    {
    img = '1';
    img.src = 'image_03.jpg';
    }

    setTimeout("change_image()", 10000);

    }

    change_image();

    </script>

    Hope this wud solve ur problem

  • #4
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    rock on!!! thanx bro....i'm gonna try this out tonight.

    MB


  •  

    Posting Permissions

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