web 3d图形渲染器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
4.7 KiB

  1. # PostCSS image-set() Function [<img src="https://postcss.github.io/postcss/logo.svg" alt="PostCSS Logo" width="90" height="90" align="right">][postcss]
  2. [![NPM Version][npm-img]][npm-url]
  3. [![CSS Standard Status][css-img]][css-url]
  4. [![Build Status][cli-img]][cli-url]
  5. [![Support Chat][git-img]][git-url]
  6. [PostCSS image-set() Function] lets you display resolution-dependent images
  7. using the `image-set()` function in CSS, following the [CSS Images]
  8. specification.
  9. ```pcss
  10. .example {
  11. background-image: image-set(
  12. url(img.png) 1x,
  13. url(img@2x.png) 2x,
  14. url(img@print.png) 600dpi
  15. );
  16. }
  17. /* becomes */
  18. .example {
  19. background-image: url(img.png);
  20. }
  21. @media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
  22. .example {
  23. background-image: url(img@2x.png);
  24. }
  25. }
  26. @media (-webkit-min-device-pixel-ratio: 6.25), (min-resolution: 600dpi) {
  27. .example {
  28. background-image: url(my@print.png);
  29. }
  30. }
  31. .example {
  32. background-image: image-set(
  33. url(img.png) 1x,
  34. url(img@2x.png) 2x,
  35. url(img@print.png) 600dpi
  36. );
  37. }
  38. ```
  39. ## Usage
  40. Add [PostCSS image-set() Function] to your project:
  41. ```bash
  42. npm install postcss-image-set-function --save-dev
  43. ```
  44. Use [PostCSS image-set() Function] to process your CSS:
  45. ```js
  46. const postcssImageSetFunction = require('postcss-image-set-function');
  47. postcssImageSetFunction.process(YOUR_CSS /*, processOptions, pluginOptions */);
  48. ```
  49. Or use it as a [PostCSS] plugin:
  50. ```js
  51. const postcss = require('postcss');
  52. const postcssImageSetFunction = require('postcss-image-set-function');
  53. postcss([
  54. postcssImageSetFunction(/* pluginOptions */)
  55. ]).process(YOUR_CSS /*, processOptions */);
  56. ```
  57. [PostCSS image-set() Function] runs in all Node environments, with special
  58. instructions for:
  59. | [Node](INSTALL.md#node) | [PostCSS CLI](INSTALL.md#postcss-cli) | [Webpack](INSTALL.md#webpack) | [Create React App](INSTALL.md#create-react-app) | [Gulp](INSTALL.md#gulp) | [Grunt](INSTALL.md#grunt) |
  60. | --- | --- | --- | --- | --- | --- |
  61. ## Options
  62. ### preserve
  63. The `preserve` option determines whether the original declaration using
  64. `image-set()` is preserved. By default, it is preserved.
  65. ```js
  66. postcssImageSetFunction({ preserve: false })
  67. ```
  68. ```pcss
  69. .example {
  70. background-image: image-set(
  71. url(img.png) 1x,
  72. url(img@2x.png) 2x,
  73. url(img@print.png) 600dpi
  74. );
  75. }
  76. /* becomes */
  77. .example {
  78. background-image: url(img.png);
  79. }
  80. @media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
  81. .example {
  82. background-image: url(img@2x.png);
  83. }
  84. }
  85. @media (-webkit-min-device-pixel-ratio: 6.25), (min-resolution: 600dpi) {
  86. .example {
  87. background-image: url(my@print.png);
  88. }
  89. }
  90. ```
  91. ### onvalid
  92. The `oninvalid` option determines how invalid usage of `image-set()` should be
  93. handled. By default, invalid usages of `image-set()` are ignored. They can be
  94. configured to display a `warning` or `throw` an error.
  95. ```js
  96. postcssImageSetFunction({ oninvalid: 'warning' }) // warn on invalid usages
  97. ```
  98. ```js
  99. postcssImageSetFunction({ oninvalid: 'throw' }) // throw on invalid usages
  100. ```
  101. ## Image Resolution
  102. The `image-set()` function allows an author to provide multiple resolutions of
  103. an image and let the browser decide which is most appropriate in a given
  104. situation. The `image-set()` also never fails to choose an image; the
  105. `<resolution>` just helps determine which of the images is chosen.
  106. Since this plugin is not a browser, the image options are sorted by device
  107. pixel ratio and the lowest ratio is used as the default, while the remaining
  108. images are pushed behind media queries.
  109. Therefore, this plugin can only approximate native browser behavior. While
  110. images should typically match the resolution as the device they’re being viewed
  111. in, other factors can affect the chosen image. For example, if the user is on a
  112. slow mobile connection, the browser may prefer to select a lower-res image
  113. rather than wait for a larger, resolution-matching image to load.
  114. [cli-img]: https://img.shields.io/travis/jonathantneal/postcss-image-set-function.svg
  115. [cli-url]: https://travis-ci.org/jonathantneal/postcss-image-set-function
  116. [css-img]: https://cssdb.org/badge/image-set-function.svg
  117. [css-url]: https://cssdb.org/#image-set-function
  118. [git-img]: https://img.shields.io/badge/support-chat-blue.svg
  119. [git-url]: https://gitter.im/postcss/postcss
  120. [npm-img]: https://img.shields.io/npm/v/postcss-image-set-function.svg
  121. [npm-url]: https://www.npmjs.com/package/postcss-image-set-function
  122. [CSS Images]: https://drafts.csswg.org/css-images-4/#image-set-notation
  123. [Gulp PostCSS]: https://github.com/postcss/gulp-postcss
  124. [Grunt PostCSS]: https://github.com/nDmitry/grunt-postcss
  125. [PostCSS]: https://github.com/postcss/postcss
  126. [PostCSS Loader]: https://github.com/postcss/postcss-loader
  127. [PostCSS image-set() Function]: https://github.com/jonathantneal/postcss-image-set-function