Solved

How to have Background Image Change on Refresh?

Posted on 2013-11-20
11
407 Views
Last Modified: 2013-11-25
Dear EE experts,

We would like to ask on how to have a banner image that everytime you refresh the site, it'll change its image... we already have that, BUT the problem is, we don't know where to set its size. What we want to happen is even we browse it on a high resolution, the image will stay fit on its cell or box. Because we have a web right now, if we browse it on iphone, the banner is fully fit, even if we rotate it, everything's fine. But when we browse it with iPad, Portrait or Landscape, the size of the image stays the same, but its cell or box is stretched and you can see its bgcolor already...
Is there a way that even a user browse the page using iphone, ipad, or any other tablets, the banner image will fit automatically with its box?

jQuery or CSS or Javascript? Please advise...

Thank you!
0
Comment
Question by:Stiebel Eltron
  • 5
  • 3
  • 3
11 Comments
 
LVL 9

Expert Comment

by:oheil
ID: 39665153
You can get the size of the box with
var height=$("#YourBoxID").height();
var width=$("#YourBoxID").width();
and change the image size with that values.
It would be better to pre-format pictures with different sizes for the different viewers.

Oli
0
 

Author Comment

by:Stiebel Eltron
ID: 39665261
Is it PHP? Oh, sorry but we write our script in ASP...
That's why we couldn't understand your advise Oli...

:(
0
 
LVL 9

Expert Comment

by:oheil
ID: 39665272
No, thats javascript jquery style.
The idea is to adjust the image after the page has loaded using JavaScript and the container has the right size.

Oli
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:Stiebel Eltron
ID: 39665278
Can you give us an example? Because we really don't know how to start or what to put inside <HEAD>--</HEAD>, etc.

Please help us for a sample...
0
 
LVL 9

Expert Comment

by:Sar1973
ID: 39665295
I could propose you to define an ID (or name) of the set of your images like "(URL)/myImage1", "(URL)/myImage2", ... and so on and then apply a Javascript code that loads the background image with a random criteria like this:
var myRandomNMB=Math.floor(Math.random()*10)+1; //if you have 10 images
var newBCKGRDImage=myImage+myRandomNMB;
document.getElementById("myImageBCGRD").style.backgroundImage = newBCKGRDImage;
0
 
LVL 9

Expert Comment

by:oheil
ID: 39665362
<html ...>
<head>
...
</head>
<body>
...
<div id="YourBoxID>
<img id="YourImgID" src="some/path/to/image.gif" />
</div>
...
<script type="text/javascript">
<!--
var height=$("#YourBoxID").height();
var width=$("#YourBoxID").width();
$('#YourImgID').width(width);
$('#YourImgID').height(height);
//-->
</script>
</body>
</html>

Open in new window


This is just a beginner example.
A better approach would be to have images available for the possible resolutions you expect. Than you do not change the image size but the image source depending e.g. on the width of the box. But first, you should get the above example running.
If you have problems, put some alerts into it , e.g.:
var height=$("#YourBoxID").height();
var width=$("#YourBoxID").width();
alert(height);
alert(width);
and check if the values make sense.

Oli
0
 

Author Comment

by:Stiebel Eltron
ID: 39665391
Actually, we have a sample of it:
<HEAD>
<SCRIPT LANGUAGE="JavaScript">

<!-- Begin
// Set up the image files to be used.
var theImages = new Array() // do not change this
// To add more image files, continue with the
// pattern below, adding to the array.

theImages[0] = 'IMG/xg-header-00.jpg'
theImages[1] = 'IMG/xg-header-01.jpg'
theImages[2] = 'IMG/xg-header-02.jpg'
theImages[3] = 'IMG/xg-header-03.jpg'
theImages[4] = 'IMG/IL-header.jpg'

// do not edit anything below this line

var j = 0
var p = theImages.length;
var preBuffer = new Array()
for (i = 0; i < p; i++){
   preBuffer[i] = new Image()
   preBuffer[i].src = theImages[i]
}
var whichImage = Math.round(Math.random()*(p-1));
function showImage(){
document.write('<img src="'+theImages[whichImage]+'">');
}

//  End -->
</script>
</HEAD>

<BODY>
<td width="395" valign="middle" bgcolor="#666666">
       	<SCRIPT LANGUAGE="JavaScript">
    	<!-- Begin
		showImage();
		//  End -->
		</script>
    </td>
</BODY>

Open in new window


but the problem is, we don't know where to re-size. Because we want that the image will adjust its size automatically...
0
 
LVL 9

Expert Comment

by:Sar1973
ID: 39665397
So I think you need a code that detects onload the page size (or the device type and size) and sets the image size.
0
 

Author Comment

by:Stiebel Eltron
ID: 39665407
@Sar1973, how? Do u have sample?
Or do you have any advise if we'll change / edit the said scripts above?
0
 
LVL 9

Accepted Solution

by:
Sar1973 earned 500 total points
ID: 39665498
0
 

Author Closing Comment

by:Stiebel Eltron
ID: 39676488
Great! Got the idea...
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question