Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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