473,378 Members | 1,527 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,378 software developers and data experts.

Multiple backgrounds

I can set/change a background with:

document.getElementById("photodiv").style.backgrou nd =
"#282828 url(../img/pict_1.jpg)";

How do I set/change multiple backgrounds using javascript?
Aug 25 '08 #1
6 3057
On Aug 25, 8:37 pm, Johan <por...@can.spamwrote:
I can set/change a background with:

document.getElementById("photodiv").style.backgrou nd =
"#282828 url(../img/pict_1.jpg)";

How do I set/change multiple backgrounds using javascript?
Repeat that line for each div?
Aug 26 '08 #2
SAM
Johan a écrit :
I can set/change a background with:

document.getElementById("photodiv").style.backgrou nd =
"#282828 url(../img/pict_1.jpg)";

How do I set/change multiple backgrounds using javascript?
The easiest way is to use a class in the body tag

document.body.className = 'back_1';
document.body.className = 'back_2';

CSS :
=====
..back_1 { background: yellow; }
..back_1 div { background: url(../img/pict_1.jpg) }
..back_1 div.special { background: #282828 }
..back_2 { background: white; }
..back_1 div { background: url(../img/pict_2.jpg) }
/* and so on */
Or using a loop on elements to change :

var D = document.getElemementsByTagName('DIV');
for(var i=0, n = D.length; i<n; i++)
D[i].style.background="#282828 url(../img/pict_1.jpg)";
You can also use alternative style sheets

--
sm
Aug 26 '08 #3
SAM wrote:
Johan a écrit :
>I can set/change a background with:

document.getElementById("photodiv").style.backgro und =
"#282828 url(../img/pict_1.jpg)";

How do I set/change multiple backgrounds using javascript?


The easiest way is to use a class in the body tag

document.body.className = 'back_1';
document.body.className = 'back_2';

CSS :
=====
.back_1 { background: yellow; }
.back_1 div { background: url(../img/pict_1.jpg) }
.back_1 div.special { background: #282828 }
.back_2 { background: white; }
.back_1 div { background: url(../img/pict_2.jpg) }
/* and so on */
Or using a loop on elements to change :

var D = document.getElemementsByTagName('DIV');
for(var i=0, n = D.length; i<n; i++)
D[i].style.background="#282828 url(../img/pict_1.jpg)";
You can also use alternative style sheets
Thank for your suggestions.

However, my question is how I can set/change multiple backgrounds with
javascript. I think I wasn't clear but they should appear in one div.
You can set them with css in this way:
#show {
background-image:
url("images/pict_1.jpg"),
url("images/pict_2.jpg"),
url("images/pict_3.jpg"),
url("images/pict_4.jpg");
background-repeat: no-repeat, no-repeat, no-repeat, no-repeat;
background-position: top left, top right, bottom right, bottom left;
}
Note: it's one sigle div.

Now I want to change them with javascript.
Aug 26 '08 #4
Laser Lips wrote:
On Aug 25, 8:37 pm, Johan <por...@can.spamwrote:
>>I can set/change a background with:

document.getElementById("photodiv").style.backgr ound =
"#282828 url(../img/pict_1.jpg)";

How do I set/change multiple backgrounds using javascript?


Repeat that line for each div?
They should appear in one div.
Aug 26 '08 #5
On 26 Aug., 16:06, Johan <por...@can.spamwrote:
SAM wrote:
You can set them with css in this way:
#show {
* *background-image:
* * *url("images/pict_1.jpg"),
* * *url("images/pict_2.jpg"),
* * *url("images/pict_3.jpg"),
* * *url("images/pict_4.jpg");
* *background-repeat: no-repeat, no-repeat, no-repeat, no-repeat;
* *background-position: top left, top right, bottom right, bottom left;}
You are aware, that this is CSS3 and is currently supported only by
Safari and KHTML-based browsers?
Now I want to change them with javascript.
Basically by combining the what you posted in your original post and
here:

var theDivStyle = document.getElementById("photodiv").style;
theDivStyle.backgroundImage = 'url("images/pict_1.jpg"), url("images/
pict_2.jpg"), url("images/pict_3.jpg"), url("images/pict_4.jpg")';
theDivStyle.backgroundRepeat = 'no-repeat, no-repeat, no-repeat, no-
repeat';
theDivStyle.backgroundPosition = 'top left, top right, bottom right,
bottom left';

Or by using the shorthand property as in CSS

theDivStyle.background = 'url("images/pict_1.jpg") no-repeat top left,
url("images/pict_2.jpg") no-repeat top right, ...'; /* abbreviated */

(watch the line breaks)

Robin
Aug 26 '08 #6
SAM
Johan a écrit :
>
However, my question is how I can set/change multiple backgrounds with
javascript. I think I wasn't clear but they should appear in one div.

You can set them with css in this way:
??? in which navigator can you have this kind of CSS ?
(my Fx told me it is not good)
#show {
background-image:
url("images/pict_1.jpg"),
url("images/pict_2.jpg"),
url("images/pict_3.jpg"),
url("images/pict_4.jpg");
background-repeat: no-repeat, no-repeat, no-repeat, no-repeat;
background-position: top left, top right, bottom right, bottom left;
}
Note: it's one sigle div.
and there is no image in background.
Now I want to change them with javascript.
Why for ?
because you want to correct the error ?
CSS :
=====
#show { background: url(images/pict1.jpg) no-repeat top left #ffc; }
#show.p_tr { background-position: top right; }
#show.p_br { background-position: bottom right; }
#show.p_bl { background-position: bottom left; }
#show.i_l { background-image: url(images/pict2.jpg) }
#show.i_2 { background-image: url(images/pict3.jpg) }
#show.i_3 { background-image: url(images/pict4.jpg) }

JS :
====
function chang(img, pos) {
var d = document.getElementById('show');
d.className = img+' '+pos;
}

HTML:
=====

<div id="show" style="height:600px">
<p><a href="javascript:chang('i_1','p_tr')">
pict #2 top right</a>
<p><a href="javascript:chang('i_1','p_br')">
pict #2 bottom right</a>
<p><a href="javascript:chang('i_2','p_bl')">
pict #3 bottom left</a>
<p><a href="javascript:chang('','')">
original pict and position</a>
</div>

======== soluce 2 =============

CSS :
=====
#show { background: url(images/pict1.jpg) no-repeat top left #ffc; }

JS :
====
function chang(img, pos) {
var d = document.getElementById('show').style;
d.backgroundPosition = pos;
d.backgroundImage = 'url(images/'+img+'.jpg)';
}

HTML :
======
<div id="show" style="height:600px">
<p><a href="javascript:chang('pict2','top right')">
pict #2 top right</a>
<p><a href="javascript:chang('pict2','bottom right')">
pict #2 bottom right</a>
<p><a href="javascript:chang('pict3','bottom left')">
pict #3 bottom left</a>
<p><a href="javascript:chang('pict1','top left')">
original pict and position</a>
</div>

--
sm
Aug 26 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: RoDzZzZ | last post by:
when i try print (ctrl +p) my webpage, my printer not print the backgrounds and bgcolors of my tables.... what i need make to show the backgrounds and bgcolors in my paper when the user print...
2
by: Greg Locke | last post by:
Hi All I am programming MFC Visual C++ using Microsoft Visual Studio.net 2003 under Windows XP professional. I have developed some bitmaps as graphic overlays for buttons. I had trouble finding...
6
by: MLH | last post by:
I have a logo image that I can paste into MS Paint as an image with clear background. Think of the logo as a black circle in a white, rectangular background. When I paste into Paint, the white...
0
by: ghadley_00 | last post by:
Hi, Have created 2 seperate reports that have different images as backgrounds. Would like to have both pages print out as 1 print job (so duplex printer will make each report page on opposite...
6
by: Rik Brooks | last post by:
I'm developing Web Applications with C# code behind. I have found that, because of the particular nuances of my company, I am stuck with positioning the items on the page with tables. No problem...
1
gregerly
by: gregerly | last post by:
I'm a total flash / actionscript newbie. I have a question about how to create a flash background as seen here: Adobe MAX I don't expect a detailed explanation. I'm just looking for some info...
1
by: alice | last post by:
I'm trying to have a background image in a div, then have several divs within that div that have black backgrounds. The problem I'm finding is that in Firefox and Safari, the first background image...
1
by: matturn | last post by:
Is there any way to get IE7 to render the whole viewpoint without forcing a page reload? If you set a DIV to 100% wide or high, and give it a background; that background will not be rendered if it...
2
duhcoolies
by: duhcoolies | last post by:
I have a table-cell (width=!00%) with a background (BG1), and within this table-cell I have another table-cell (again width=100%) with another background (BG2). BG1 needs to be tiled horizontally...
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: 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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.