Last active
December 16, 2015 05:29
-
-
Save danbrianwhite/ec545a8ead2665ae22dc to your computer and use it in GitHub Desktop.
pixelToPercent
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
var pixelToPercent = function(elements) | |
{ | |
var _patt=/px/; | |
for (var i = elements.length - 1; i >= 0; i--) | |
{ | |
var _this = elements[i]; | |
var _parentWidth = _this.parentNode.offsetWidth; | |
var _parentHeight = _this.parentNode.offsetHeight; | |
var _style = getStyle(_this); | |
var _tempWidth = _this.style.width; | |
var _dragWidth = _tempWidth === "" ? _style.width : _tempWidth; | |
if(_patt.test(_dragWidth)) | |
{ | |
var _dragWidth = _this.offsetWidth; | |
_this.style.width = ((_dragWidth/_parentWidth)*100)+'%'; | |
} | |
var _tempHeight = _this.style.height; | |
var _dragHeight = _tempHeight === "" ? _style.height : _tempHeight; | |
if(_patt.test(_dragHeight)) | |
{ | |
var _dragHeight = _this.offsetHeight; | |
_this.style.height =((_dragHeight/_parentHeight)*100)+'%'; | |
} | |
var _tempTop = _this.style.top; | |
var _dragTop = _tempTop === "" ? _style.top : _tempTop; | |
if(_patt.test(_dragTop)) | |
{ | |
_this.style.top = ((parseFloat(_dragTop)/_parentHeight)*100)+'%'; | |
} | |
var _tempLeft = _this.style.left; | |
var _dragLeft = _tempLeft === "" ? _style.toleftp : _tempLeft; | |
if(_patt.test(_dragLeft)) | |
{ | |
_this.style.left = ((parseFloat(_dragLeft)/_parentWidth)*100)+'%'; | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment