Respond README

The project's README from github.com/scottjehl/Respond. As one markdown file: README.md.

Respond.js

A fast & lightweight polyfill for min/max-width CSS3 Media Queries (for IE 6-8, and more)

The goal of this script is to provide a fast and lightweight (3kb minified / 1kb gzipped) script to enable responsive web designs in browsers that don't support CSS3 Media Queries - in particular, Internet Explorer 8 and under. It's written in such a way that it will probably patch support for other non-supporting browsers as well (more information on that soon).

If you're unfamiliar with the concepts surrounding Responsive Web Design, you can read up here and also here

Demo page (the colors change to show media queries working)

Usage Instructions

  1. Craft your CSS with min/max-width media queries to adapt your layout from mobile (first) all the way up to desktop
    @media screen and (min-width: 480px){
        /** ...styles for 480px and up go here **/
    }
  1. Reference the respond.min.js script (1kb min/gzipped) after all of your CSS (the earlier it runs, the greater chance IE users will not see a flash of un-media'd content)

  2. Crack open Internet Explorer and pump fists in delight

CDN/X-Domain Setup

Respond.js works by requesting a pristine copy of your CSS via AJAX, so if you host your stylesheets on a CDN (or a subdomain), you'll need to set up a local proxy to request the CSS for old IE browsers. Prior versions recommended a deprecated x-domain approach, but a local proxy is preferable (for performance and security reasons) to attempting to work around the cross-domain limitations.

Support & Caveats

Some notes to keep in mind:

How's it work?

Basically, the script loops through the CSS referenced in the page and runs a regular expression or two on their contents to find media queries and their associated blocks of CSS. In Internet Explorer, the content of the stylesheet is impossible to retrieve in its pre-parsed state (which in IE 8-, means its media queries are removed from the text), so Respond.js re-requests the CSS files using Ajax and parses the text response from there. Be sure to configure your CSS files' caching properly so that this re-request doesn't actually go to the server, hitting your browser cache instead.

From there, each media query block is appended to the head in order via style elements, and those style elements are enabled and disabled (read: appended and removed from the DOM) depending on how their min/max width compares with the browser width. The media attribute on the style elements will match that of the query in the CSS, so it could be "screen", "projector", or whatever you want. Any relative paths contained in the CSS will be prefixed by their stylesheet's href, so image paths will direct to their proper destination

API Options?

Sure, a couple:

Alternatives to this script

This isn't the only CSS3 Media Query polyfill script out there; but it damn well may be the fastest.

If you're looking for more robust CSS3 Media Query support, you might check out https://code.google.com/p/css3-mediaqueries-js/. In testing, I've found that script to be noticeably slow when rendering complex responsive designs (both in filesize and performance), but it really does support a lot more media query features than this script. Big hat tip to the authors! :)

Respond (scottjehl/Respond)

Searching Database…

11.2K ·GitHub
archived
这是什么?一个快速、轻量的 JavaScript polyfill,能让不支持 CSS3 min/max-width 媒体查询的浏览器获得支持——主要是 IE 8 及更旧版本——让响应式网页设计在这些浏览器上也能正常工作。
这是一个 CSS 框架,还是只需要往页面里放一个脚本?不是框架——就是一个小脚本(压缩后 3kb,gzip 后 1kb),除了自带的一个 matchMedia polyfill 之外没有任何依赖。已经支持媒体查询的浏览器会通过一个快速检测被跳过,所以它实际上只在需要的地方才会运行。
我在自己的网站上怎么开始使用它?照常在你的 CSS 里写 min/max-width 媒体查询,然后在所有 CSS 之后引用 respond.min.js——它运行得越早,IE 用户看到无样式内容闪现的可能性就越小。
它支持所有类型的媒体查询吗?范围是有意收窄的:只转换 min-width 和 max-width 查询(适用于所有媒体类型)。不支持嵌套的媒体查询,通过 @import 引入的 CSS 以及 style 元素里的 CSS 也会被忽略。
它在哪里运行——是在浏览器里,还是服务器上还需要装点什么?完全在客户端运行,但它会通过 Ajax 重新请求你的 CSS 文本,所以页面必须通过 HTTP 提供(file:// 不行),而托管在 CDN 或子域名上的样式表需要一个本地代理才能提供给旧版 IE。
它还在维护吗——我今天还该用它吗?不该用。这个仓库已经归档,它面向的浏览器(IE 6-8)早就淘汰了——所有现代浏览器都原生支持媒体查询。把它当作给非常老的代码库用的遗留补丁,而不是新项目的依赖。
为什么它要重新下载我的 CSS?IE 8 及更旧版本在解析样式表时会丢掉媒体查询,所以 Respond.js 通过 Ajax 重新获取原始 CSS,用正则表达式找出查询块,再把它们注入为 style 元素,并随着窗口宽度变化启用或禁用。
有哪些最容易踩的坑需要注意?把 @font-face 规则放在媒体查询之外,否则 IE 7/8 在加载时会卡死;样式表数量保持在 32 个以内,否则 IE 会报错;避免使用 UTF-8 BOM 编码;另外 Sass/SCSS 的 debug-info 注释会破坏它的解析器。
它提供 JavaScript API 吗?提供了一个小的:respond.update() 用于在你添加样式表后重新运行解析器,respond.mediaQueriesSupported 表示浏览器是否原生支持媒体查询,respond.getEmValue() 返回一个 em 对应的像素值。
Creator
scottjehl
Size
0.3 MB
Stars
11.2K
Age
15y
Commits · 90 Days