Skip to content

Instantly share code, notes, and snippets.

@mpjura
Created October 24, 2012 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpjura/3948443 to your computer and use it in GitHub Desktop.
Save mpjura/3948443 to your computer and use it in GitHub Desktop.
PixelToPercent / PercentToPixel
/*jshint browser:true */
/*global _ */
//requires underscore.js for debounced function
(function(win, _){
"use strict";
var width = win.innerWidth,
handleResize;
win.pixelToPercent = function pixelToPercent(pixel){
return ( pixel / width ) * 100;
};
win.percentToPixel = function percentToPixel(percent){
return Math.round( ( percent * width ) / 100 );
};
handleResize = _.debounce(function(){
width = window.innerWidth;
}, 100);
win.addEventListener('resize', handleResize, false);
})(window, _);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment