Solved

How to have Background Image Change on Refresh?

Posted on 2013-11-20
11
410 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

749 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