/
rwd.images.min.js
12 lines (12 loc) · 3.66 KB
/
rwd.images.min.js
1
2
3
4
5
6
7
8
9
10
11
12
/*!
* RWD Images v0.4.5
*
* A lightweight, customisable responsive image solution, which uses a familar media query syntax
*
* Copyright (c) 2014 Matt Stow
*
* http://mattstow.com
*
* Licensed under the MIT license
*/
(function(q,C){var t,B;if(C.getElementsByClassName){t=C.getElementsByClassName("rwdimage")}else{if(C.querySelectorAll){t=C.querySelectorAll(".rwdimage")}else{return}}B=t.length;if(B===0){return}var l,F,n,o,I,J,e,z,c,f,k,h,b,w="",g,D,A,m,v=[],u,G=0,s=0,a=!!q.enquire,H;var d=function(i){return i.replace(/(^\s*)/,"").replace(/src:\s*/gi,"background-image: ").replace(/ratio\((\d+)\/(\d+)\)/gi,function(y,x,j){return((parseInt(x,10)/parseInt(j,10))*100)+"%"})};for(G;G<B;G++){l=t[G];l.setAttribute("data-rwdimage-id",G);F='[data-rwdimage-id="'+G+'"] ';n=l.getAttribute("data-rwdimage").split(",");o=n.length;v[G]={};v[G]["breakpoints"]=new Array(o);if(l.tagName.toLowerCase()==="img"){if(!l.src){l.src="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEHAAAALAAAAAABAAEAAAICRAEAOw=="}if(a){l.setAttribute("data-rwdimage-has-enquire",true)}v[G]["isImage"]=true}v[G]["elem"]=C.querySelector(F);c=l.getAttribute("data-rwdimage-em")==="true"?true:false;f=l.getAttribute("data-rwdimage-em-base")?parseInt(l.getAttribute("data-rwdimage-em-base"),10):16;g="";for(var E=0;E<o;E++){I=d(n[E]);v[G]["breakpoints"][E]={};if(I.match(/^\(\s*?(min|max)/)){if(c){I=I.replace(/m(?:in|ax)-(?:width|height):\s*(\d+)px/gi,function(j,i){return j.replace(i,parseInt(i,10)/f).replace("px","em")})}g+="@media "+I.match(/.*?\{/)[0]+F+I.replace(/(\((min|max).*?\)|\s+and\s+)/g,"")+" }\n";v[G]["breakpoints"][E]["mediaquery"]=I.match(/(.*?)(\{)/)[1]}else{g+=F+I+"\n";v[G]["breakpoints"][E]["mediaquery"]=""}}e=l.getAttribute("data-rwdimage-retina")==="true"?true:false;z=l.getAttribute("data-rwdimage-retina-suffix")?l.getAttribute("data-rwdimage-retina-suffix"):"@2x";if(e){g=g.replace(/(^@media\s*?)(\(.*?)\{(.*?\[.*?)(\..*?\))(.*)/gm,"$1$2$3$4$5\n$1$2 and (min--moz-device-pixel-ratio: 1.3), $2 and (-o-min-device-pixel-ratio: 2.6/2), $2 and (-webkit-min-device-pixel-ratio: 1.3), $2 and (min-device-pixel-ratio: 1.3), $2 and (min-resolution: 1.3dppx) {$3"+z+"$4$5").replace(/(^\[.*?)(\..*?\))(.*)/gm,"$1$2$3\n@media (min--moz-device-pixel-ratio: 1.3), (-o-min-device-pixel-ratio: 2.6/2), (-webkit-min-device-pixel-ratio: 1.3), (min-device-pixel-ratio: 1.3), (min-resolution: 1.3dppx) { $1"+z+"$2$3 }")}k=l.getAttribute("data-rwdimage-fallback");if(k){h=l.getAttribute("data-rwdimage-fallback-class")?l.getAttribute("data-rwdimage-fallback-class"):"ltie9";g+="."+h+" "+F+d(k)+"\n";if(!b){b=C.documentElement.className.match(h)}}if(l.getAttribute("data-rwdimage-lazy-load")==="true"){g=g.replace(/(\[data-rwdimage-id="\d+"\])/g,".lazy-loaded$1")}w+=g}w=".rwdimage { background-repeat: no-repeat; background-size: contain; height: 0; width: 100%; }\n"+w+'.rwdimage[data-rwdimage-has-enquire="true"] { height: auto; padding-bottom: 0; width: auto; }\n';H=C.createElement("style");H.type="text/css";if(H.styleSheet){H.styleSheet.cssText=w}else{H.appendChild(C.createTextNode(w))}C.getElementsByTagName("head")[0].appendChild(H);q.rwdImageChangeSrc=function(j){if(!j||j.tagName.toLowerCase()!=="img"){return}var i=!!q.getComputedStyle?q.getComputedStyle(j).getPropertyValue("background-image"):j.currentStyle.backgroundImage;i=i.replace(/url\((?:"|')?(.*?)(?:"|')?\)/,"$1");if(i!=="none"&&j.src!==i){j.src=i}};var p=function(i,j){if(!v[i]["breakpoints"][j]["mediaquery"]){return}enquire.register(v[i]["breakpoints"][j]["mediaquery"],function(){rwdImageChangeSrc(v[i]["elem"])})};for(s;s<v.length;s++){if(v[s]["isImage"]){if(!b&&a){for(var r=0;r<v[s]["breakpoints"].length;r++){p(s,r)}}else{rwdImageChangeSrc(v[s]["elem"])}}}})(window,document);