Talking about Css, Html and Javascript.
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:
function resize_bg(){
$("#triquibackimg").css("left","0");
var doc_width = $(window).width();
var doc_height = $(window).height();
var image_width = $("#triquibackimg").width();
var image_height = $("#triquibackimg").height();
var image_ratio = image_width/image_height;
var new_width = doc_width;
var new_height = Math.round(new_width/image_ratio);
if(new_height
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:
$(document).ready(function() {
$("#triquibackimg").load(function(){
resize_bg();
})
$(window).resize(function(){
resize_bg();
})
function resize_bg(){
$("#triquibackimg").css("left","0");
var doc_width = $(window).width();
var doc_height = $(window).height();
var image_width = $("#triquibackimg").width();
var image_height = $("#triquibackimg").height();
var image_ratio = image_width/image_height;
var new_width = doc_width;
var new_height = Math.round(new_width/image_ratio);
if(new_height
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.
Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.