Created
October 5, 2012 17:16
-
-
Save say2joe/3841088 to your computer and use it in GitHub Desktop.
Render Hi-Res IMGs for Retina Displays (JavaScript)
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
/** | |
* Retina image update script. Invoke with Retina.render(). | |
* Retina.render() may be passed a selector for img selection. | |
* The default filter looks for imgs with data-scale="retina". | |
* @param {string|function} expr A CSS selector expression or jQuery filter function. | |
* @return {boolean|collection} Returns false or (for Retina displays) the images updated. | |
* @author Joe Johnson (joe.johnson@icrossing.com) | |
*/ | |
(function(ns,$){ | |
ns.Retina = { | |
render: (function(isRetina){ | |
return isRetina ? function(expr){ | |
var r1 = /retina/i, r2 = /\d{3}/, | |
filter = expr ? expr : function(){ | |
return r1.test($(this).data("scale")); | |
}; | |
return $("img").filter(filter).each(function(){ | |
this.src = this.src.replace(r2,function(m){ | |
return (parseInt(m,10)>=640 ? m : 2*m); | |
}); | |
}); | |
} : function(){ return false; }; | |
})(ns.devicePixelRatio && (ns.devicePixelRatio > 1)) | |
}; | |
})(window,jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment