Last active
August 29, 2015 14:09
-
-
Save joe-watkins/c67139c1276aae8f258d to your computer and use it in GitHub Desktop.
svgFallbacks.js - offer png fallback for inline svg - uses Modernizr
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
/*! | |
* 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