Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/abernier/srcset-polyfill


https://github.com/abernier/srcset-polyfill

Last synced: 3 months ago
JSON representation

Awesome Lists containing this project

README

        

# A spec-compatible, unit-tested polyfill for ``

See [the specification][spec] for the reference algorithm.

## INSTALL

with NPM:

```sh
npm install https://github.com/abernier/srcset-polyfill/archive/master.tar.gz
```

or with a plain old `` tag:

```html
<script src="https://cdn.rawgit.com/abernier/srcset-polyfill/master/build/srcset.js">
```

NB: minified version here https://cdn.rawgit.com/abernier/srcset-polyfill/master/build/srcset.min.js

## Usage

Use the `data-srcset` attribute of `` elements. For example:

```html

```

or more advanced one:

```html

```

Include `build/srcset.min.js` in your page. Then, you'll have a `srcset` object with the following API :

- `srcset.update()` -- update all images in the page
- `srcset.imgs.get().update()` -- update one image

A `'srcchange'` event will also be triggered when the `src` of an image changes :

```javascript
myimg.addEventListener('srcchanged', function (data) {
console.log("img with previous src %s was changed to %s", data.previous, data.actual)
}, false);
```

## Open questions

- How to reliably check for srcset support in the browser (so as to not
attempt to polyfill if it's not necessary?)
- Is it safe to use `-webkit-transform` to scale things?
- Is it worth falling back to `-webkit-image-set` if available?

## Using srcset-polyfill to reduce bandwidth for mobile devices

If you are wanting to serve smaller images to mobile devices **to reduce
bandwidth** it is important to set your syntax correctly to [avoid downloading
the mobile optimised image and the original (larger) image][issue11]. The
correct syntax to use is:

> ``

#### Notes

* Include the smallest image in the `src` attribute (in the above example:
`small.jpg`).
* Include the smallest image and its associated **max** viewport width in the
`srcset` attribute (in the above example: `small.jpg 320w`).
* Include any other, wider viewport widths in the `srcset` attribute (in the
above example: `medium.jpg 960w`).
* Include the full size image in the `srcset` attribute, without any viewport
width restriction (in the above example; `large.jpg`).

[spec]: http://www.whatwg.org/specs/web-apps/current-work/multipage/embedded-content-1.html#processing-the-image-candidates
[issue11]: https://github.com/borismus/srcset-polyfill/issues/11