respimage 是一个响应式图像补丁,可快速且负责任地加载您的图像
#respimage
respimage is a fast, lightweight and robust responsive images polyfill, that saves the users bandwidth by utilizing smart resource selection algorithm. It implements the srcset/sizes attributes as also the picture element. Unlike other responsive images polyfills respimage plays nicely with your graceful degradation / progressive enhancement and image SEO strategy.
##Download and Embed Simply download the respimage.min.js script and add it to your website or bundle it in your normal JS.
respimage will automatically run and polyfill all images. So you can simply start writing responsive images.
In case you want to include respimage only if the browser doesn't support responsive images you can add the following inline script at the top of your head (before any stylesheets or any blocking JS, This should be added as inline script and not inside of an external script.):
In case you need to support IE8 and want to include the script at the bottom you need to use either the html5shiv or add at least the following script inside your head element:
document.createElement('picture');
Also note, that only IE8 in strict mode is supported. In case you need to support IE8 compatibility view or IE7, please use the oldie plugin.
###Mobile support
For mobile support it is crucial to set the viewport meta tag to device-width
####Install via bower
$ bower install respimage --save
####Install via npm
$ npm install respimage --save
##Markup Examples Responsive images can be technically differentiated between 2 types.
srcset with source descriptors (let the browser choose the right image based on screen size/resolution, bandwidth…):x) (for static image sizes, Retina vs. normal resolution)w) and the corresponding sizes attribute (for flexible, responsive / adaptive images (automatically also includes Retina optimization)picture element with its source[media] children (gives the author control about what srcset should be chosen by the browser depending on specific media queries)###srcset with the density x descriptor
The x descriptor is natively supported in Firefox 38, Chrome 34+ and Safari 7.1+. All other browsers will be polyfilled.
Note: You must not mix the w and the x descriptor in one srcset attribute!
###srcset with the width w descriptor and the sizes attribute
The w descriptor is in Firefox 38 and Chrome 38+. All other browsers will be polyfilled.
Note: You must not mix the w and the x descriptor in one srcset attribute!
###The picture element
The picture element is currently only supported in Firefox 38 and Chrome 38+. All other browsers will be polyfilled. To support IE9 all source elements have to be wrapped inside of an audio or hidden video element:
The art direction approach of the picture element and the descriptor approach can also be mixed:
##API
###respimage function
In case new responsive images are created and dynamically added to the DOM simply invoke the respimage method.
window.respimage();
Here an extended example how this could look like in a jQuery environment.
$("div.dynamic-context").load("path-to-content.html", function(){
if( window.respimage ) {
respimage();
}
});
In case you are dynamically changing relevant attributes (srcset, sizes, media) on responsive images or their associated source elements, you need to call respimage with an additional argument:
respimage({elements: [imageElement], reevaluate: true});
In the unlikely case you want either remove the srcset of an img (not of an source) or want to directly change the src of an responsive image the additional src or srcset option has to be set (Note: In most cases, you don't want to do that!).
var $imgs = $('img').removeAttr('srcset');
respimage({elements: $imgs, reevaluate: true, srcset: true});
//or for src
var $imgs = $('img').attr('src', 'some-img.jpg');
respimage({elements: $imgs, reevaluate: true, src: true});
Note: The reparse option was renamed with version 1.4.0 to reevaluate to better match the option used by picturefill.
In case you are not supporting IE8 we recommend to use the Mutation plugin instead of using this API (It fully polyfills also the DOM APIs and makes additional calls to respimage automatically for you).
respimage supports a broad range of browsers and devices. It is actively tested in the following browsers and devices IE8+, Firefox (ESR and current), Safari 7.0+, Chrome, Opera, Android 4.1+ and IOS 7+, but should work in a lot more browsers/devices. IE6 and IE7 are only supported with the oldIE plugin.
##Troubleshooting and bug reporting In case of any problems include the respimage.dev.js into your project and open your JS console. In case you think you have found a bug, please create a testcase and then report your issue. Note: You should not use the dev build inside your production environment, because it is a lot slower.
*Note: It is highly recommended to test with the .dev.js file, especially if you are using responsive images the first time or you start a new project setup. The respimage.dev.js file can give you some useful hints in the console. About 80% of all tutorials suggest wrong markup examples! Also note: That our respimg debugger can't check every possible error.
##Plugins
respimage has some really nice extensions/plugins to improve standards support even more. In case you want to use a CDN, you can use the combohandler service provided by jsDelivr:
###The intrinsic sizes / dimensions - Plugin
The intrinsic dimension plugin extends respimage to add the intrinsic dimension based on the descriptor (and the sizes attribute) and the density of the source candidate to the width content attribute of the image element.
###The Mutation - Plugin
This plugin automatically detects new responsive images and also any changes to srcset/media and sizes attributes. It also implements the corresponding DOM properties for those attributes.
###The typesupport - Plugin The type support plugin adds type support detection for the following image file types: apng, JPEG 2000, JPEG XR, WEBP
###The oldie - Plugin Respimage supports IE8+ (including) out of the box. In case you need to support IE6/7 or any IE in compatibility view or quirksmode use the oldie plugin.
##Known issues/caveats/
src attribute or - if omitted - show only the alt text. In case a src attribute is used non-supporting browser might download a wasted addtional image. For workarounds and markup patterns to improve this problem see below.src strategy. This yields among other things to the fact, that using an inital src attribute in conjunction with respimage can improve perceived performance (although an additional request is generated:###low quality image source
In case JS off is a concern. Use a low quality source as the fallback src. As soon as an image has already a source respimage will not simply switch the image src but will implement the low quality image placeholder pattern. While the lquip technique can often increase the time until the onload event and the transferred image data, it improves perceived performance:
Due to the fact, that the lqip src attribute can be optimized by the browser's preload parser this technique yields to a very fast first impression, while the best image candidate can be loaded in the background.
This technique can be combined with lazyLoading, which will also additionally decrease the time until onload event and gives you the possibility to implement the improved perceived performance also for native supporting browsers.
###Omit the src
In case JS disabled legacy browsers are no concern and you can't provide an lquip source or you are using client side rendering (No preload parser optimization advantage), use a one pixel src or better a data URI.
###Simply live with it and use either the most often used or the smallest source candidate as the fallback src
In this case respimage will guard your chosen src strategy and will only kick in as a progressive enhancement script in the following situations:
the inital image candidate is too heavy and the browser supports image request abortion
the inital image candidate is too fuzzy (see LQIP pattern above)
or art direction is envolved and the fallback src candidate is not part of the chosen srcset
In case the currently set source candidate is not perfect (or perfect of course), but good enough the src won't be changed by respimage. (See also the lazyFactor option below.)
Media queries support in old IEs (IE8/IE9) are limited to min-width, max-width, max-height and min-height. For IE9 it is possible to extend support by including a matchMedia polyfill.
##Responsive images and lazy loading Beside the fact, that lazy loading improves performance, there is an interesting side effect. Due to delayed image loading the sizes attribute can be dynamically calculated with JS and makes integrating responsive images in any environment therefore easy. We recommend lazysizes.
##Setting options
respimage uses the asynchronous push syntax for configuration. Simply create a global respimgCFG array if it doesn't exist already and push your options:
window.respimgCFG = window.respimgCFG || [];
respimgCFG.push(['lazyFactor', 0.6]);
Also Note: respimage is a drop-in polyfill solution and you normally shouldn't need to configure anything. But in case you want to play around with respimage's options here is a small testing zone for you.
###The maxX option (default: 2)
Due to the fact that reliable bandwidth detection is nearly impossible and 3x image density means 9x image data respimage constraints the maximum considered devicePixelRatio to 2. In case you want to serve 3x images to 3x devices even with possibly lower bandwidth set this option to 3:
window.respimgCFG = window.respimgCFG || [];
respimgCFG.push(['maxX', 2]);
Note: This only affects polyfilled browsers. In case you want to constrain the maximum dpi for all browser you can try lazySizes - optimumx extension.
###The lazyFactor option (default: 0.4)
In case an image already has a source candidate (either initially set as src attribute or on resize) respimage becomes lazy changing the source. The higher the lazyFactor the more respimage honors your fallback src. Reasonable values
暂无开放 Issues,或尚未同步最近议题。