Skip to content

Instantly share code, notes, and snippets.

@DaniloShan
Last active August 29, 2015 14:20
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 DaniloShan/0586f806159ae50f7234 to your computer and use it in GitHub Desktop.
Save DaniloShan/0586f806159ae50f7234 to your computer and use it in GitHub Desktop.
function getStylePropValInNum (el, prop) {
	if (!prop || typeof prop !== 'string' || !el || el.nodeType !== 1) return 0;
	
	var reg = /(\d*)px/, matchResult, result;
	matchResult = el.style[prop].match(reg);
	
	if (matchResult && matchResult.length === 2) {
		result = +matchResult[1];
	}
	
	return isNaN(result) ? 0 : result;
}

getStylePropValByNum(document.querySelectorAll('div')[0], 'marginTop');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment