Coder Social home page Coder Social logo

yass.js's Introduction

yass.js

Yet Another SrcSet implementation.

How to use it

Add yass.js just after the last img tag on your page. Yass works from a mobile-first perspective, so the properties are seen as a minimal value. So 500w should be visible on a 500px or wider screen.

The order of the queries is not important, these are sorted by Yass. To force the update of the (including new) images, just call YASS() in your code.

Simple img tag with srcset

This may cause 2 requests for a image, when it matches one in the srcset.

<img src="small.png"
  data-srcset="medium.png 500w, [email protected] 2x, large.png 1000w, [email protected] 1000w 2x">

Img tag with noscript fallback

Now only the image that matches the device/viewport is loaded, and non-js users (also spiders) will get the fallback between the noscript tags. With a library like Modernizr you can hide the images for non-js users.

<img data-srcset="small.png, medium.png 500w, [email protected] 2x, large.png 1000w, [email protected] 1000w 2x">
<noscript><img src="medium.png"></noscript>
.no-js img[data-srcset] { display: none; }

API

YASS([obj], [options], [callback]) One method with multiple functions. When called without arguments it updates all current instances of Yass, and searches and updates new images. The return value will be all YassSrcSet instances.

You can pass an object that will be updated. This must be a valid DOM object. You can pass options (see below), and a callback which is called each time the src will be changed. The return value will be the created instance.

Options

You can set these properties for all instances, by setting these on window.YASS_OPTIONS before including yass.js.

ready_class: 'yass-ready'

ClassName to be set on the element when it has been Yassified

src_attr: 'src'

Property to set. This can be anything, most times src, but can also be poster etc..

srcset_attr: 'srcset'

Property to read the candidates from

CSS trick

For slightly better user experience you can add some css to hide the images with a srcset onload. Yass gives the img the class yass-ready when loaded. So with a simple line of css you can hide the srcset images:

img[data-srcset] { visibility: hidden; }
img.yass-ready { visibility: visible; }

Check the /demo directory for a sample implementation.

Notes

Yass.js is small, under 500bytes when minified and gzipped...

Tested in Chrome 28, Android 4.2, IOS6, BlackBerry10 and IE6. Should work on most/all browsers.

yass.js's People

Contributors

jtangelder avatar

Stargazers

Rıfat Caner Anıltürk avatar Mohammad Bagher Ehtemam avatar TinyBear avatar Raphaël Améaume avatar Stéphane Guigné avatar  avatar Jase Su avatar Chris Yip avatar Uros Urosevic avatar  avatar Vitaliy avatar Ellen Gummesson avatar Tomi avatar Michal Šimon avatar Jakob Grannas avatar Christian Oliff avatar xianlaioy avatar Raúl avatar Islam Magdy avatar Nico Rohrbach avatar Drew avatar Viktor Fröberg avatar TheCreation avatar Steve Hanna avatar Yanik Magnan avatar Karol Falkiewicz avatar Chris Smith avatar Malte Wessel avatar Jose De Gouveia avatar  avatar Matthieu Holzer avatar Alexey Okhrimenko avatar Jupiter St John avatar Flip avatar Steffen Gransow avatar Eric Novins avatar Stuart Trann avatar Dan Bennett avatar Jon de M avatar Ivan Zazueta avatar Immi avatar  avatar Evan Travers avatar Rodrigo Nascimento avatar  avatar hasan sabah avatar  avatar  avatar Ara L Abcarians avatar Tariq Khairallah avatar Shreef avatar Sean Goresht avatar Jaroslav Tesarik avatar Erick Patrick avatar Andrew-David J. avatar Andrey Matsovkin avatar Dmitry Malkov avatar Rostyslav Diachok avatar Alexander Khlebnikov avatar Alexey Volkov avatar Vadym Okun avatar Piotr Kapera avatar Anselm Hannemann avatar  avatar Ronald Lokers avatar Nikita Koksharov avatar Georgy avatar Jared Fox avatar Michael Puckett avatar April Johnson avatar Gabriel Baciu avatar Oscar Brito avatar Zachary avatar Emanuel Kluge avatar Kory Mathis avatar  avatar Tim Spann avatar Goom JM avatar  avatar Krister Kari avatar Florent Bourgeois avatar youxiachai avatar Richard Butler avatar Jidé avatar Vlad Alive avatar Leland Riordan avatar Sascha avatar Henrik Westphal avatar Jordan Sitkin avatar Guido avatar Felix Lau avatar Lane Allen avatar Joohun, Maeng avatar Chee Aun avatar Benjamin Turner avatar Allen Bargi avatar Chad Smith avatar Félix Girault avatar Umar Hansa avatar Aleksandar Simovic avatar

Watchers

Vladislav Shkodin avatar  avatar Arjen avatar Blake. avatar Tariq Khairallah avatar Michal Šimon avatar James Cloos avatar Krister Kari avatar  avatar  avatar Hsuching avatar Matthijs Brouwer avatar Dennis Passway avatar Daniel Spronk avatar Charlotte avatar  avatar  avatar

Forkers

val9 wheat1117

yass.js's Issues

Should update to match the new spec

It looks like this polyfill implements the old spec for srcset, where 'w' descriptors meant viewport max-width. The current spec makes 'w' descriptors mean width of the image, to be used together with the sizes attribute. The functionality for the old 'w' is available by using the picture element.

Doesn't export anything

At the momemnt this is an unusable npm module as it doesn't export anything. Would love it if it did export yass() so I could easily use it with Browserify.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.