Solved

Resize backgroung image with Javascript

Posted on 2008-09-29
13
1,268 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
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 how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

730 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