Solved

Resize backgroung image with Javascript

Posted on 2008-09-29
13
1,270 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
[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
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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 500 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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

729 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