Angular Core Dependency: >= 1.0.6
Module License: MIT
Added by: Sam Deering
Angular directive for handling responsive images.
bower install angular-responsive-images
bh-src-responsive directive takes an array of arrays. The inner arrays each have two elements, the first being the media query and the second being the href of the image to load if that media query matches. The last matcing media query gets used so order your array from smallest to largest.
Also you can use a default small image for
src="" as that will always get loaded, and can prevent weird page reflows when we alter the
src after your angular app is loaded.
// In your script file, inject the ngResponsiveImages module var app = angular.module('yourModule', ['bhResponsiveImages']);
<!-- Use the bh-src-responsive directive to set up your queries and sources --> <img src="small_image.jpg" bh-src-responsive="[ [ '(min-width: 960px)', 'larger_image.jpg' ], [ '(min-width: 1700px)', 'much_larger_image.jpg' ] ]" />
Changes to the viewport that causes your media queries to either suddenly match or no longer match (i.e. from browser resizing, orientation change, etc), will trigger updates to the source. Because these event handlers can be fired concurrently, running through the media query set happens within a zero-second
$timeout. That way when you have three media queryies and maximize your browser from small to large, the image source doesn't get set to the last matching media query 3 separate times.
Here are some useful presets that you can use, which were stolen from Foundation.
|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)|
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)
<img src="blah.jpg" bh-src-responsive="[ [ 'retina', 'big_retina_image.jpg' ] ]" />
It's difficult to test different viewport sizes, or at least difficult enough that I can't figure it out. So right now I dynamically get the
innerHeight properties to know what the testing browser has, then my tests run off that.
The test tasks a pretty simple:
grunt test # Lint, build from source, and run the test suite grunt debug # Start a watch for the files. Re-lint, re-build, and run tests when the source files change; # re-lint and run tests when the test spec files change.
You can also pass a --browsers option to these tasks (the default browser is PhantomJS):
grunt debug --browsers=Chrome,Firefox,PhantomJS,IE # Automatically run the test suite in all 4 browsers
The idea for this directive came from Foundation's interchange attribute. Also their media query preset were mercilessly stolen.
Module stats last updated: 2017-02-07 16:00:02