Skip to content

Instantly share code, notes, and snippets.

@bacoords
Created October 13, 2020 03:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bacoords/c52476b80edd5c34770cc4d768410f8a to your computer and use it in GitHub Desktop.
Save bacoords/c52476b80edd5c34770cc4d768410f8a to your computer and use it in GitHub Desktop.
public - function to load large images
// Function to background load images and swap out their source.
function replaceImageSrc() {
jQuery('svg image').each(function(){
var el = jQuery(this);
// Get the new href.
var href = el.attr('xlink:href');
href = href.replace( 'assets_small', 'assets_large' );
href = href.replace( '-s.', '-l.' );
// Create an image.
var curImg = new Image();
curImg.src = href;
curImg.onload = function(){
// do whatever here, add it to the background, append the image ect.
// imgHolder.appendChild(curImg);
el.attr( 'xlink:href', href );
console.log( href );
}
});
}
document.addEventListener('DOMContentLoaded', replaceImageSrc );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment