├── .gitignore ├── README.md ├── demo ├── assets │ ├── css │ │ ├── style.css │ │ └── style.less │ ├── images │ │ ├── bg.png │ │ ├── blank.gif │ │ ├── close.png │ │ ├── favicon.ico │ │ ├── large.png │ │ ├── logo.png │ │ ├── medium.png │ │ └── small.png │ └── js │ │ └── modernizr.js └── index.html └── js ├── jquery-picture-min.js └── jquery-picture.js /.gitignore: -------------------------------------------------------------------------------- 1 | *.DS_Store 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | jQuery-Picture 2 | ============== 3 | 4 | jQuery plugin to handle responsive images. 5 | 6 | Read the guide here. -------------------------------------------------------------------------------- /demo/assets/css/style.css: -------------------------------------------------------------------------------- 1 | /* http://sonspring.com/journal/clearing-floats */ 2 | .clear { 3 | clear: both; 4 | display: block; 5 | overflow: hidden; 6 | visibility: hidden; 7 | width: 0; 8 | height: 0; 9 | } 10 | /* http://www.yuiblog.com/blog/2010/09/27/clearfix-reloaded-overflowhidden-demystified */ 11 | .clearfix:before, 12 | .clearfix:after { 13 | content: '\0020'; 14 | display: block; 15 | overflow: hidden; 16 | visibility: hidden; 17 | width: 0; 18 | height: 0; 19 | } 20 | .clearfix:after { 21 | clear: both; 22 | } 23 | /* 24 | The following zoom:1 rule is specifically for IE6 + IE7. 25 | Move to separate stylesheet if invalid CSS is a problem. 26 | */ 27 | .clearfix { 28 | zoom: 1; 29 | } 30 | /* ==================== */ 31 | /* = Default Stylings = */ 32 | /* ==================== */ 33 | body { 34 | color: #222; 35 | font: normal normal 400 100%/1.5em georgia, serif; 36 | margin: 3em auto; 37 | max-width: 40em; 38 | padding: 0 2em; 39 | text-shadow: 0 -1px 1px rgba(255, 255, 255, 0.01); 40 | -webkit-font-smoothing: antialiased; 41 | text-rendering: optimizeLegibility; 42 | } 43 | a:link { 44 | color: #3366CC; 45 | } 46 | a:visited { 47 | color: #224488; 48 | } 49 | h1, 50 | h2, 51 | h3, 52 | h4, 53 | h5, 54 | h6 { 55 | font-weight: 400; 56 | } 57 | h1 { 58 | font-size: 225%; 59 | line-height: 1.3334em; 60 | margin: 0 0 .1666em; 61 | } 62 | h2 { 63 | font-size: 175%; 64 | line-height: 1.28571429em; 65 | margin: 0 0 .35714286em; 66 | } 67 | h3 { 68 | font-size: 137.5%; 69 | line-height: 1.3636em; 70 | margin: 0 0 .5em; 71 | } 72 | h4, 73 | h5, 74 | h6 { 75 | font-size: 112.5%; 76 | line-height: 1.3334em; 77 | margin: 0 0 .7778em; 78 | } 79 | ol, 80 | ul { 81 | list-style-position: outside; 82 | padding: 0; 83 | } 84 | ol ol, 85 | ol ul, 86 | ul ol, 87 | ul ul { 88 | margin: 0 0 0.75em 2em; 89 | } 90 | blockquote, 91 | ol, 92 | p, 93 | ul { 94 | display: block; 95 | margin: 0 0 1.5em; 96 | } 97 | blockquote { 98 | border-left: solid .1em #E4E4E4; 99 | color: #919191; 100 | padding: 0 1.5em 0 1.4em; 101 | } 102 | code { 103 | font: normal normal 87.5%/1.71428571em monospace, sans-serif; 104 | background: #eee; 105 | } 106 | img { 107 | display: block; 108 | margin: 1.5em auto; 109 | } 110 | pre { 111 | font: normal normal 87.5%/1.71428571em monospace, sans-serif; 112 | padding: 0.857142855em 1.71428571em; 113 | background: #eee; 114 | display: block; 115 | overflow: auto; 116 | } 117 | table { 118 | border-collapse: collapse; 119 | margin: 1.5em 0; 120 | width: 100%; 121 | } 122 | td, 123 | th { 124 | border: solid .1em #eee; 125 | font-size: 87.5%; 126 | line-height: 1.71428571em; 127 | text-align: left; 128 | padding: 5px 10px; 129 | font-weight: normal; 130 | } 131 | th { 132 | background: #eee; 133 | border-right: 1px solid #ddd; 134 | } 135 | th:last-child { 136 | border-right: 1px solid #eee; 137 | } 138 | td.green { 139 | background: #a7b559; 140 | color: #fff; 141 | } 142 | td.red { 143 | background: #9d4b37; 144 | color: #fff; 145 | } 146 | figure { 147 | margin: 0; 148 | padding: 0; 149 | border: 0; 150 | outline: 0; 151 | font-size: 100%; 152 | vertical-align: baseline; 153 | background: transparent; 154 | } 155 | figure img { 156 | width: 100%; 157 | margin-bottom: 0; 158 | } 159 | picture img { 160 | width: 100%; 161 | } 162 | figcaption { 163 | margin: 0; 164 | padding: 0; 165 | border: 0; 166 | outline: 0; 167 | font-size: 100%; 168 | vertical-align: baseline; 169 | background: transparent; 170 | font-size: 12px; 171 | margin-bottom: 1.5em; 172 | } 173 | /* =================== */ 174 | /* = Custom Stylings = */ 175 | /* =================== */ 176 | header { 177 | padding-left: 130px; 178 | margin-bottom: 2em; 179 | background: url(../images/logo.png) top left no-repeat; 180 | } 181 | header p { 182 | color: #666; 183 | font-weight: normal; 184 | font-style: italic; 185 | font-size: 14px; 186 | } 187 | #buttons { 188 | margin: 0 -5% 1.5em; 189 | } 190 | #buttons a { 191 | display: block; 192 | background: #ddd; 193 | -webkit-border-radius: 3px; 194 | -moz-border-radius: 3px; 195 | -ms-border-radius: 3px; 196 | border-radius: 3px; 197 | text-align: center; 198 | line-height: 2.6em; 199 | width: 40%; 200 | margin: 0 5%; 201 | float: left; 202 | text-decoration: none; 203 | color: #111; 204 | } 205 | #buttons a:hover, 206 | #buttons a:focus { 207 | background: #3366CC; 208 | color: #fff; 209 | } 210 | #resize { 211 | position: fixed; 212 | right: 0; 213 | bottom: 0; 214 | background: url(../images/bg.png) bottom right no-repeat; 215 | height: 77px; 216 | width: 77px; 217 | } 218 | footer { 219 | font-size: small; 220 | border-top: 1px solid #ddd; 221 | padding: 1.5em 0; 222 | } 223 | footer p { 224 | float: left; 225 | } 226 | footer div { 227 | float: right; 228 | } 229 | .plug { 230 | background: #e1f0de; 231 | padding: 0.75em; 232 | margin-top: -1.5em; 233 | margin-bottom: 1.5em; 234 | position: relative; 235 | } 236 | .plug > a { 237 | display: block; 238 | position: absolute; 239 | top: 10px; 240 | right: 10px; 241 | height: 16px; 242 | width: 16px; 243 | background: url(../images/close.png); 244 | overflow: hidden; 245 | text-indent: 100%; 246 | } 247 | .plug p { 248 | margin-bottom: 0; 249 | } 250 | @media screen and (max-width: 600px) { 251 | #resize { 252 | display: none; 253 | } 254 | #buttons a { 255 | line-height: 2em; 256 | } 257 | } 258 | @media screen and (max-width: 440px) { 259 | header { 260 | background: transparent; 261 | padding-left: 0; 262 | } 263 | #resize { 264 | display: none; 265 | } 266 | footer div { 267 | clear: both; 268 | float: left; 269 | } 270 | } 271 | -------------------------------------------------------------------------------- /demo/assets/css/style.less: -------------------------------------------------------------------------------- 1 | /* http://sonspring.com/journal/clearing-floats */ 2 | 3 | .clear { 4 | clear: both; 5 | display: block; 6 | overflow: hidden; 7 | visibility: hidden; 8 | width: 0; 9 | height: 0; 10 | } 11 | 12 | /* http://www.yuiblog.com/blog/2010/09/27/clearfix-reloaded-overflowhidden-demystified */ 13 | 14 | .clearfix:before, 15 | .clearfix:after { 16 | content: '\0020'; 17 | display: block; 18 | overflow: hidden; 19 | visibility: hidden; 20 | width: 0; 21 | height: 0; 22 | } 23 | 24 | .clearfix:after { 25 | clear: both; 26 | } 27 | 28 | /* 29 | The following zoom:1 rule is specifically for IE6 + IE7. 30 | Move to separate stylesheet if invalid CSS is a problem. 31 | */ 32 | 33 | .clearfix { 34 | zoom: 1; 35 | } 36 | 37 | /* ==================== */ 38 | /* = Default Stylings = */ 39 | /* ==================== */ 40 | 41 | body{ 42 | color: #222; 43 | font: normal normal 400 100%/1.5em georgia,serif; 44 | margin: 3em auto; 45 | max-width: 40em; 46 | padding: 0 2em; 47 | text-shadow: 0 -1px 1px rgba(255, 255, 255, 0.01); 48 | -webkit-font-smoothing: antialiased; 49 | text-rendering: optimizeLegibility; 50 | } 51 | 52 | a:link{ color: #3366CC; } 53 | a:visited{ color: #224488; } 54 | 55 | h1,h2,h3,h4,h5,h6{ font-weight: 400 } 56 | 57 | h1{ 58 | font-size: 225%; 59 | line-height: 1.3334em; 60 | margin: 0 0 .1666em; 61 | } 62 | h2{ 63 | font-size: 175%; 64 | line-height: 1.28571429em; 65 | margin: 0 0 .35714286em; 66 | } 67 | h3{ 68 | font-size: 137.5%; 69 | line-height: 1.3636em; 70 | margin: 0 0 .5em; 71 | } 72 | h4,h5,h6{ 73 | font-size: 112.5%; 74 | line-height: 1.3334em; 75 | margin: 0 0 .7778em; 76 | } 77 | 78 | ol,ul{ 79 | list-style-position: outside; 80 | padding: 0; 81 | } 82 | ol ol,ol ul,ul ol,ul ul{ margin: 0 0 .75em 2em } 83 | 84 | blockquote,ol,p,ul{ 85 | display: block; 86 | margin: 0 0 1.5em; 87 | } 88 | blockquote{ 89 | border-left: solid .1em #E4E4E4; 90 | color: #919191; 91 | padding: 0 1.5em 0 1.4em; 92 | } 93 | code{ 94 | font: normal normal 87.5%/1.71428571em monospace,sans-serif; 95 | background: #eee; 96 | } 97 | img { 98 | display: block; 99 | margin: 1.5em auto; 100 | } 101 | pre{ 102 | display: block; 103 | font: normal normal 87.5%/1.71428571em monospace,sans-serif; 104 | padding: 1.71428571/2em 1.71428571em; 105 | background: #eee; 106 | display:block; 107 | overflow:auto; 108 | } 109 | table{ 110 | border-collapse: collapse; 111 | margin: 1.5em 0; 112 | width: 100% 113 | } 114 | td,th{ 115 | border: solid .1em #eee; 116 | font-size: 87.5%; 117 | line-height: 1.71428571em; 118 | text-align: left; 119 | padding: 5px 10px; 120 | font-weight:normal; 121 | } 122 | th{ 123 | background: #eee; 124 | border-right: 1px solid #ddd; 125 | } 126 | th:last-child{ 127 | border-right: 1px solid #eee; 128 | } 129 | 130 | td.green{ 131 | background:#a7b559; 132 | color:#fff; 133 | } 134 | 135 | td.red{ 136 | background:#9d4b37; 137 | color:#fff; 138 | } 139 | 140 | figure{ 141 | margin:0; 142 | padding:0; 143 | border:0; 144 | outline:0; 145 | font-size:100%; 146 | vertical-align:baseline; 147 | background:transparent; 148 | img{ 149 | width: 100%; 150 | margin-bottom:0; 151 | } 152 | } 153 | 154 | picture{ 155 | img{ 156 | width: 100%; 157 | } 158 | } 159 | 160 | figcaption{ 161 | margin:0; 162 | padding:0; 163 | border:0; 164 | outline:0; 165 | font-size:100%; 166 | vertical-align:baseline; 167 | background:transparent; 168 | font-size: 12px; 169 | margin-bottom: 1.5em; 170 | } 171 | 172 | /* =================== */ 173 | /* = Custom Stylings = */ 174 | /* =================== */ 175 | 176 | header{ 177 | padding-left: 130px; 178 | margin-bottom: 2em; 179 | background: url(../images/logo.png) top left no-repeat; 180 | p{ 181 | color:#666; 182 | font-weight:normal; 183 | font-style:italic; 184 | font-size: 14px; 185 | } 186 | } 187 | 188 | #buttons{ 189 | margin: 0 -5% 1.5em; 190 | a{ 191 | display:block; 192 | background:#ddd; 193 | -webkit-border-radius: 3px; 194 | -moz-border-radius: 3px; 195 | -ms-border-radius: 3px; 196 | border-radius: 3px; 197 | text-align:center; 198 | line-height: 2.6em; 199 | width: 40%; 200 | margin: 0 5%; 201 | float:left; 202 | text-decoration:none; 203 | color:#111; 204 | &:hover, &:focus{ 205 | background: #3366CC; 206 | color:#fff; 207 | } 208 | } 209 | } 210 | 211 | #resize{ 212 | position: fixed; 213 | right:0; 214 | bottom:0; 215 | background: url(../images/bg.png) bottom right no-repeat; 216 | height: 77px; 217 | width: 77px; 218 | } 219 | 220 | footer{ 221 | font-size:small; 222 | border-top: 1px solid #ddd; 223 | padding: 1.5em 0; 224 | p{ 225 | float:left; 226 | } 227 | div{ 228 | float:right; 229 | } 230 | } 231 | 232 | .plug{ 233 | background: #e1f0de; 234 | padding: 0.75em; 235 | margin-top: -1.5em; 236 | margin-bottom: 1.5em; 237 | position: relative; 238 | >a{ 239 | display:block; 240 | position: absolute; 241 | top:10px; 242 | right: 10px; 243 | height: 16px; 244 | width: 16px; 245 | background: url(../images/close.png); 246 | overflow:hidden; 247 | text-indent: 100%; 248 | } 249 | p{ 250 | margin-bottom:0; 251 | } 252 | } 253 | 254 | @media screen and (max-width: 600px) { 255 | #resize{ 256 | display:none; 257 | } 258 | #buttons{ 259 | a{ 260 | line-height: 2em; 261 | } 262 | } 263 | } 264 | 265 | @media screen and (max-width: 440px) { 266 | header{ 267 | background: transparent; 268 | padding-left:0; 269 | } 270 | #resize{ 271 | display:none; 272 | } 273 | footer{ 274 | div{ 275 | clear:both; 276 | float:left; 277 | } 278 | } 279 | } -------------------------------------------------------------------------------- /demo/assets/images/bg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Abban/jQuery-Picture/ca37fbc229ab02544fc978399a8d36b6eff21205/demo/assets/images/bg.png -------------------------------------------------------------------------------- /demo/assets/images/blank.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Abban/jQuery-Picture/ca37fbc229ab02544fc978399a8d36b6eff21205/demo/assets/images/blank.gif -------------------------------------------------------------------------------- /demo/assets/images/close.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Abban/jQuery-Picture/ca37fbc229ab02544fc978399a8d36b6eff21205/demo/assets/images/close.png -------------------------------------------------------------------------------- /demo/assets/images/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Abban/jQuery-Picture/ca37fbc229ab02544fc978399a8d36b6eff21205/demo/assets/images/favicon.ico -------------------------------------------------------------------------------- /demo/assets/images/large.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Abban/jQuery-Picture/ca37fbc229ab02544fc978399a8d36b6eff21205/demo/assets/images/large.png -------------------------------------------------------------------------------- /demo/assets/images/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Abban/jQuery-Picture/ca37fbc229ab02544fc978399a8d36b6eff21205/demo/assets/images/logo.png -------------------------------------------------------------------------------- /demo/assets/images/medium.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Abban/jQuery-Picture/ca37fbc229ab02544fc978399a8d36b6eff21205/demo/assets/images/medium.png -------------------------------------------------------------------------------- /demo/assets/images/small.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Abban/jQuery-Picture/ca37fbc229ab02544fc978399a8d36b6eff21205/demo/assets/images/small.png -------------------------------------------------------------------------------- /demo/assets/js/modernizr.js: -------------------------------------------------------------------------------- 1 | /* Modernizr 2.5.3 (Custom Build) | MIT & BSD 2 | * Build: http://www.modernizr.com/download/#-shiv-mq-cssclasses-teststyles-load 3 | */ 4 | ;window.Modernizr=function(a,b,c){function w(a){j.cssText=a}function x(a,b){return w(prefixes.join(a+";")+(b||""))}function y(a,b){return typeof a===b}function z(a,b){return!!~(""+a).indexOf(b)}function A(a,b,d){for(var e in a){var f=b[a[e]];if(f!==c)return d===!1?a[e]:y(f,"function")?f.bind(d||b):f}return!1}var d="2.5.3",e={},f=!0,g=b.documentElement,h="modernizr",i=b.createElement(h),j=i.style,k,l={}.toString,m={},n={},o={},p=[],q=p.slice,r,s=function(a,c,d,e){var f,i,j,k=b.createElement("div"),l=b.body,m=l?l:b.createElement("body");if(parseInt(d,10))while(d--)j=b.createElement("div"),j.id=e?e[d]:h+(d+1),k.appendChild(j);return f=["­",""].join(""),k.id=h,(l?k:m).innerHTML+=f,m.appendChild(k),l||(m.style.background="",g.appendChild(m)),i=c(k,a),l?k.parentNode.removeChild(k):m.parentNode.removeChild(m),!!i},t=function(b){var c=a.matchMedia||a.msMatchMedia;if(c)return c(b).matches;var d;return s("@media "+b+" { #"+h+" { position: absolute; } }",function(b){d=(a.getComputedStyle?getComputedStyle(b,null):b.currentStyle)["position"]=="absolute"}),d},u={}.hasOwnProperty,v;!y(u,"undefined")&&!y(u.call,"undefined")?v=function(a,b){return u.call(a,b)}:v=function(a,b){return b in a&&y(a.constructor.prototype[b],"undefined")},Function.prototype.bind||(Function.prototype.bind=function(b){var c=this;if(typeof c!="function")throw new TypeError;var d=q.call(arguments,1),e=function(){if(this instanceof e){var a=function(){};a.prototype=c.prototype;var f=new a,g=c.apply(f,d.concat(q.call(arguments)));return Object(g)===g?g:f}return c.apply(b,d.concat(q.call(arguments)))};return e});for(var B in m)v(m,B)&&(r=B.toLowerCase(),e[r]=m[B](),p.push((e[r]?"":"no-")+r));return w(""),i=k=null,function(a,b){function g(a,b){var c=a.createElement("p"),d=a.getElementsByTagName("head")[0]||a.documentElement;return c.innerHTML="x",d.insertBefore(c.lastChild,d.firstChild)}function h(){var a=k.elements;return typeof a=="string"?a.split(" "):a}function i(a){var b={},c=a.createElement,e=a.createDocumentFragment,f=e();a.createElement=function(a){var e=(b[a]||(b[a]=c(a))).cloneNode();return k.shivMethods&&e.canHaveChildren&&!d.test(a)?f.appendChild(e):e},a.createDocumentFragment=Function("h,f","return function(){var n=f.cloneNode(),c=n.createElement;h.shivMethods&&("+h().join().replace(/\w+/g,function(a){return b[a]=c(a),f.createElement(a),'c("'+a+'")'})+");return n}")(k,f)}function j(a){var b;return a.documentShived?a:(k.shivCSS&&!e&&(b=!!g(a,"article,aside,details,figcaption,figure,footer,header,hgroup,nav,section{display:block}audio{display:none}canvas,video{display:inline-block;*display:inline;*zoom:1}[hidden]{display:none}audio[controls]{display:inline-block;*display:inline;*zoom:1}mark{background:#FF0;color:#000}")),f||(b=!i(a)),b&&(a.documentShived=b),a)}var c=a.html5||{},d=/^<|^(?:button|form|map|select|textarea)$/i,e,f;(function(){var a=b.createElement("a");a.innerHTML="",e="hidden"in a,f=a.childNodes.length==1||function(){try{b.createElement("a")}catch(a){return!0}var c=b.createDocumentFragment();return typeof c.cloneNode=="undefined"||typeof c.createDocumentFragment=="undefined"||typeof c.createElement=="undefined"}()})();var k={elements:c.elements||"abbr article aside audio bdi canvas data datalist details figcaption figure footer header hgroup mark meter nav output progress section summary time video",shivCSS:c.shivCSS!==!1,shivMethods:c.shivMethods!==!1,type:"default",shivDocument:j};a.html5=k,j(b)}(this,b),e._version=d,e.mq=t,e.testStyles=s,g.className=g.className.replace(/(^|\s)no-js(\s|$)/,"$1$2")+(f?" js "+p.join(" "):""),e}(this,this.document),function(a,b,c){function d(a){return o.call(a)=="[object Function]"}function e(a){return typeof a=="string"}function f(){}function g(a){return!a||a=="loaded"||a=="complete"||a=="uninitialized"}function h(){var a=p.shift();q=1,a?a.t?m(function(){(a.t=="c"?B.injectCss:B.injectJs)(a.s,0,a.a,a.x,a.e,1)},0):(a(),h()):q=0}function i(a,c,d,e,f,i,j){function k(b){if(!o&&g(l.readyState)&&(u.r=o=1,!q&&h(),l.onload=l.onreadystatechange=null,b)){a!="img"&&m(function(){t.removeChild(l)},50);for(var d in y[c])y[c].hasOwnProperty(d)&&y[c][d].onload()}}var j=j||B.errorTimeout,l={},o=0,r=0,u={t:d,s:c,e:f,a:i,x:j};y[c]===1&&(r=1,y[c]=[],l=b.createElement(a)),a=="object"?l.data=c:(l.src=c,l.type=a),l.width=l.height="0",l.onerror=l.onload=l.onreadystatechange=function(){k.call(this,r)},p.splice(e,0,u),a!="img"&&(r||y[c]===2?(t.insertBefore(l,s?null:n),m(k,j)):y[c].push(l))}function j(a,b,c,d,f){return q=0,b=b||"j",e(a)?i(b=="c"?v:u,a,b,this.i++,c,d,f):(p.splice(this.i++,0,a),p.length==1&&h()),this}function k(){var a=B;return a.loader={load:j,i:0},a}var l=b.documentElement,m=a.setTimeout,n=b.getElementsByTagName("script")[0],o={}.toString,p=[],q=0,r="MozAppearance"in l.style,s=r&&!!b.createRange().compareNode,t=s?l:n.parentNode,l=a.opera&&o.call(a.opera)=="[object Opera]",l=!!b.attachEvent&&!l,u=r?"object":l?"script":"img",v=l?"script":u,w=Array.isArray||function(a){return o.call(a)=="[object Array]"},x=[],y={},z={timeout:function(a,b){return b.length&&(a.timeout=b[0]),a}},A,B;B=function(a){function b(a){var a=a.split("!"),b=x.length,c=a.pop(),d=a.length,c={url:c,origUrl:c,prefixes:a},e,f,g;for(f=0;f 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | A jQuery plugin to help ease the transition to responsive images | Jquery Picture 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 36 | 37 | 38 | 39 | 40 | 41 | 42 |
43 | 44 | 49 | 50 |
51 |

jQuery Picture

52 |

A jQuery plugin to help ease the transition to responsive images

53 |
54 | 55 |
56 | 57 | Download 58 | 59 | Github Page 60 | 61 |
62 | 63 |

Overview

64 | 65 |

jQuery Picture is a tiny (2kb) plugin to add support for responsive images to your layouts. It supports both figure elements with some custom data attributes and the new proposed picture format. This plugin will be made redundant when the format is approved and implemented by browsers. Lets hope that happens soon but in the meantime this plugin will be kept up to date with latest developments.

66 | 67 |
68 | 69 | 70 | 73 | 74 | 75 |
76 | 77 | Fig 1. Drag your browser window in an out to see the image source change. 78 | 79 |
80 | 81 |
82 | 83 |

Usage

84 | 85 |

You have a choice of two ways to use the plugin, with <figure> tags or with the newly proposed <picture> and <source> tags. Bear in mind that the picture and source tags have only recently been proposed and are not yet valid HTML code.

86 | 87 |

Initialise the plugin

88 | 89 |

To initialise it you just add .picture() to the element you want to apply it to. It only works on figure and picture tags for now:

90 | 91 |
 92 | $(function(){
 93 |     $('figure.responsive').picture();
 94 | });
 95 | 
96 | 97 |

Usage with Figures

98 | 99 |

To use the plugin with your figure tags you need to add data attributes to them for each size of the image you want to use. It's a good idea to add a class to the tag too so it doesn't select every figure on the page. Heres a code example:

100 | 101 |
<figure class="responsive" data-media="assets/images/small.png" data-media440="assets/images/medium.png" data-media600="assets/images/large.png" title="A Half Brained Idea">
102 |     <noscript>
103 |         <img src="assets/images/large.png" alt="A Half Brained Idea">
104 |     </noscript>
105 | </figure>
106 | 107 |

You can see that there are data attributes added to the figure tag that hold the url of the different size images. In this example the attribute with no number is for the 0-400 area. Each one also specifies the break point that image is to be used at. If javascript is turned off it defaults to the image specified in the noscript tag. You initialise the plugin like this:

108 | 109 |
110 | $(function(){
111 |     $('figure.responsive').picture();
112 | });
113 | 
114 | 115 |

Usage with Pictures

116 | 117 |

Using the plugin with the new picture tag works in a similar way. Instead of the images being declared in data attributes they're declared using <source> tags:

118 | 119 |
120 | <picture alt="A Half Brained Idea">
121 |     <source src="assets/images/small.png">
122 |     <source src="assets/images/medium.png" media="(min-width:440px)">
123 |     <source src="assets/images/large.png" media="(min-width:600px)">
124 |     <noscript>
125 |         <img src="assets/images/large.png" alt="A Half Brained Idea">
126 |     </noscript>
127 | </picture>
128 | 
129 | 130 |

And then it's initialised in a similar way:

131 | 132 |
133 | $(function(){
134 |     $('picture').picture();
135 | });
136 | 
137 | 138 |

Using images with links

139 | 140 |

In both use cases if you wrap a link around the noscript tag it will insert the image inside it:

141 | 142 |
<figure class="responsive" data-media="assets/images/small.png" data-media440="assets/images/medium.png" data-media600="assets/images/large.png" title="A Half Brained Idea">
143 |     <a href="http://abandon.ie">
144 |         <noscript>
145 |             <img src="assets/images/large.png" alt="A Half Brained Idea">
146 |         </noscript>
147 |      </a>
148 | </figure>
149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 |
BrowserStatusDescription
ChromeWorkingLooks grand so it does.
SafariWorkingAlso looks grand.
Mobile SafariWorkingLooks grand on iPhone, yet to test on the iPad.
Firefox 10 OSXWorkingOh so grand.
Firefox 6 OSXWorkingTesting on a random old FF version.
IE9WorkingI thought it wasn't gonna work but it does.
IE7/8WorkingIE7 and 8 don't support media queries. They still load the appropriate images however as you can see in this demo. If you want to force media queries on these browsers however you can try Respond.
239 | 240 |

Plugins & Stuff

241 | 242 |

I'm going to list plugins and themes that use the script here. If you want your one added please drop me a line on Twitter.

243 | 244 | 247 | 248 | 256 | 257 | 258 | 259 | 260 | -------------------------------------------------------------------------------- /js/jquery-picture-min.js: -------------------------------------------------------------------------------- 1 | /** 2 | * jQuery Picture 3 | * http://jquerypicture.com 4 | * http://github.com/Abban/jQuery-Picture 5 | * 6 | * May 2012 7 | * 8 | * @version 0.9 9 | * @author Abban Dunne http://abandon.ie 10 | * @license MIT 11 | * 12 | * jQuery Picture is a plugin to add support for responsive images to your layouts. 13 | * It supports both figure elements with some custom data attributes and the new 14 | * proposed picture format. This plugin will be made redundant when the format is 15 | * approved and implemented by browsers. Lets hope that happens soon. In the meantime 16 | * this plugin will be kept up to date with latest developments. 17 | * 18 | */(function(e){e.fn.picture=function(t){var n={container:null,ignorePixelRatio:!1,useLarger:!1,insertElement:">a",inlineDimensions:!1},r=e.extend({},n,t);this.each(function(){function a(o){if(o){if(s.get(0).tagName.toLowerCase()=="figure"){var a=s.data();e.each(a,function(e){var n;n=e.replace(/[^\d.]/g,"");n&&t.push(parseInt(n))})}else s.find("source").each(function(){var n,r;n=e(this).attr("media");if(n){r=n.replace(/[^\d.]/g,"");t.push(parseInt(r))}});t.sort(function(e,t){return e-t})}var c=0;r.container==null?n=e(window).width()*u:n=e(r.container).width()*u;e.each(t,function(e,t){parseInt(n)>=parseInt(t)&&parseInt(c)<=parseInt(t)&&(c=t)});if(r.useLarger){idx=t.indexOf(c);idx").attr("src",e("img",s).attr("src")).load(function(){e("img",s).attr("height",this.height);e("img",s).attr("width",this.width)})}function l(){var t=new Object,n=s.data();e.each(n,function(e,n){var r;r=e.replace(/[^\d.]/g,"");r||(r=0);t[r]=n});if(s.find("img").length==0){var o='").attr("src",e("img",s).attr("src")).load(function(){e("img",s).attr("height",this.height);e("img",s).attr("width",this.width)})}var t=new Array,n,i,s,o,u=1;!r.ignorePixelRatio&&window.devicePixelRatio!==undefined&&(u=window.devicePixelRatio);s=e(this);s.find("noscript").remove();a(!0);o=!1;e(window).resize(function(){o!==!1&&clearTimeout(o);o=setTimeout(a,200)})})}})(jQuery); -------------------------------------------------------------------------------- /js/jquery-picture.js: -------------------------------------------------------------------------------- 1 | /** 2 | * jQuery Picture 3 | * http://jquerypicture.com 4 | * http://github.com/Abban/jQuery-Picture 5 | * 6 | * May 2012 7 | * 8 | * @version 0.9 9 | * @author Abban Dunne http://abandon.ie 10 | * @license MIT 11 | * 12 | * jQuery Picture is a plugin to add support for responsive images to your layouts. 13 | * It supports both figure elements with some custom data attributes and the new 14 | * proposed picture format. This plugin will be made redundant when the format is 15 | * approved and implemented by browsers. Lets hope that happens soon. In the meantime 16 | * this plugin will be kept up to date with latest developments. 17 | * 18 | */ 19 | (function($){ 20 | 21 | $.fn.picture = function(args){ 22 | 23 | var defaults = { 24 | 25 | container : null, 26 | ignorePixelRatio: false, 27 | useLarger: false, 28 | insertElement: '>a', 29 | inlineDimensions: false 30 | 31 | }; 32 | 33 | var settings = $.extend({}, defaults, args); 34 | 35 | this.each(function(){ 36 | 37 | var breakpoints = new Array(); 38 | 39 | var windowWidth, currentMedia, element, timeoutOffset; 40 | 41 | // Check the device pixel ratio 42 | var PixelRatio = 1; 43 | if(!settings.ignorePixelRatio && window.devicePixelRatio !== undefined) PixelRatio = window.devicePixelRatio; 44 | 45 | // Save off the element so it can be easily used inside a function 46 | element = $(this); 47 | 48 | //Delete the noscript we don't need it now anyway 49 | element.find('noscript').remove(); 50 | 51 | // Initialise the images 52 | getCurrentMedia(true); 53 | 54 | // Only call the image resize function 200ms after window stops being resized 55 | timeoutOffset = false; 56 | 57 | $(window).resize(function(){ 58 | 59 | if(timeoutOffset !== false) 60 | clearTimeout(timeoutOffset); 61 | 62 | timeoutOffset = setTimeout(getCurrentMedia, 200); 63 | 64 | }); 65 | 66 | 67 | /** 68 | * getCurrentMedia 69 | * 70 | * Checks the window width off the media query types and selects the current one. 71 | * Calls the setPicture or setFigure function to set the image. 72 | * 73 | */ 74 | function getCurrentMedia(init){ 75 | 76 | if(init){ 77 | 78 | if(element.get(0).tagName.toLowerCase() == 'figure'){ 79 | 80 | var mediaObj = element.data(); 81 | 82 | $.each(mediaObj, function(media){ 83 | 84 | var num; 85 | 86 | num = media.replace(/[^\d.]/g, ''); 87 | 88 | if(num) 89 | breakpoints.push(parseInt(num)); 90 | 91 | }); 92 | 93 | }else{ 94 | 95 | element.find('source').each(function(){ 96 | 97 | var media, num; 98 | 99 | media = $(this).attr('media'); 100 | 101 | if(media){ 102 | 103 | num = media.replace(/[^\d.]/g, ''); 104 | 105 | breakpoints.push(parseInt(num)); 106 | } 107 | 108 | }); 109 | 110 | } 111 | breakpoints.sort(function(a,b){return a - b}); //make sure the largest breakpoint is the last 112 | 113 | } 114 | 115 | var c = 0; 116 | 117 | // Check if user defined container, otherwise take window 118 | if (settings.container == null){ 119 | 120 | windowWidth = ($(window).width()) * PixelRatio; 121 | 122 | }else{ 123 | 124 | windowWidth = ($(settings.container).width()) * PixelRatio; 125 | 126 | } 127 | 128 | // Set the c variable to the current media width 129 | $.each(breakpoints, function(i,v){ 130 | 131 | if(parseInt(windowWidth) >= parseInt(v) && parseInt(c) <= parseInt(v)) 132 | c = v; 133 | 134 | }); 135 | 136 | if (settings.useLarger ){ 137 | idx = breakpoints.indexOf(c); 138 | if (idx < breakpoints.length-1) //make sure we're not already using the largest breakpoint 139 | c = breakpoints[ idx + 1]; 140 | } 141 | 142 | if(currentMedia !== c){ 143 | currentMedia = c; 144 | 145 | if(element.get(0).tagName.toLowerCase() == 'figure') 146 | setFigure(); 147 | else 148 | setPicture(); 149 | } 150 | 151 | } 152 | 153 | 154 | /** 155 | * setPicture 156 | * 157 | * Pulls the image src and media attributes from the source tags and sets 158 | * the src of the enclosed img tag to the appropriate one. 159 | * 160 | */ 161 | function setPicture(){ 162 | 163 | var sizes = new Object(); 164 | 165 | element.find('source').each(function(){ 166 | 167 | var media, path, num; 168 | media = $(this).attr('media'); 169 | path = $(this).attr('src'); 170 | 171 | if(media) 172 | num = media.replace(/[^\d.]/g, ''); 173 | else 174 | num = 0; 175 | 176 | sizes[num] = path; 177 | 178 | }); 179 | 180 | if(element.find('img').length == 0){ 181 | 182 | var prep = '").attr("src", $('img', element).attr("src")).load(function(){ 206 | $('img', element).attr('height', this.height); 207 | $('img', element).attr('width', this.width); 208 | }); 209 | 210 | } 211 | 212 | } 213 | 214 | 215 | /** 216 | * setFigure 217 | * 218 | * Pulls the image src and and media values from the data attributes 219 | * and sets the src of the enclosed img tag to the appropriate one. 220 | * 221 | */ 222 | function setFigure(){ 223 | 224 | var sizes = new Object(); 225 | 226 | var mediaObj = element.data(); 227 | 228 | $.each(mediaObj, function(media, path){ 229 | 230 | var num; 231 | 232 | num = media.replace(/[^\d.]/g, ''); 233 | 234 | if(!num) 235 | num = 0; 236 | 237 | sizes[num] = path; 238 | 239 | }); 240 | 241 | if(element.find('img').length == 0){ 242 | 243 | var prep = '").attr("src", $('img', element).attr("src")).load(function(){ 267 | $('img', element).attr('height', this.height); 268 | $('img', element).attr('width', this.width); 269 | }); 270 | 271 | } 272 | 273 | } 274 | 275 | }); 276 | 277 | }; 278 | 279 | })(jQuery); --------------------------------------------------------------------------------