Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Resize backgroung image with Javascript

Posted on 2008-09-29
13
Medium Priority
?
1,282 Views
Last Modified: 2012-06-22
Hi Everyone,

I'm trying to use a backgroung image for a website that I'm devoloping,the image is quite large,what I want is a small image and resizes autamitacly according to users resolution.
Here  is the site I wanted like,I couldnt figure out how did they achive:
mediacom.com.

Thanks                    
0
Comment
Question by:informabiz
13 Comments
 
LVL 15

Expert Comment

by:MMDeveloper
ID: 22596566
what they did was not use a background image per-say.. They embedded the image directly into the page as you normally would. THen used CSS to make sure it was behind everything, and then a sprinkly of jQuery javascript to dynamically resize the image. The relevant jquery code was:

/* page.js|customers */
// to prevent loadflickering the html is hidden immediately
if (document.getElementById) {
      document.write('<style type="text/css">');
      document.write('body #bg {display: block; visibility: visible}');
      document.write('</style>');
}

function resize() {
      var bg = document.getElementById('bg');
      if (bg) {
            if (!bg.aspectRatio) {
                  if (bg.offsetWidth && bg.offsetHeight) {
                        bg.aspectRatio = bg.offsetWidth / bg.offsetHeight;
                  } else {
                        return;
                  }
            }
            var w = window.innerWidth || document.body.clientWidth;
            var h = window.innerHeight || document.body.clientHeight;
            if ((w/h) > bg.aspectRatio) {
                  bg.style.width = '100%';
                  bg.style.height = '';
            } else {
                  bg.style.width = '';
                  bg.style.height = '100%';
            }
      }
}
setInterval('resize()', 100);


and the relevant CSS was

#bg {position: fixed; z-index: 1; margin: 0; padding: 0; display: none; visibility: hidden}
where "bg" is the ID of the image.

They then have the rest of their content inside a wrapper with an ID of "mdWrap" and they simply use CSS to ensure that it's 'layered over' the image.

#mdWrap {z-index: 2;}
0
 
LVL 8

Expert Comment

by:MatthiasVance
ID: 22596689
You do not need to use JavaScript to perform this "trick", you can embed the image in a DIV with CSS rules applied to it so it stretches and stays in the background.

Kind regards,

Matthias Vance
#background {
	height: 100%;
	left: 0;
	position: fixed;
	top: 0;
	width: 100%;
}
#background img {
	height: 100%;
	width: 100%;
}
 
#foreground {
	height: 100%;
	position: relative;
	width: 100%;
	z-index: 1;
}

Open in new window

0
 
LVL 5

Expert Comment

by:jose_juan
ID: 22596866
Hi,

you can not set background dimensions on css or javascrip time (see w3c specifications) but you have alternatives, the trivial way are a layer on lower z-index

see solution running on

http://jose-juan.computer-mind.com/test/background.htm
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:informabiz
ID: 22596926
When I add as an image on body,scrolling become imposibble,Jquery has solution but I couldnt find the scripting for it.
0
 
LVL 5

Expert Comment

by:jose_juan
ID: 22597057
Hi informabiz:

You need resize a image (my example can make this), what is the problem?, what dimensions you need? refered to body, refered to client-rect, fixed on scroll-view, ...
0
 

Author Comment

by:informabiz
ID: 22597240
The images are look quite big on smaller resolutions.
Here is the link.

http://www.mediacomuk.com/info/MediaComNorth/index.html

I have used  javascript to resize the image but smaller monitors or laptops cant display properly.

Also I have been fiddeling http://www.shotview.com/vintage this website.Their background is emaining same on all screens.
0
 
LVL 5

Expert Comment

by:jose_juan
ID: 22597581
Hi,

then, you need this

http://jose-juan.computer-mind.com/test/background.htm

when user change the scroll position or resize window, the script put the layer on current viewport dimensions and position. Onload is needed for init purpose.

On the other hand, your background are 450K bytes!!! compress this or use jpg on low quality.
0
 

Author Comment

by:informabiz
ID: 22597876
Background is not sharp and also when you scroll it,it blinks..
0
 
LVL 5

Accepted Solution

by:
jose_juan earned 2000 total points
ID: 22598129
Yes! your image are 1300x870 pixels! 3,24 memory!!!! and the background size are calculate in realtime, the advantage are that all size and ratios are available.

You have other alternative, prepare many background versions and use expressions on CSS time if you don't like recompute background on resize events. Best solution.

You can put this on onload event if you don't like css expressions or would recompute resize.

See (again)

http://jose-juan.computer-mind.com/test/background.htm

and try resize width of your browser.


0
 

Author Comment

by:informabiz
ID: 22603661
Hi Jose Juan,
It is working,thank you very much,is there ant way I can stop the scrolling?

Regards
0
 
LVL 5

Expert Comment

by:jose_juan
ID: 22603765
Hi,

I don't understand you... you would disable scroll bars?
0
 

Author Comment

by:informabiz
ID: 22603888
It is sorted now,thanks
0
 

Author Closing Comment

by:informabiz
ID: 31501179

            <script>
                  function move_fixed_background() {
                        var v
                        if( document.body.clientWidth <= 800 )
                              v = 'url(images/1024.jpg)';
                        else
                              v = 'url(images/bg.gif)';
                        if( document.body.style.backgroundImage != v )
                              document.body.style.backgroundImage = v;
                  }
</script>
0

Featured Post

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses

916 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