Skip to content

Instantly share code, notes, and snippets.

@johnny5th
Created September 19, 2018 19:22
Show Gist options
  • Save johnny5th/2484eb35748ee0f93dc6169a113b97a5 to your computer and use it in GitHub Desktop.
Save johnny5th/2484eb35748ee0f93dc6169a113b97a5 to your computer and use it in GitHub Desktop.
SVG Image Loading
// SVG Masked Images
var masked_images = document.querySelectorAll('.svg-masked-image');
var changeHref = function(path, img_wrapper) {
var image = img_wrapper.querySelector('svg g image');
image.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', path);
};
for(let i = 0; i < masked_images.length; i++) {
let img = masked_images[i].querySelector('img');
// Load initial image
if( typeof img.currentSrc === 'undefined' ) changeHref( img.src, masked_images[i] );
else changeHref( img.currentSrc, masked_images[i] );
img.onload = function() {
// Old browser
if( typeof img.currentSrc === 'undefined' ) changeHref( img.src, masked_images[i] );
// Modern browser
else changeHref( img.currentSrc, masked_images[i] );
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment