Skip to content

Instantly share code, notes, and snippets.

@stephenbelyea
Last active September 13, 2016 18:26
Show Gist options
  • Save stephenbelyea/9835a5f106e4e840d12bf456daa4e15c to your computer and use it in GitHub Desktop.
Save stephenbelyea/9835a5f106e4e840d12bf456daa4e15c to your computer and use it in GitHub Desktop.
Load and place SVG icons within wrapper container (ES6).
// Loop context for svg icons to load.
// Wrap arg defaults to main wrapper,
// but will accept any query string.
export function setSvgIcons(wrap = "#wrap") {
let ctx = $(wrap + " [data-svg-icon]:not(.loaded)");
if (ctx.length) {
let path = window.location.origin;
for (let i = 0; i < ctx.length; i++) {
let c = ctx[i];
let img = path + c.getAttribute("data-svg-icon");
$(c).load(img, () => {
c.setAttribute("class", "loaded");
});
}
}
}
// Use in markup like this:
// <span data-svg-icon="/images/icons/icon.svg"></span>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment