Created
September 19, 2018 19:22
-
-
Save johnny5th/2484eb35748ee0f93dc6169a113b97a5 to your computer and use it in GitHub Desktop.
SVG Image Loading
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
// 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