Skip to content

Instantly share code, notes, and snippets.

@runspired
Last active December 20, 2015 00:19
Show Gist options
  • Save runspired/6040508 to your computer and use it in GitHub Desktop.
Save runspired/6040508 to your computer and use it in GitHub Desktop.
Quick Javascript Fix for Google Chrome "scale elements with position relative/absolute within element with position fixed" bug.
/*
Google Chrome fauls to resize the bounding box of elements with relative/absollute position that are within
fixed position elements. (Discovered this problem when making this page: http://jthoburn.com/expressive.ly/splash )
This solution is for an 'em based' layout in which width needed to remain in ems in order to not break a scale-while-scroll
effect, but would work similarly for pixel and percent based layouts as well.
*/
(function(){
var isChrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1
, forceRender = function(e){
e.style.width = ""; //enforce calculation off of native value
e.style.width = (e.offsetWidth + (e.offsetWidth%2? -1 : 1))/(parseFloat( window.getComputedStyle(e, null).getPropertyValue('font-size') )) + 'em';
}
, existingCallback = window.onresize
;
if( isChrome ) {
window.onresize = function(){
if(existingCallback) existingCallback():
forceRender( document.getElementById('logo'));
forceRender( document.getElementById('tagline'));
};
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment