Fullscreen background image with jQuery
Set an image as fullscreen background in your html document, that is centered and proportionally scaled according to browser window.
Load the jquery library straight from Google’s CDN in your html document
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js" type="text/javascript"></script>
The code above can be inserted inside the head tag or at the very bottom of your document, before the closing body tag (which is normally recommended for better performance).
Add the markup of your background image
<div id="bg"> <img src="image-name.jpg" width="1280" height="800" id="bgimg" /> </div>
Change the source attribute image-name.jpg
to your image path and set its actual width and height attributes.
The css for the image container:
body{margin:0px; padding:0px; background:#000;} #bg{position:fixed; z-index:1; overflow:hidden;}
Next we need to insert the actual function that does all the work of streching proportionaly and centering our image. So after the jquery inclusion insert:
<script type="text/javascript"> function FullScreenBackground(theItem){ var winWidth=$(window).width(); var winHeight=$(window).height(); var imageWidth=$(theItem).width(); var imageHeight=$(theItem).height(); var picHeight = imageHeight / imageWidth; var picWidth = imageWidth / imageHeight; if ((winHeight / winWidth) < picHeight) { $(theItem).css("width",winWidth); $(theItem).css("height",picHeight*winWidth); } else { $(theItem).css("height",winHeight); $(theItem).css("width",picWidth*winHeight); }; $(theItem).css("margin-left",winWidth / 2 - $(theItem).width() / 2); $(theItem).css("margin-top",winHeight / 2 - $(theItem).height() / 2); } </script>
This function makes our image width and height stretch, depending on the browser and image dimensions ratio.
Finally, insert the functions that check when our page is loaded and browser is resized:
<script type="text/javascript"> window.onload = function () { FullScreenBackground('#bgimg'); } $(window).resize(function() { FullScreenBackground('#bgimg'); }); </script>
Hey – this is very nice, thank you! However i need something a bit different and i was wondering if you could point me in the right direction or maybe even code it if you like the idea. ๐
I want a full screen picture as this when you enter site no matter the size of your browser window. But i also want to be able to scroll down so i kinda use the full screen image as a form for intro but text etc. should only come once i scroll down.
I saw this effect here: http://www.wearetheweather.co.uk/ but i cant seem to find any scripts etc. that will do this trick for me. any help is appreciated!
I want a full screen picture as this when you enter site no matter the size of your browser window. But i also want to be able to scroll down so i kinda use the full screen image as a form for intro but text etc. should only come once i scroll down.
Hey Malihu!
I’ve used your tweak for the background scaling on Mary Lou’s Codrop tutorial (the fullscreen thumbnail navigation one).
I’m having trouble with getting the script to perfectly understand my images. I’ve noticed that images with an unsual aspect ratio will more or less not scale properly at all (for example a picture with height 800px and width 200 px). Here’s an example http://joacimeldre.com/interactive/test
Is there any way to make the script understand and keep the original image aspect ratio? I know it would probably mean getting a “cutoff” with a simple background on for example the bottom or the right side, but that doesn’t matter.
Either way, thanks for the help this tweak has given – I’m far from experienced enough to dive into this on my own.
Hello Malihu
Your work’s much appreciated, I’ve found it very useful in creating a portfolio site!
I have one problem though: My background images are not centered straight away upon loading the page; they only center once the window gets resized manually. I tried adding:
$(window).onload(function() {
FullScreenBackground(‘img.st_preview’);
});
…but it still doesn’t work. Any idea why?
Many thanks!
Actually, I noticed that it’s missing from your edit of Mary Lou’s script as well… See, when I open your index.html and then resize the window, the background image jumps suddenly because the centering doesn’t take effect until a window.resize happens. Do you have an easy fix for this?
All in all, excellent work! Keep it up!
Hello, is there a way to have a fullscreen background and have a table in the midle of the page centred? to load content
Thank you
Hi,
Placing your content (a table or anything you want) in the html and giving it a z-index of 2 will do the trick.
If you know the exact height of your content, you can center it vertically easily by adding few simple CSS rules. For instance if your content height is say 400 pixels, you can do the following:
.content { position: absolute; height: 400px; top: 50%; margin-top: -200px; }
You can also center it horizontally by defining left and right margin to auto:
margin: -200px auto 0 auto;
If you don’t know the height of your content and still need to center it vertically you’d need to implement some jquery like this:
http://manos.malihu.gr/center-div-with-jquery
Hello, This is a great scrip (easy explained) and it works perfectly only I bummed into a little problem.
I’m using the PHP tag in my website.
But using this makes the z-index don’t work anymore, it will place the content behind the background image.
And if you take a look at the source code the include tag is not even in there anymore.
Do you know what I’m doing wrong here.
thanks for your help….
Hi malihu!
Great stuff you’re doing here!
I wanted to use the fullscreen background script with the fade in effect, but it’s not working in IE9 and Opera. Is there any way to get it to work?
Thanks a bunch!
Check this:
http://manos.malihu.gr/fullscreen-background-image-inside-frame-with-jquery
It’s pretty much the same script with a margin-frame (which you can remove), a preloader animation and a fade-in effect ๐
Thanks so much! It worked for IE but still no Opera. Then again, I guess I can live with that…
I now have another question, though. I was also using your custom scroller script “jquery.mCustomScrollbar.js” and it’s giving me a conflict with “prototype.js”. I tried using the jQuery.noConflict(); solution, but it’s not working. Any tips on how I can remove the conflict?
Again, thank you, thank you, thank you!
bro, my fren play with the http://manos.malihu.gr/tuts/ThumbnailsNavigationGallery_EditedByMalihu.zip and discover ie6 has serious problem after the fullscreen script added in. you will notice it auto double up browser size (enlarge to bottom) when you click on thumb image.
In other words, you notice another image appear at the bottom of browser when you click on thumb. he wish to use the codrop with your fullscreen solution. arigato.
The “thumbnails navigation gallery” is a script created by codrops (http://tympanus.net/codrops/2010/07/29/thumbnails-navigation-gallery/). I just added my fullscreen background function after some user requests made on codrops site and this blog, so I don’t really know if the original script was working in IE6 or not.
Moreover I do not code/support or test any of the scripts in IE6 cause it’s an endless cycle of pain that’s just not worth the time and effort. Why would you care for IE6 anyway?
i understand and appreciate all your effort/help to all reader. i from malaysia, ie(6-8) itself still occupy about 60-70% our market share with ie6 about 20-30% market share. we have ppl using chrome, firefox, ie even ie6.
but dont worry, no problem for the codrop, it is their disability on ie6 anyway. thanks alot.
you have all the great tutorial/ideas with great explanation. appreciate it very much. is there any way to implement this fullscreen ideas to your “ANIMATE PAGE TO ID WITH JQUERY”(each animate page now change to pictures and each picture auto fit to fullscreen)?
thanks.
I plan to make a single page template with page animation and fullscreen background images so keep checking for new posts!
Thanks for your sharing. Due to my limited knowledge, when i use the “animate page to id with jquery” with fullscreen, 2 issues appear. 1st, the sliding not smooth and the fullscreen only happen on 1st page.
I tried the ThumbnailNavi file as in comment section, it work well in chrome just the commonly know problem browser ie6 dun display well. THanks alot for all tut, wait for your new tut.
Thanks for share! , i will try it!
Regards from spain
hi, thanks a lot to share your work, it’s a cool code.
I tried the demo with the start fading effect, but it’s seems to be broken!
I’m a begginer in javascript and i don’t know why.
Any idea?
if you can help me ,
thanks in advance! ๐
Pierre
Ouch finally it work! sorry ๐
hey it is amazing but if i want to change the background images like a slide then is there is any possibility???
This script deals only with the fullscreen image scaling and positioning. A fullscreen slideshow is another story…
Yes no more errors!
Thank you for your time~ much appreciated:D
Hi, thank you for your script.
I’m using the above script you’ve modified(ThumbnailsNavigationGallery_EditedByMalihu.zip), but I’m getting an error on IE7 & 8.
Seems like it’s coming from this line: $(theItem).css(“width”,picWidth*winHeight) and I have no idea what’s wrong with it. Could you please help?
Hi ๐
Unfortunately I won’t be able to check it untill next week cause I’m abroad. As soon as I come back I’ll check it and let you know.
ok thank you, I’ll wait=)
Hello again christy
Please remove line 163 (“FullScreenBackground(this);”). You should get no script error in IE ๐
Also updated the files at http://manos.malihu.gr/tuts/ThumbnailsNavigationGallery_EditedByMalihu.zip
i’m very thankful for your help,
you are a genius my friend!!!!
thanks a lot for your time!
all the best to your life and your upcoming projects!!!
kalispera Mano,
i see today that you suggest to use your script to fix the bug of codrops navigation gallery, but i can’t understand what i have to do to fix it,
i have try a lot to implement your code to Mary Lou’s code but with no luck,
if you can help me it would be nice,
thanks a lot!!!
Philip
Kalispera ๐
About Mary Lou’s script I think you’d have to change a bit the css of “st_main” and “st_preview” by removing the “top” and “left” values. Then you should run the FullScreenBackground function shown in this post (e.g. FullScreenBackground(‘.st_main img.st_preview’)) and add it to the resize function as well. I don’t have much time right now to check it properly but maybe I’ll work on Mary Lou’s script to implement this script with hers. I’ll let you know. Cheers
Philip check the comment area on codrops or get the Mary Lou’s script I edited from here:
http://manos.malihu.gr/tuts/ThumbnailsNavigationGallery_EditedByMalihu.zip