Full screen centered background image with CSS and jQuery # 2

There are a couple of improvements to do to Full screen centered background image with CSS and jQuery to add some functionality such as window resizing management and make it compatible with Opera.

First, you should change the CSS to

Setting triquibackimg‘s position to relative rather than fixed will make it work in Opera too. Then since we want to resize the background image when it loads and when the user resizes the browser, I made a function to handle resizing:

I removed all the alerts since they became useless. First, I reset image’s left position to zero (line 2), to align it to top left corner. I am doing this because at line 14 left position can be changed due to background alignment.

The remaining script is the same you saw at step 1, with the only exception doc_width and doc_height are determined according to window size rather than document size. I found it to be more accurate.

At this time, the entire javascript can be written this way:

As you can see, resize_bg function is called both when the background image is loaded and when the window is resized.

You can see the final result here and copy the entire source code by looking at the html.

Next steps will be the creation of a standalone jQuery plugin and a WordPress plugin too. Stay tuned.