Created
October 24, 2012 19:58
-
-
Save mpjura/3948443 to your computer and use it in GitHub Desktop.
PixelToPercent / PercentToPixel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*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