Skip to content

Instantly share code, notes, and snippets.

@joe-watkins
Last active August 29, 2015 14:09
Show Gist options
  • Save joe-watkins/c67139c1276aae8f258d to your computer and use it in GitHub Desktop.
Save joe-watkins/c67139c1276aae8f258d to your computer and use it in GitHub Desktop.
svgFallbacks.js - offer png fallback for inline svg - uses Modernizr
/*!
* svgFallbacks.js
* Author: Joe Watkins - joe-watkins.io
* Requires - Modernizr for inlinesvg test
* Usage:
* 1. add a class to <img> eg.
* <img class="svg" src="path/file.svg">
*
* 2. add png fallback for older browsers via data attribute
* <img class="svg" src="path/image.svg" data-svg-fallback="/path/image.png" alt="cool icon">
*
* 3. target these images with plugin $(".svg").svgFallbacks();
*
*/
(function($){
$.fn.svgFallbacks = function(options) {
var defaults = {
wrapper: this,
fallbackDataAttr : "svg-fallback"
}
var options = $.extend(defaults, options);
var o = options;
var svgSupport = Modernizr.inlinesvg,
$svgIcons = $(o.wrapper);
if(!svgSupport){
$.each($svgIcons, function(){
$(this).attr("src",$(this).data(o.fallbackDataAttr));
});
}
};
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment