Auto Resize Picture - jQuery Picture
Overview
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.
Usage
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.
Initialise the plugin
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:
$(function(){ $('figure.responsive').picture(); });
Usage with Figures
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:
<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"> <noscript> <img src="assets/images/large.png" alt="A Half Brained Idea"> </noscript> </figure>
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:
$(function(){ $('figure.responsive').picture(); });
Usage with Pictures
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:
<picture alt="A Half Brained Idea"> <source src="assets/images/small.png"> <source src="assets/images/medium.png" media="(min-width:440px)"> <source src="assets/images/large.png" media="(min-width:600px)"> <noscript> <img src="assets/images/large.png" alt="A Half Brained Idea"> </noscript> </picture>
And then it's initialised in a similar way:
$(function(){ $('picture').picture(); });
Using images with links
In both use cases if you wrap a link around the noscript tag it will insert the image inside it:
<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"> <a href="http://abandon.ie"> <noscript> <img src="assets/images/large.png" alt="A Half Brained Idea"> </noscript> </a> </figure>
You might also like
Tags
accordion accordion menu animation navigation animation navigation menu carousel checkbox inputs css3 css3 menu css3 navigation date picker dialog drag drop drop down menu drop down navigation menu elastic navigation form form validation gallery glide navigation horizontal navigation menu hover effect image gallery image hover image lightbox image scroller image slideshow multi-level navigation menus rating select dependent select list slide image slider menu stylish form table tabs text effect text scroller tooltips tree menu vertical navigation menu