Solved

How to have Background Image Change on Refresh?

Posted on 2013-11-20
11
402 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:Stiebel Eltron
Comment Utility
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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 9

Expert Comment

by:oheil
Comment Utility
<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
Comment Utility
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
Comment Utility
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
Comment Utility
@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
Comment Utility
0
 

Author Closing Comment

by:Stiebel Eltron
Comment Utility
Great! Got the idea...
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
In this tutorial viewers will learn how to define a gradient in CSS. Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Gradient. Define the background as "linear-gradient(to right, #ee3668, black)". Ensure you …
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now