Results 1 to 3 of 3

Thread: randome background generator

  1. #1
    Join Date
    Jul 2005
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default randome background generator

    hi! i've been trying to look around, but I can't find any way to use CSS so that random background pictures appear every time the user enters my website

    i guess one could also use javascript, but since i dont have much experience with that, i dont know how to get the background position always in the "top center" etc and other features of CSS

    but, if some can help me out, give me a link to learn or some code in either javascript or CSS, i'd really appreciate it ...

    thanks a lot!
    laksh

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    This script will work for 9 images named photo1.jpg through photo9.jpg, located in the same directory as the page this script is on.
    Code:
    <script type="text/javascript">
    function random(n) {
      return Math.floor((Math.random() % 1) * n);
    }
    document.documentElement.style.background="url('photo"+[random(9)+1]+".jpg') pink center no-repeat"
    </script>
    As you can see by the part after the image declaration, it will accept conventional css values for background in the usual manner. Be aware, random means random so, the same image may be used twice or more times in a row.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jul 2005
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    cool cool

    works perfectly

    thanks a lot!!!!

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
  •