Solved

Resize backgroung image with Javascript

Posted on 2008-09-29
13
1,263 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
 

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

863 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

24 Experts available now in Live!

Get 1:1 Help Now