{"id":29926574,"url":"https://github.com/ecomfe/saber-matchmedia","last_synced_at":"2025-08-02T12:42:19.121Z","repository":{"id":11553016,"uuid":"14038860","full_name":"ecomfe/saber-matchmedia","owner":"ecomfe","description":"移动端 matchMedia 支持","archived":false,"fork":false,"pushed_at":"2015-05-19T06:51:59.000Z","size":188,"stargazers_count":8,"open_issues_count":0,"forks_count":1,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-07-22T05:16:44.177Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ecomfe.png","metadata":{"files":{"readme":"README.md","changelog":"History.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-11-01T08:33:24.000Z","updated_at":"2017-04-09T08:19:01.000Z","dependencies_parsed_at":"2022-08-30T20:00:55.299Z","dependency_job_id":null,"html_url":"https://github.com/ecomfe/saber-matchmedia","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/ecomfe/saber-matchmedia","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ecomfe%2Fsaber-matchmedia","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ecomfe%2Fsaber-matchmedia/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ecomfe%2Fsaber-matchmedia/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ecomfe%2Fsaber-matchmedia/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ecomfe","download_url":"https://codeload.github.com/ecomfe/saber-matchmedia/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ecomfe%2Fsaber-matchmedia/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268392148,"owners_count":24243297,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-02T02:00:12.353Z","response_time":74,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-08-02T12:42:04.825Z","updated_at":"2025-08-02T12:42:19.087Z","avatar_url":"https://github.com/ecomfe.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"saber-matchmedia\n===\n\n移动端 [`matchMedia`](http://dev.w3.org/csswg/cssom-view/#dom-window-matchmedia) 支持。\n\n## Installation\n\n通过 [edp](https://github.com/ecomfe/edp) 引入模块：\n\n```sh\nedp import saber-matchmedia\n```\n\n## Usage\n\n该模块有 **两个版本**：*完整版* 性能高，体积较大；*简化版* 体积小，功能受限；请根据项目需求自由选择。\n\n#### 完整版\n\n默认版本，返回的对象包含 `MediaQueryList` 的所有主要功能：\n\n```js\nvar matchMedia = require('saber-matchmedia');\n```\n\n*完整版* 来自 [`weblinc/media-match`](https://github.com/weblinc/media-match)，性能 (*via [jsperf](http://jsperf.com/matchmedia/11)*) 比 `Native matchMedia` 高，远超 `paulirish/matchMedia.js`。\n\n#### 简化版\n\n该版本 **不支持** `MediaQueryList` 的 `addListener`、`removeListener`：\n\n```js\nvar matchMedia = require('saber-matchmedia/lite');\n```\n\n*简化版* 是 [`paulirish/matchMedia.js`](https://github.com/paulirish/matchMedia.js) 不带 **桌面浏览器** 兼容的版本。\n\n### 屏幕翻转\n\n因为 `orientationchange` 的支持情况不佳，`resize` 又在很多情况下都会触发，所以通常用该模块来判断屏幕翻转：\n\n```javascript\nvar matchMedia = require('saber-matchmedia');\n\nmatchMedia('screen and (orientation: portrait)')\n    .addListener(orientationHandler);\n\nfunction orientationHandler(mql) {\n    // do something...\n}\n```\n\n## API\n\n使用方式与标准的 `window.matchMedia` 一致，请参考 [MDN上的文档](https://developer.mozilla.org/en-US/docs/Web/API/Window.matchMedia)。\n\n### Methods\n\n#### matchMedia(mediaQueryString)\n\n返回一个 `MediaQueryList` 对象（在不支持原生 `matchMedia` 的设备返回模拟的对象）。\n\n* **mediaQueryString** `{string}` Media Query\n* _return_ `{MediaQueryList}`\n\n### Classes\n\n`MediaQueryList` 实例的属性与方法。\n\n#### matches\n\nMedia Query 规则匹配则返回 `true`，否则为 `false`。\n\n* _return_ `{boolean}`\n\n#### media\n\nMeida Query 的值。\n\n* _return_ `{string}`\n\n#### addListener(listener)\n\n`MediaQueryList` 对象 **添加** 监听函数的方法。\n\n* **listener** `{Function}` 回调函数\n\n#### removeListener(listener)\n\n`MediaQueryList` 对象 **移除** 监听函数的方法。\n\n* **listener** `{Function}` 回调函数\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fecomfe%2Fsaber-matchmedia","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fecomfe%2Fsaber-matchmedia","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fecomfe%2Fsaber-matchmedia/lists"}