Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to have Background Image Change on Refresh?

Posted on 2013-11-20
11
Medium Priority
?
418 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 2000 total points
ID: 39665498
0
 

Author Closing Comment

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
In this tutorial viewers will learn how to embed custom externally-hosted Google Fonts using the Google Font API in CSS Go to the Google Fonts website at google.com/fonts: Browse or search based on font properties or name to find a suitable font for…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

604 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