<script type="text/javascript"> window.onload = function () {
var header = document.getElementById('header'); var pictures = new Array('images/image1.jpg','images/image2.jpg','images/image3.jpg','images/image4.jpg','images/image5.jpg'); var numPics = pictures.length; if (document.images) { var chosenPic = Math.floor((Math.random() * numPics)); header.style.background = 'url(' + pictures[chosenPic] + ')';
} } </script>
Unfortunately the script switches background images but the sprites which are linked to from the html stay on top and are not replaced. I need to find a way to replace the existing images or hide them.
What do you mean by "randomly" hide? You could go: artcontainer.style.display = 'none';
I'm not exactly sure what the problem is - Perhaps I'm reading it incorrectly. Also are you intentionally trying to avoid jQuery? (I'm asking because my javascript capabilities are embarrassing lol)
I am assuming that the 'art-boxes' contains thumbnailed versions of the random backgrounds? When the page loads, choose a random background, and hide it from the list of other backgrounds? A kind of reverse gallery selection?
Again I'm with @jamy_za and although I understand your script, if it was written in jQuery it would seem to read more semantically.
artcontainer.style.display = 'none'; // vs $('#artcontainer').hide();
I also find it easier to draft out ideas - even if the script itself is not valid (I would have at least broken the procedure down!):
$('#header').css('background-image', picture-url); //'picture-url' being the result of the math; $('#artcontainer a[href="'+ picture-url'"]).hide(); //Find the link to the same image, and hide it;
The code hasn't been tested, so may not work - but even then you can see the method I was aiming for?
Thanks for getting back to me, it's too late for me to look at this now and my brain is fried. Anyway I have put up a very basic cleaned out version of the problem. As you can see the script changes out the background images on refresh, but the sprite css rollovers remain on top and visible even though they are also background images. I would like them to appear randomly on refresh as the other background images do, but I don't know how to write it into the script. I'm not exactly a Jquery or JS Jenius.
P.S. Later.....I tried out both of your solutions, but obviously I didn't explain the problem correctly.
Thanks for anwering, but that is not at all what I want to do. There are 5 background images which show on Refresh, my problem is that they show up behind those 12 boxes, I want each of the 5 images to show up and then have the 12 boxes show up as if it were the 6th image.
window.onload = function () { var header = document.getElementById('header'); var pictures = new Array('images/image1.jpg','images/image2.jpg','images/image3.jpg','images/image4.jpg','images/image5.jpg','art-boxes'); var numPics = pictures.length; if (document.images) { var chosenPic = pictures[Math.random() * numPics];
So, you want the five background images to alternate in front of the boxes, while this happens the boxes will not be visible. Then, after the five images, the boxes show up. Is that right? Now, if you want the background images to show in front of the boxes you will have to actually put them as an image in front of the boxes with absolute positioning and a higher z-index(all this with css). then have a function change the image in front of the boxes in an interval that you specify like setInterval(). Also, when the function runs through all the images, just place an empty source and nothing will show up, therefore leting the person see the blocks... Sorry if it doesn't make sense, but I wrote this in a real big rush.. Hope this helps...
Well i should read it twice after writing code without testing it or: copy/pasting is bad :P There is no element with the id of 'header' in your dom.. Change also:
var header = document.getElementById('contentright');
Thank you guys so much for taking a look, I'm just reading this quickly on Superbowl Sunday, so I will have another trial at it tomorrow with a fresh look and get back to you to let you know the results.
Unfortunately the script switches background images but the sprites which are linked to from the html stay on top and are not replaced. I need to find a way to replace the existing images or hide them.
Is there some JS that I can add to the script to tell it to randomly hide the sprites e.g. artcontainer = 'hide'
You could go:
artcontainer.style.display = 'none';
I'm not exactly sure what the problem is - Perhaps I'm reading it incorrectly. Also are you intentionally trying to avoid jQuery? (I'm asking because my javascript capabilities are embarrassing lol)
Again I'm with @jamy_za and although I understand your script, if it was written in jQuery it would seem to read more semantically.
I also find it easier to draft out ideas - even if the script itself is not valid (I would have at least broken the procedure down!):
The code hasn't been tested, so may not work - but even then you can see the method I was aiming for?
P.S. Later.....I tried out both of your solutions, but obviously I didn't explain the problem correctly.
http://emasai.com/depinho/jstrial.html
well with jquery try:
I havn't testet it, so don't blame me ^^
There are 5 background images which show on Refresh, my problem is that they show up behind those 12 boxes, I want each of the 5 images to show up and then have the 12 boxes show up as if it were the 6th image.
and html
Hope thats want you want ;)
change:
After some sleep i will test the whole thing.
There is no element with the id of 'header' in your dom..
Change also: