Log in

View Full Version : BG Changer?



Recursion
15-04-2007, 06:13 PM
Hello,

How do I make a background changer?

Where a user can select a background and it changes without refreshing.

I want them to click say a 50px*50px image and the background change to that image (the image will be tiled).

Thanks
Tom

timROGERS
15-04-2007, 06:36 PM
Here you go :) This is the script that I wrote for my site. Put this in the head:



<script type="text/javascript">
function setBackgroundImage(url) {
document.body.style.backgroundImage = 'url(' + url + ')';
}
</script>

Then you can call it pretty easily. For example, if you want to change the background colour when you click a specific image:



<img src="whatever.jpg" alt="This will change the bg to bg.gif" onClick="setBackgroundImage('bg.gif')" />


I have different version on my site that changes the background, and then loads a PHP page which saves your choice into a cookie.

Recursion
15-04-2007, 06:58 PM
i dont understand how to use it lol I know whwere to put it all etc... But:

1. Will it let me set the Background IMAGE to the Image thats clicked.
2. How do I add images
3. what do I change where
4. I am blonde so thats why i am dumb :)

timROGERS
15-04-2007, 07:02 PM
Just put the Javascript bit into the head of your page. Then, for example, if the image was called "animage.gif" and you wanted to change the background to itself when you clicked it, you would use this code for the image:



<img src="animage.gif" alt="An image" onclick="setBackgroundImage('animage.gif')" />

Ryan.
15-04-2007, 07:04 PM
:o. I wanted this a while ago. Thanks tim +rep :)

edit: need to spread :(

Recursion
15-04-2007, 08:09 PM
Thanks alot you two! It worked perfectly :) +REP if i can...

Want to hide these adverts? Register an account for free!