{"id":9051,"date":"2019-11-08T01:09:38","date_gmt":"2019-11-07T17:09:38","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=9051"},"modified":"2019-11-14T10:04:00","modified_gmt":"2019-11-14T02:04:00","slug":"js-css-supports-detect","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2019\/11\/js-css-supports-detect\/","title":{"rendered":"JS\u68c0\u6d4bCSS\u5c5e\u6027\u6d4f\u89c8\u5668\u662f\u5426\u652f\u6301\u7684\u591a\u79cd\u65b9\u6cd5"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=9051\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=9051<\/a><br \/>\n\u672c\u6587\u6b22\u8fce\u5206\u4eab\u4e0e\u805a\u5408\uff0c\u5168\u6587\u8f6c\u8f7d\u5c31\u4e0d\u5fc5\u4e86\uff0c\u5c0a\u91cd\u7248\u6743\uff0c\u5708\u5b50\u5c31\u8fd9\u4e48\u5927\uff0c\u82e5\u6025\u7528\u53ef\u4ee5\u8054\u7cfb\u6388\u6743\u3002<\/p>\n<p><script>window.wxShareImgUrl = 'https:\/\/www.zhangxinxu.com\/study\/image\/share\/201911-css-supports.png';<\/script><\/p>\n<h3>\u4e00\u3001\u539f\u751fCSS.supports\u8bed\u6cd5<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201911\/pandeng.png\" width=\"484\" height=\"300\" alt=\"\u65b9\u6cd5\u603b\u662f\u6709\u7684\" class=\"alignright clip thumb size-medium\" \/><\/p>\n<p>\u8fd4\u56de\u5e03\u5c14\u503c<code>true<\/code>\u6216\u8005<code>false<\/code>\uff0c\u7528\u6765\u68c0\u6d4b\u662f\u5426\u652f\u6301\u67d0CSS\u5c5e\u6027\u3002<\/p>\n<h4>\u8bed\u6cd5<\/h4>\n<pre>CSS.supports(propertyName, value);\r\nCSS.supports(supportCondition);<\/pre>\n<h4>\u53c2\u6570<\/h4>\n<dl>\n<dt><strong>propertyName<\/strong><\/dt>\n<dd>\u5b57\u7b26\u4e32\u3002\u7528\u6765\u68c0\u6d4b\u7684CSS\u5c5e\u6027\u540d\u3002<\/dd>\n<dt><strong>value<\/strong><\/dt>\n<dd>\u5b57\u7b26\u4e32\u3002\u7528\u6765\u68c0\u6d4b\u7684CSS\u5c5e\u6027\u503c\u3002<\/dd>\n<dt><strong>supportCondition<\/strong><\/dt>\n<dd>\u5b57\u7b26\u4e32\u3002\u7528\u6765\u68c0\u6d4b\u7684CSS\u58f0\u660e\u3001\u8868\u8fbe\u5f0f\u6216\u8005\u8bed\u6cd5\u3002<\/dd>\n<\/dl>\n<h4>\u6848\u4f8b<\/h4>\n<p>\u4f8b\u5982\uff0c\u68c0\u6d4b\u6d4f\u89c8\u5668\u662f\u5426\u652f\u6301CSS filter\u6ee4\u955c\uff0c\u5219\u53ef\u4ee5\uff1a<\/p>\n<pre>result = CSS.supports('filter', 'blur(5px)');\r\nresult = CSS.supports('filter: 5px');   <span style=\"color:#cd0000;\">\/\/ \u9519\u8bef\u8bed\u6cd5\uff0c\u8fd4\u56defalse<\/span><\/pre>\n<p>\u4f8b\u5982\u5728Chrome\u6d4f\u89c8\u5668\u4e0b\u8fd4\u56de\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201911\/2019-11-07_235710.png\" width=\"370\" height=\"198\" alt=\"CSS.supports\u6ee4\u955c\u652f\u6301\u68c0\u6d4b\" class=\"alignnone size-medium\" \/><\/p>\n<p><code>CSS.supports()<\/code>\u8fd8\u53ef\u4ee5\u7528\u6765\u68c0\u6d4b\u8868\u8fbe\u5f0f\uff0c\u4f8b\u5982<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/2016\/11\/css-css3-variables-var\/\">CSS\u53d8\u91cf<\/a>\u8bed\u6cd5\uff1a<\/p>\n<pre>result = CSS.supports('--username: zhangxinxu');\r\nresult = CSS.supports('(--username: zhangxinxu)');<\/pre>\n<p>\u7ed3\u679c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201911\/2019-11-08_000503.png\" width=\"421\" height=\"175\" alt=\"CSS.supports\u68c0\u6d4bCSS\u53d8\u91cf\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u53ef\u4ee5\u770b\u5230\u6709\u6ca1\u6709\u62ec\u53f7\u90fd\u652f\u6301\u3002<\/p>\n<p><code>CSS.supports()<\/code>\u8fd8\u652f\u6301<code>@supports<\/code>\u89c4\u5219\uff0c\u652f\u6301\u903b\u8f91\u8868\u8fbe\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><span style=\"color:hotpink;\">result = CSS.supports('width: fit-content or width: -webkit-fit-content');<\/span>\r\nresult = CSS.supports('(width: fit-content) or (width: -webkit-fit-content)');<\/pre>\n<p>\u7b2c\u4e00\u884c\uff08\u7c89\u8272\u6587\u5b57\uff09\u8fd4\u56defalse\uff0c\u7b2c\u4e8c\u884c\u652f\u6301\u8fd4\u56detrue\uff08\u89c1\u4e0b\u56fe\uff09\u3002\u53ef\u89c1\uff0c\u5728\u8fd9\u4e2a\u8bed\u6cd5\u73af\u5883\u4e0b\uff0c\u62ec\u53f7\u662f\u5fc5\u987b\u7684\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201911\/2019-11-08_001013.png\" width=\"584\" height=\"163\" alt=\"\u903b\u8f91\u89c4\u5219\u8bed\u53e5\u652f\u6301\u6d4b\u8bd5\" class=\"alignnone size-medium\" \/><\/p>\n<h4>\u517c\u5bb9\u6027<\/h4>\n<p>\u548cCSS <code>@supports<\/code>\u89c4\u5219\u517c\u5bb9\u6027\u4e00\u6837\uff0cEdge12+\u652f\u6301\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201911\/2019-11-08_001713.png\" width=\"730\" height=\"327\" alt=\"CSS \u652f\u6301 API\u517c\u5bb9\u6027\" class=\"alignnone size-medium\" \/><\/p>\n<h4>\u5c34\u5c2c\u7684\u73b0\u72b6<\/h4>\n<p>\u5b9e\u9645\u5f00\u53d1\u7684\u65f6\u5019\uff0c\u9700\u8981\u4f7f\u7528\u5230\u5bf9CSS\u68c0\u6d4b\u573a\u666f\uff0c\u5f80\u5f80\u90fd\u662f\u9488\u5bf9\u4f4e\u7248\u672c\u7684IE\u6d4f\u89c8\u5668\uff0c\u4f8b\u5982IE9-IE11\u3002<\/p>\n<p>\u4e8e\u662f\u5c34\u5c2c\u7684\u4e8b\u60c5\u51fa\u73b0\u4e86\uff0c\u4f4e\u7248\u672c\u7684IE\u6d4f\u89c8\u5668\u5e76\u4e0d\u652f\u6301\u6d4f\u89c8\u5668\u539f\u751f\u652f\u6301\u7684<code>CSS.supports()<\/code>\u65b9\u6cd5\uff0c\u4e8e\u662f\uff0c\u6211\u4eec\u7684\u5b9e\u9645\u9700\u6c42\u5e76\u6ca1\u6709\u56e0\u4e3a\u8fd9\u4e2a\u65b0\u7684API\u800c\u5f97\u5230\u89e3\u51b3\uff0c\u4e0d\u5f97\u4e0d\u6c42\u52a9\u4e8e\u5176\u4ed6\u65b9\u6cd5\u3002<\/p>\n<h3>\u4e8c\u3001JS\u8d4b\u503c\u518d\u53d6\u503c\u7684\u68c0\u6d4b\u65b9\u6cd5<\/h3>\n<p>\u8fd9\u662f\u6211<span class=\"hidden\">\u5f20\u946b\u65ed https:\/\/www.zhangxinxu.com <\/span>\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u7ecf\u5e38\u4f7f\u7528\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u539f\u7406\u5f88\u7b80\u5355\uff0c\u5f53\u6d4f\u89c8\u5668\u4e0d\u652f\u6301\u67d0\u4e2aCSS\u5c5e\u6027\u7684\u65f6\u5019\uff0c\u5c31\u7b97\u4f60\u5f3a\u5236\u8bbe\u7f6e\uff0c\u83b7\u53d6\u7684\u8ba1\u7b97\u503c\u4e5f\u4e0d\u4f1a\u662f\u4f60\u8bbe\u7f6e\u7684\u5c5e\u6027\u503c\uff0c\u8fd8\u662f\u68c0\u6d4b\u6d4f\u89c8\u5668\u662f\u5426\u652f\u6301CSS filter\u6ee4\u955c\u7684\u4f8b\u5b50\u3002<\/p>\n<p>\u6211\u4f1a\u8fd9\u4e48\u5904\u7406\uff1a<\/p>\n<pre>document.head.style.filter = 'blur(5px)';\r\nresult = window.getComputedStyle(document.head).filter == 'blur(5px)';<\/pre>\n<p>\u6211\u4eec\u770b\u4e0b\u4e0a\u9762\u4ee3\u7801\u5728\u5b9e\u9645\u6d4f\u89c8\u5668\u4e2d\u7684\u8868\u73b0\uff0c\u9996\u5148\u662f\u652f\u6301\u7684Chrome\u6d4f\u89c8\u5668\uff0c<code>result<\/code>\u7684\u503c\u662f<code>true<\/code>\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201911\/2019-11-08_002701.png\" width=\"533\" height=\"124\" alt=\"Chrome\u6d4f\u89c8\u5668\u4e0b\u622a\u56fe\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u7136\u540e\u662f\u4e0d\u652f\u6301\u7684IE11\u6d4f\u89c8\u5668\uff0c<code>result<\/code>\u7684\u503c\u662f<code>false<\/code>\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201911\/2019-11-08_002602.png\" width=\"542\" height=\"171\" alt=\"IE11\u4e0b\u7684\u622a\u56fe\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u6b64\u65b9\u6cd5\u539f\u7406\u5f88\u7b80\u5355\uff0c\u517c\u5bb9\u6027\u4e5f\u975e\u5e38\u597d\uff0c\u5b9e\u73b0\u7684\u5173\u952e\u70b9\u5c31\u662f\u4f7f\u7528<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/2012\/05\/getcomputedstyle-js-getpropertyvalue-currentstyle\/\">getComputedStyle\u8fd9\u4e2aIE9+\u652f\u6301\u7684DOM API\u65b9\u6cd5<\/a>\uff0c\u4e0d\u80fd\u4f7f\u7528<code>dom.style.xxxx<\/code>\u76f4\u63a5\u83b7\u53d6\u3002<\/p>\n<p>\u5982\u679c\u662f\u60f3\u8981\u5b9e\u73b0\u7c7b\u4f3cor\u6216\u8005and\u903b\u8f91\uff0c\u4f8b\u5982\u5e26\u79c1\u6709\u524d\u7f00\u90a3\u79cd\uff0c\u53ef\u4ee5\u4e0b\u9762\u8fd9\u6837\u5904\u7406\uff0c\u6b63\u5219\u5339\u914d\u5173\u952e\u7684\u90e8\u5206\uff1a<\/p>\n<pre>document.head.style.width= 'fit-content';\r\ndocument.head.style.width= '-moz-fit-content';\r\nresult = \/fit-content\/.test(window.getComputedStyle(document.head).width);<\/pre>\n<p>\u4f8b\u5982Firefox\u6d4f\u89c8\u5668\u4e0b\u7684\u7ed3\u679c\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201911\/2019-11-08_004646.png\" width=\"516\" height=\"98\" alt=\"Firefox\u6d4f\u89c8\u5668\u4e0b\u622a\u56fe\" class=\"alignnone size-medium\" \/><\/p>\n<h4>\u517c\u5bb9\u6027<\/h4>\n<p>IE9+<\/p>\n<h4>\u6ce8\u610f\u70b9<\/h4>\n<p><code>getComputedStyle()<\/code>\u65b9\u6cd5\u8fd4\u56de\u7684\u662f\u8ba1\u7b97\u503c\uff0c\u5f88\u591a\u65f6\u5019\u548c\u8bbe\u7f6e\u7684\u5c5e\u6027\u503c\u5e76\u4e0d\u540c\u3002<\/p>\n<p>\u4f8b\u5982\u8bbe\u7f6e\u884c\u9ad8\u4e3a\u5c0f\u6570\uff0c\u5728IE\u6d4f\u89c8\u5668\u4e0b\u8fd4\u56de\u5c31\u662fpx\u8ba1\u7b97\u503c\u3002<\/p>\n<p>\u53c8\u6216\u8005\u8bbe\u7f6e<code>background<\/code>\u5c5e\u6027\u503c\uff0c\u7ed3\u679c\u8fd4\u56de\u7684\u662fbackground\u5144\u5f1f\u59d0\u59b9\u4e00\u5927\u5bb6\u5b50\u503c\uff1a<\/p>\n<pre>document.head.style.background = 'paint(abc)';\r\nresult = \/paint\/.test(window.getComputedStyle(document.head).background);\r\n<span style=\"color:green;\">\/\/ reseult\u503c\u662ftrue<\/span>\r\nwindow.getComputedStyle(document.head).background\r\n<span style=\"color:green;\">\/\/ \"rgba(0, 0, 0, 0) paint(abc) repeat scroll 0% 0% \/ auto padding-box border-box\"<\/span><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201911\/2019-11-08_005025.png\" width=\"585\" height=\"154\" alt=\"\u8fd4\u56de\u7684\u662f\u96c6\u5408\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u9700\u8981\u4f7f\u7528\u6a21\u7cca\u5339\u914d\u624d\u884c\u3002<\/p>\n<h3>\u4e09\u3001\u5176\u4ed6\u65b9\u6cd5<\/h3>\n<p>\u6838\u5fc3\u601d\u60f3\u7c7b\u4f3c\uff1a<\/p>\n<pre>document.head.setAttribute('style', 'filter: blur(5px)');\r\nresult = !!document.head.style.filter;<\/pre>\n<p>Chrome\u548cIE\u4e0b\u9a8c\u8bc1\u68c0\u6d4b\u7ed3\u679c\u56fe\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201911\/2019-11-08_005507.png\" width=\"434\" height=\"129\" alt=\"Chrome\u4e0b\" class=\"alignnone size-medium\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201911\/2019-11-08_005440.png\" width=\"435\" height=\"153\" alt=\"IE\u6d4f\u89c8\u5668\u4e0b\" class=\"alignnone size-medium\" \/><\/p>\n<h4>\u603b\u7ed3\u4e00\u4e0b<\/h4>\n<p>\u4e0d\u8003\u8651\u517c\u5bb9\u6027\uff0c\u5bf9CSS\u7684\u8fdb\u884c\u68c0\u6d4b\u4f7f\u7528<code>CSS.supports()<\/code>\u65b9\u6cd5\uff0c\u8981\u68c0\u6d4bIE\u6d4f\u89c8\u5668\u4f7f\u7528\u8d4b\u503c\u53d6\u503c\u6cd5\u3002<\/p>\n<p>\u6b22\u8fce\u8865\u5145\u5176\u4ed6\u65b9\u6cd5\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/emtion\/emoji\/1f61c.svg\" width=\"20\" height=\"20\" align=\"absmiddle\"><\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u6b22\u8fce\u5206\u4eab\uff0c\u52ff\u5168\u6587\u8f6c\u8f7d\uff0c\u5982\u679c\u5185\u5bb9\u4f60\u5b9e\u5728\u559c\u6b22\uff0c\u53ef\u4ee5\u52a0\u5165\u6536\u85cf\u5939\uff0c\u6c38\u4e0d\u8fc7\u671f\uff0c\u800c\u4e14\u8fd8\u4f1a\u53ca\u65f6\u66f4\u65b0\u77e5\u8bc6\u70b9\u4ee5\u53ca\u4fee\u6b63\u9519\u8bef\uff0c\u9605\u8bfb\u4f53\u9a8c\u4e5f\u66f4\u597d\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=9051\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=9051<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201911\/pandeng.png\" width=\"161\" height=\"100\" alt=\"\u6211\u6240\u77e5\u9053\u7684\u51e0\u79cd\u65b9\u6cd5\" class=\"alignright size-medium\" \/><\/p>\n<p>CSS.supports()\u662f\u4e00\u79cd\u975e\u5e38\u4e0d\u9519\u539f\u751f\u65b9\u6cd5\uff0c\u4f46IE11\u53ca\u5176\u4ee5\u4e0b\u6d4f\u89c8\u5668\u4e0d\u517c\u5bb9\uff0c\u6b64\u65f6\u53ef\u4ee5\u8bd5\u8bd5\u5148\u8d4b\u503c\u5728\u53d6\u503c\u7684\u68c0\u6d4b\u65b9\u6cd5\uff0c\u8bb0\u4f4fgetComputedStyle\u65b9\u6cd5\u6216\u8005setAttribute style\u5c5e\u6027\u8d4b\u503c\uff0c\u90fd\u662f\u53ef\u4ee5\u7684\uff0c\u662f\u6211\u5b9e\u9645\u5f00\u53d1\u7ecf\u5e38\u4f7f\u7528\u7684\u65b9\u6cd5\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[180],"tags":[1012,1507,1506,1271,1334,1059,1508,1370],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/9051"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=9051"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/9051\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=9051"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=9051"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=9051"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}