An open API service indexing awesome lists of open source software.

https://github.com/bradoyler/another-responsive-image-hack

An ultra-light (lib-free) responsive image technique ~works in IE8~
https://github.com/bradoyler/another-responsive-image-hack

Last synced: 18 days ago
JSON representation

An ultra-light (lib-free) responsive image technique ~works in IE8~

Awesome Lists containing this project

README

        

Yet another responsive image hack
=============================

An ultra-light responsive image technique that even works in IE8

```html

// minify and inline this after the targeted img tags
function adaptImages(viewportwidth, selector, breakpoints){
for(var i=0;i<breakpoints.length;i++) {
var startwidth = breakpoints[i], nextIdx = i+1, nextwidth = '99999';
if(breakpoints[nextIdx]) {
nextwidth = breakpoints[nextIdx];
}
if(viewportwidth>=startwidth && viewportwidth < nextwidth) {
var imgs = document.querySelectorAll(selector);
for(var j=0;j<imgs.length;j++) {
imgs[j].setAttribute('src',imgs[j].getAttribute('data-bp'+ i));
}
break;
}
}
};
// gets viewport dimensions
function viewport() {
var t=window,n="inner";
if(!("innerWidth"in window)) {
n="client";t=document.documentElement||document.body
}
return { width:t[n+"Width"], height:t[n+"Height"]};
};

// call adaptImages with screen width, img selector & breakpoints
adaptImages(viewport().width, '.js-adaptive-img', [0,768,992,1230]);
// u *could* attach this to window.onresize event

```
#### Thats it.
This was intended to be a performant technique for 'above the fold' responsive images.

#### [Demo](http://bradoyler.github.io/another-responsive-image-hack/)