473,385 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Change Background Image OnClick

6
Hello , i'm trying to find a script that changes the background of the page by clicking different images... i mean, I have three different image backgrounds and i would like to have three small pictures (each one for each background) and when the user clicks on them the background changes...
hope you understand my question, my english is very bad.. :-(
thanxs !
Mar 3 '08 #1
13 14512
xbcn27
6
me again...

i've founded a script that works, when you click on one thumbnail it appears on the background.. but i would like to fix one of the images at the begining, when the page is loaded...
If i set one of the pictures on the body tag, like this:
"body {background-image: 11.jpg} "
the script doesn't works..

Here's the script, if anyone can help me...

Expand|Select|Wrap|Line Numbers
  1. <script language="JavaScript">
  2. <!--
  3.  
  4. // Copyright 2001 by www.CodeBelly.com
  5. // Please do *not* remove this notice.
  6.  
  7. var backImage = new Array(); // don't change this
  8.  
  9. // Enter the image filenames you wish to use.
  10. // Follow the pattern to use more images.  The
  11. // number in the brackets [] is the number you
  12. // will use in the function call to pick each
  13. // image.
  14.  
  15. // Note how backImage[3] = "" -- which would
  16. // set the page to *no* background image.
  17.  
  18. backImage[0] = "portfolio/11.jpg";
  19. backImage[1] = "portfolio/22.jpg";
  20. backImage[2] = "33.jpg";
  21. backImage[3] = "";
  22.  
  23. // Do not edit below this line.
  24. //-----------------------------
  25.  
  26. function changeBGImage(whichImage){
  27. if (document.body){
  28. document.body.background = backImage[whichImage];
  29. }
  30. }
  31.  
  32. //-->
  33. </script>
  34.  
Mar 3 '08 #2
acoder
16,027 Expert Mod 8TB
Set the style.backgroundImage property instead. See how.
Mar 3 '08 #3
xbcn27
6
Set the style.backgroundImage property instead. See how.

thank you ! but i still have the problem...if i set the style.backgroundImage property the script doesn't works...

i've got three images that i want to be showed in the background by clicking on different thumbnails but whith the script i've posted when the page is loaded no pictures are showed until you click on one thumbnail...

and if i set backgroundimage on load the script does not changes the images...

:-) thank you...
Mar 3 '08 #4
acoder
16,027 Expert Mod 8TB
If i set one of the pictures on the body tag, like this:
"body {background-image: 11.jpg} "
the script doesn't works..
That's CSS. Put it within style tags on the page, e.g. [HTML]<style type="text/css">
body {background-image: url(11.jpg);}
</script>[/HTML]
Mar 3 '08 #5
xbcn27
6
That's CSS. Put it within style tags on the page, e.g. [HTML]<style type="text/css">
body {background-image: url(11.jpg);}
</script>[/HTML]
no... first thing i tried, and it doesn't works with the script i posted because the background does not changes, only see the one i load with the css sentence...
Mar 3 '08 #6
acoder
16,027 Expert Mod 8TB
Post your whole page code.
Mar 4 '08 #7
hsriat
1,654 Expert 1GB
me again...

i've founded a script that works, when you click on one thumbnail it appears on the background.. but i would like to fix one of the images at the begining, when the page is loaded...
If i set one of the pictures on the body tag, like this:
"body {background-image: 11.jpg} "
the script doesn't works..

Here's the script, if anyone can help me...

Expand|Select|Wrap|Line Numbers
  1. <script language="JavaScript">
  2. <!--
  3.  
  4. // Copyright 2001 by www.CodeBelly.com
  5. // Please do *not* remove this notice.
  6.  
  7. var backImage = new Array(); // don't change this
  8.  
  9. // Enter the image filenames you wish to use.
  10. // Follow the pattern to use more images.  The
  11. // number in the brackets [] is the number you
  12. // will use in the function call to pick each
  13. // image.
  14.  
  15. // Note how backImage[3] = "" -- which would
  16. // set the page to *no* background image.
  17.  
  18. backImage[0] = "portfolio/11.jpg";
  19. backImage[1] = "portfolio/22.jpg";
  20. backImage[2] = "33.jpg";
  21. backImage[3] = "";
  22.  
  23. // Do not edit below this line.
  24. //-----------------------------
  25.  
  26. function changeBGImage(whichImage){
  27. if (document.body){
  28. document.body.background = backImage[whichImage];
  29. }
  30. }
  31.  
  32. //-->
  33. </script>
  34.  
In all the three thumbnails, add changeBGImage(0), changeBGImage(1), changeBGImage(2) as the value of onclick attribute, respectively.
If that doesn't work, post your code here.

PS: Your background images may take time to change when you click on the corresponding thumbnail. That can be solved by caching those images but keeping them hidden by using display:none.
Mar 4 '08 #8
xbcn27
6
Thank you very much.. :-) here's the code..


[HTML]<html>
<head>
<title>POPLUV.COM</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="css/estilo.css" rel="stylesheet" type="text/css">
<script language="JavaScript">
<!--

// Copyright 2001 by www.CodeBelly.com
// Please do *not* remove this notice.

var backImage = new Array(); // don't change this

// Enter the image filenames you wish to use.
// Follow the pattern to use more images. The
// number in the brackets [] is the number you
// will use in the function call to pick each
// image.

// Note how backImage[3] = "" -- which would
// set the page to *no* background image.

backImage[0] = "portfolio/11.jpg";
backImage[1] = "portfolio/22.jpg";
backImage[2] = "portfolio/33.jpg";
backImage[3] = "portfolio/44.jpg";

// Do not edit below this line.
//-----------------------------

function changeBGImage(whichImage){
if (document.body){
document.body.background = backImage[whichImage];
}
}

//-->
</script>

</head>
<body >
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<font class="destacadoportfolio1">La Gresca</font><font class="destacadoportfolio2"><br>
texto </font> <br>
<span class="destacadoportfolio1"><a href="javascript:changeBGImage(0)">1</a> <a href="javascript:changeBGImage(1)">2</a> <a href="javascript:changeBGImage(2)">3</a> <a href="javascript:changeBGImage(3)">4</a> </span></td>
</tr>
</table>
</body>
</html>[/HTML]
Mar 4 '08 #9
acoder
16,027 Expert Mod 8TB
In changeBGImage, change the JavaScript to:
Expand|Select|Wrap|Line Numbers
  1. document.body.style.backgroundImage = "url("+backImage[whichImage]+")";
PS. use [code] tags when posting code. Thanks!
Mar 5 '08 #10
xbcn27
6
it works !! hehe.. thank you very much ..
Mar 5 '08 #11
acoder
16,027 Expert Mod 8TB
No problem. Glad it's working!
Mar 5 '08 #12
I have question for this script.
How to add link page target onclick bottom?

for example:
changeBGImage(0) on page1.php, so open page1.php with image (0)
changeBGImage(1) on page2.php, so open page2.php with image (1)
changeBGImage(2) on page2.php, so open page3.php with image (2)
etc

What's idea?

Many Thanks
Mar 22 '08 #13
acoder
16,027 Expert Mod 8TB
So if I understand correctly, you want to open a page with the image as background? If it's the same page, you can change the background image without opening a new page.
Mar 22 '08 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Nathan Given | last post by:
Hello All, I am trying to randomly change the background image of my home page but I can't seem to figure it out. Here is a snippet of my css .... BODY {background:transparent...
1
by: nick | last post by:
I have the following code: <style> #item1 { DISPLAY: block; BACKGROUND: url(ProjMenuImgs/ProjBtn.gif) no-repeat 0px 0px; WIDTH: 87px; HEIGHT: 94px; } #item1:hover { background-position: 0...
3
by: Bobofrut | last post by:
Hi In my ASP.NET application I try to change dynamicly picture in background of page. Ther is any way to do it without using webcontrols(like panel) I wrote the public function named getName()...
1
by: Mike | last post by:
I want to change the background image of the html page within code based on the users selection. I'm trying to use this in the page onload even response.write("<body background=' & dataitem>") ...
2
by: urgido | last post by:
How I can change the background image to <li> tag when is clicked? Regards
6
by: spit0033 | last post by:
I have created a vertical menu with a white background, once a link is clicked i want that menu item's background color to change. Then once another menu item is clicked the first menu items...
4
by: Israel Richner | last post by:
I know I can use <a></aand modify it's :hover and :visited appearance in my .css file. I'd like to know if I can do the same for a asp:button control. I'd like to do some processing of data in...
1
by: can2564 | last post by:
Hello, I have an image as a background in the style sheet. The image is big and I would like to make it smaller. Can I do that in the style sheet? I really don't have room to crop. This is...
3
by: vartana | last post by:
I will be getting the background url dynamically using ajax and html. I have set an id=boxFooter_002 to my div and have ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.