Solved

Resize backgroung image with Javascript

Posted on 2008-09-29
13
1,262 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
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…

744 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

12 Experts available now in Live!

Get 1:1 Help Now