% @side_nav = "js" %>
data-interchange
The Foundation Interchange component utilizes the data-interchange
attribute for specifying your media queries and their respective images.
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.
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.
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:
<%= code_example ' data-interchange="[image_path, (screen and only (min-width: 640px) and (-webkit-min-device-pixel-ratio: 2))]" ', :html %>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: 768px) |
medium | only screen and (min-width: 1280px) |
large | only screen and (min-width: 1440px) |
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),
|
To use the named queries, just pass the name in where you would normally specify your query.
<%= code_example ' data-interchange="[image_path, (large)], [image_path, (landscape)]" ', :html %>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:
<%= code_example " $(document).foundation('interchange', { named_queries : { my_custom_query : 'only screen and (max-width: 200px)' } }); ", :js %>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.
If you add new images after the page has been loaded, you will need to trigger a reflow
by running one of the following:
Interchange requires the Foundation Core, foundation.js
and the foundation.interchange.js
component to function.