Created
October 24, 2012 19:58
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