Skip to content

Instantly share code, notes, and snippets.

@ErDmKo
Last active December 20, 2015 00:49
Show Gist options
  • Save ErDmKo/6044451 to your computer and use it in GitHub Desktop.
Save ErDmKo/6044451 to your computer and use it in GitHub Desktop.
A simple example of the function of maintaining the scale and position of the image window is resized.
var scaleKepper = function(e){
var obj = $(/*selector*/)[0],
windowWidth = $(window).innerWidth(),
windowHeight = $(window).innerHeight(),
aspect = obj.clientWidth/obj.clientHeight,
result = windowWidth/aspect > windowHeight ?
'width:'+windowWidth+"px;height:"+windowWidth/aspect+"px;margin-top:-"+((obj.clientHeight-windowHeight)/2)+"px;" :
'width:'+windowHeight*aspect+'px;height:'+windowHeight+'px;margin-left:-'+((obj.clientWidth-windowWidth)/2)+'px';
obj.setAttribute('style', result);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment