Interchange

Interchange uses media queries to load the images that are appropriate for a user's browsers, making responsive images a snap.

Resize window to see Interchange in action


Using Interchange With HTML Content

Interchange can now pull in HTML partials so you can load different sections of your page for particular media queries. This is great for loading up a mobile-friendly component on small devices, and a more robust or heavier load component on desktops.

We use the data-interchange attribute on a markup container (probably a div) to do this. Here's an example which loads up a small, static map on mobile, a larger static map for medium-sized devices, and a full interactive Google map on large display devices.


<div data-interchange="[small-map.html, (small)], [medium-map.html, (medium)], [large-map.html, (large)]"></div>

Using Interchange With Images

The Interchange component utilizes the data-interchange attribute for specifying your media queries and their respective images.

<img data-interchange="[/path/to/default.jpg, (default)], [/path/to/bigger-image.jpg, (large)]">

<img data-interchange="[/path/to/default.jpg, (only screen and (min-width: 1px))], [/path/to/bigger-image.jpg, (only screen and (min-width: 1280px))]">

Each rule passed to data-interchange is comma delimited and encapsulated in square brackets, and each argument within a rule is also comma delimited. The first parameter is the path to your image, and the second parameter is your media query, surrounded by parenthesis.

data-interchange="[image_path, (media query)], [image_path, (media query)]"

The last rule that evaluates to true will be the image that gets loaded. We recommend that you set your image src to the smallest one, as this will always get loaded.

Using Retina Images

You can easily include retina images by using a pixel-density media query for that image. You can even combine multiple parameters in the media query if need be. The retina media query would look something like this, but you can also use dpi or other pixel densities as well:

data-interchange="[image_path, (screen and only (min-width: 640px) and (-webkit-min-device-pixel-ratio: 2))]"

Using Interchange with background-images

When you add a data-interchange attribute containing image paths (.jpg, .jpeg, .png, .gif, .bmp, .tiff), instead of replacing the element content it will set a background-image css property with the corresponding path.



<div data-interchange="[/path/to/default.jpg, (default)], [/path/to/bigger-image.jpg, (large)]"></div>

This solution does not support browsers with JavaScript disabled. To add a fallback you can add a default background-image in your css but this can result in loading more than one image on some device. This method is only switching the background-image on the element. You will need to set the others background properties in your css (background-repeat, background-position, background-size...).

Named Queries

Interchange has a few built-in shortcuts that correlate with our visibility classes.

Name Media Query
default only screen and (min-width: 1px)
small only screen and (min-width: 1px)
medium only screen and (min-width: 641px)
large only screen and (min-width: 1024px)
landscape only screen and (orientation: landscape)
portrait only screen and (orientation: portrait)
retina (4.2.1) only screen and (-webkit-min-device-pixel-ratio: 2),
only screen and (min--moz-device-pixel-ratio: 2),
only screen and (-o-min-device-pixel-ratio: 2/1),
only screen and (min-device-pixel-ratio: 2),
only screen and (min-resolution: 192dpi),
only screen and (min-resolution: 2dppx)

To use the named queries, just pass the name in where you would normally specify your query.

data-interchange="[image_path, (large)], [image_path, (landscape)]"

Custom Named Queries

Custom queries can be defined during initialization and are an easy way to make your queries more readable. You can also override the default queries in this way:

$(document).foundation('interchange', {
  named_queries : {
    my_custom_query : 'only screen and (max-width: 200px)'
  }
});

Events

Interchange triggers a replace event when a rule evaluates to true and the image has been replaced. This can be useful when you want to change some styles on your page based on which image is loaded.

$(document).on('replace', 'img', function (e, new_path, original_path) {
  console.log(e.currentTarget, new_path, original_path);
});

Adding New Images After Page Load

If you add new images after the page has been loaded, you will need to trigger a reflow by running one of the following:

$(document).foundation('interchange', 'reflow');

// OR for all Foundation components
$(document).foundation('reflow');