Last active
September 13, 2016 18:26
-
-
Save stephenbelyea/9835a5f106e4e840d12bf456daa4e15c to your computer and use it in GitHub Desktop.
Load and place SVG icons within wrapper container (ES6).
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
// 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