Last active
August 29, 2015 14:05
-
-
Save EdCharbeneau/f035747c48943be4337d to your computer and use it in GitHub Desktop.
Get and display css properties at runtime.
This file contains hidden or 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
(function () { | |
displayCssInfoFor(); | |
})(); | |
// A simple helper to display the CSS properties of an element at runtime. | |
// Use: | |
// By defualt, displays the color of the previous sibiling. | |
// <span data-properties-for></span> | |
// Specify element | |
// <span data-properties-for='{"element": "selector"}'></span> | |
// Specify properties | |
// <span data-properties-for='{"css": "[array of css properties]"}'></span> | |
// Specify both | |
// <span data-properties-for='{"element": "selector", "css": "[array of css properties]"}'></span> | |
function displayCssInfoFor() { | |
var selector = "[data-properties-for]"; | |
var $elems = $(selector); | |
$elems.each(function (index, elem) { | |
var $displayElem = $(elem); | |
var $targetElem = $displayElem.data("properties-for").element || $(elem).prev(); | |
var styles = $displayElem.data("properties-for").css || ["color"]; | |
styles.forEach(function (style, i) { | |
var isLast = styles.length - 1 === i; | |
$displayElem.append(style + ": " + $($targetElem).css(style)); | |
if (!isLast) $displayElem.append("; ") | |
}) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment