Skip to content

Instantly share code, notes, and snippets.

@danbrianwhite
Created April 14, 2013 21:35
Show Gist options
  • Save danbrianwhite/bbc74dabeb2292077c2c to your computer and use it in GitHub Desktop.
Save danbrianwhite/bbc74dabeb2292077c2c to your computer and use it in GitHub Desktop.
fillView - resize an element inside of a container to entirely fill the whole container
var fillViewList = {};
var fillViewAdd = function(element)
{
element.style.width = "auto";
element.style.height = "auto";
var _width = element.offsetWidth;
var _height = element.offsetHeight;
fillViewList[element] = {width: _width, height: _height};
}
var fillView = function(element, container)
{
var _listReference = fillViewList[element];
if(typeof(_listReference) === "undefined")
{
fillViewAdd(element);
_listReference = fillViewList[element];
}
var _eWidth =_listReference.width;
var _eHeight = _listReference.height;
var _cHeight = container.offsetHeight;
var _cWidth = container.offsetWidth;
var _newHeight;
var _newWidth;
var expandWidth = function()
{
var _percent = _cWidth/_eWidth;
_newHeight = _eHeight*_percent;
_newWidth = _eWidth*_percent;
}
var expandHeight = function()
{
var _percent = _cHeight/_eHeight;
_newHeight = _eHeight*_percent;
_newWidth = _eWidth*_percent;
}
if( _eWidth < _cWidth && _eHeight < _cHeight)
{
var _diffWidth = _cWidth / _eWidth;
var _diffHeight = _cHeight / _eHeight;
if(_diffWidth > _diffHeight)
{
expandWidth();
}
else
{
expandHeight();
}
}
else if( _eWidth < _cWidth && _eHeight >= _cHeight)
{
expandWidth();
}
else if( _eWidth >= _cWidth && _eHeight < _cHeight)
{
expandHeight();
}
else //if( _eWidth >= _cWidth && _eHeight >= _cHeight)
{
var _diffWidth = _cWidth / _eWidth;
var _diffHeight = _cHeight / _eHeight;
if(_cWidth/_cHeight > 1)
{
if(_eWidth/_eHeight > 1)
{
if(_diffHeight > _diffWidth)
{
expandHeight();
}
else
{
expandWidth();
}
}
else
{
expandWidth();
}
}
else
{
if(_eWidth/_eHeight > 1)
{
expandHeight();
}
else
{
if(_diffHeight > _diffWidth)
{
expandHeight();
}
else
{
expandWidth();
}
}
}
}
element.style.width = _newWidth+'px';
element.style.height = _newHeight+'px';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment