Resize to a Specific Element’s Dimensions

Here’s a snip­pet to resize a back­ground image to the dimen­sions of an ele­ment. In this case, #bkgIm­age.

var resizeBkg = function() { 
    var h = self.innerHeight || document.documentElement.clientHeight || document.body.clientHeight; 
    var w = self.innerWidth || document.documentElement.clientWidth || document.body.clientWidth; 
    var e = document.getElementById('bkgImage'); 
    if (w > h) { 
        e.width = w; 
        e.height = w; 
    } else { 
        e.height = h; 
        e.width = h; 
    } 
} 

Comments are currently closed.