项目作者: simonsmith

项目描述 :
Simplify handling of retina images in CSS
高级语言: JavaScript
项目地址: git://github.com/simonsmith/postcss-at2x.git
创建时间: 2015-03-24T00:02:07Z
项目社区:https://github.com/simonsmith/postcss-at2x

开源协议:MIT License

下载


postcss-at2x Build Status

Ported from rework-plugin-at2x

Installation

  1. $ npm install postcss postcss-at2x --save-dev

Usage

  1. const fs = require('fs');
  2. const postcss = require('postcss');
  3. const at2x = require('postcss-at2x');
  4. const input = fs.readFileSync('input.css', 'utf8');
  5. const output = postcss()
  6. .use(at2x())
  7. .process(input)
  8. .then(result => console.log(result.css));

.at2x()

Adds at-2x keyword to background and background-image declarations to add retina support for images.

Input

  1. .multi {
  2. background: url(http://example.com/image.png),
  3. linear-gradient(to right, rgba(255, 255, 255, 0), rgba(255, 255, 255, 1)),
  4. green,
  5. url(/public/images/cool.png) at-2x;
  6. }

Output

  1. .multi {
  2. background: url(http://example.com/image.png),
  3. linear-gradient(to right, rgba(255, 255, 255, 0), rgba(255, 255, 255, 1)),
  4. green,
  5. url(/public/images/cool.png);
  6. }
  7. @media (min-device-pixel-ratio: 1.5), (min-resolution: 144dpi), (min-resolution: 1.5dppx) {
  8. .multi {
  9. background-image: url(http://example.com/image.png),
  10. linear-gradient(to right, rgba(255, 255, 255, 0), rgba(255, 255, 255, 1)),
  11. none,
  12. url(/public/images/cool@2x.png);
  13. }
  14. }

Options

identifier (default: "@2x") string

Change the identifier added to retina images, for example file@2x.png can be file-retina.png.

detectImageSize (default: false) boolean

Obtains the image dimensions of the non-retina image automatically and applies them to the
background-size property of the retina image.

skipMissingRetina (default: false) boolean

If the retina image cannot be found on the file system it will be skipped and
not output into the result CSS.

resolveImagePath function

Get resolved image path for detecting image size. By default, original url value is resolved from current working directory (process.cwd()).

Function receives two arguments: original url value and PostCSS declaration source.

Output

  1. .element {
  2. background: url(img.jpg) no-repeat;
  3. }
  4. @media (min-device-pixel-ratio: 1.5), (min-resolution: 144dpi), (min-resolution: 1.5dppx) {
  5. .element {
  6. background: url(img@2x.jpg) no-repeat;
  7. background-size: 540px 675px; /* Dimensions of img.jpg */
  8. }
  9. }

See PostCSS docs for examples for your environment.