Data Picture
Needs translation
Purpose
Allow a web page to specify different image sources to display based on media queries. The component is an event driven port of Picturefill.
Use when
- Displaying different sized images for different browser screen sizes.
- Displaying different resolution images to devices with different pixel densities.
Working example
How to implement
- Add a
data-pic
element for each image that will have multiple sources:<span data-pic="data-pic" data-alt="Alt tag for the image">
- Nested inside the
data-pic
element, specify the default image, different images sources and fallback image:- Default image: image that will be displayed if no media queries match or the browser doesn't support media queries:
<span data-src="img/default.jpg"></span>
- Different image sources: images that will be displayed if their media query matches:
<span data-src="img/small.jpg" data-media="(min-width: 0px)"></span> <span data-src="img/medium.jpg" data-media="(min-width: 500px)"></span> <span data-src="img/large.jpg" data-media="(min-width: 960px)"></span>
- Fallback image: image to display if JavaScript is not supported:
<noscript><img src="img/fallback.jpg" alt="Alt tag for the image"/></noscript>
- Default image: image that will be displayed if no media queries match or the browser doesn't support media queries:
When you're finished, you should have an element that looks like the following:
<span data-pic="data-pic" data-alt="Alt tag for the image">
<!-- Default image -->
<span data-src="img/default.jpg"></span>
<!-- Images for browsers with CSS media query support -->
<span data-src="img/small.jpg" data-media="(min-width: 0px)"></span>
<span data-src="img/medium.jpg" data-media="(min-width: 500px)"></span>
<span data-src="img/large.jpg" data-media="(min-width: 960px)"></span>
<!-- Fallback content for non-JS browsers. -->
<noscript><img src="img/fallback.jpg" alt="Alt tag for the image"/></noscript>
</span>
Configuration options
All configuration options of the plugin are controlled with data attributes:
Option | Description | How to configure | Values |
---|---|---|---|
data-pic |
Identifies this as an element that uses the data picture plugin. | n/a |
|
data-alt |
Provides an alt attribute value to the image element created by the plugin. | Set to the string value to use as the alt attribute. |
|
data-class |
Provides a class attribute value to the image element created by the plugin. | Set to the string value to use as the image's CSS attribute. |
|
data-src |
Image source that will be used if this element's media query (specified by data-media) matches. | Set to the image resource path that should be displayed. |
|
data-media |
Media query that determines when a given image source should be displayed. | Set to the media query that should cause this element's image (specified by data-src) to display. |
|
Events
Document the public events that can be used by implementers or developers.
Event | Trigger | What it does |
---|---|---|
wb-init.wb-pic |
Triggered manually (e.g., $( "[data-pic]" ).trigger( "wb-init.wb-pic" ); ). |
Initializes the plugin and determines which data-pic image source should be displayed. Note: the data picture plugin will be initialized automatically unless it is added after the page has already loaded. |
wb-ready.wb-pic (v4.0.5+) |
Triggered automatically after Date Picture initializes. | Used to identify where Data Picture was initialized (target of the event)
|
wb-updated.wb-pic (v4.0.5+) |
Triggered automatically each time the picture is updated. | Used to identify which picture was updated (target of the event)
|
picfill.wb-pic |
Triggered during plugin initialization and manually (e.g., $( "[data-pic]" ).trigger( "picfill.wb-pic" ); ). |
Causes the data-pic element to determine which image source should be displayed. This could be called as part of a window resize event handler. |
wb-ready.wb (v4.0.5+) |
Triggered automatically when WET has finished loading and executing. | Used to identify when all WET plugins and polyfills have finished loading and executing.
|
Source code
- Date de modification :