{"id":2143,"date":"2025-03-01T00:13:39","date_gmt":"2025-02-28T16:13:39","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=2143"},"modified":"2025-03-01T00:13:39","modified_gmt":"2025-02-28T16:13:39","slug":"javascript-window-screen-%e5%af%b9%e8%b1%a1","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/01\/javascript-window-screen-%e5%af%b9%e8%b1%a1\/","title":{"rendered":"JavaScript Window.screen \u5bf9\u8c61"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5728\u6d4f\u89c8\u5668\u4e2d\uff0c<code>screen<\/code> \u5bf9\u8c61\u63d0\u4f9b\u4e86\u5173\u4e8e\u7528\u6237\u5c4f\u5e55\u7684\u4fe1\u606f\uff0c\u6bd4\u5982\u5c4f\u5e55\u7684\u5bbd\u5ea6\u3001\u9ad8\u5ea6\u3001\u989c\u8272\u6df1\u5ea6\u7b49\u3002\u8fd9\u4e9b\u4fe1\u606f\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u6839\u636e\u7528\u6237\u5c4f\u5e55\u7684\u5927\u5c0f\u548c\u5206\u8fa8\u7387\u6765\u8c03\u6574\u7f51\u9875\u5185\u5bb9\u7684\u663e\u793a\u65b9\u5f0f\uff0c\u63d0\u5347\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>screen<\/code> \u5bf9\u8c61\u5c5e\u4e8e\u6d4f\u89c8\u5668\u5bf9\u8c61\u6a21\u578b\uff08BOM\uff09\u7684\u4e00\u90e8\u5206\uff0c\u5e76\u4e14\u662f <code>window<\/code> \u5bf9\u8c61\u7684\u4e00\u4e2a\u5c5e\u6027\uff0c\u56e0\u6b64\u4f60\u53ef\u4ee5\u901a\u8fc7 <code>window.screen<\/code> \u6765\u8bbf\u95ee\u5b83\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong><code>screen<\/code> \u5bf9\u8c61\u7684\u5c5e\u6027<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>screen.width<\/code><\/strong>\uff1a\u8fd4\u56de\u5c4f\u5e55\u7684\u5bbd\u5ea6\uff08\u4ee5\u50cf\u7d20\u4e3a\u5355\u4f4d\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  console.log(screen.width);  \/\/ \u83b7\u53d6\u5c4f\u5e55\u7684\u5bbd\u5ea6<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>screen.height<\/code><\/strong>\uff1a\u8fd4\u56de\u5c4f\u5e55\u7684\u9ad8\u5ea6\uff08\u4ee5\u50cf\u7d20\u4e3a\u5355\u4f4d\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  console.log(screen.height);  \/\/ \u83b7\u53d6\u5c4f\u5e55\u7684\u9ad8\u5ea6<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>screen.availWidth<\/code><\/strong>\uff1a\u8fd4\u56de\u5c4f\u5e55\u7684\u53ef\u7528\u5bbd\u5ea6\uff0c\u901a\u5e38\u4e0d\u5305\u62ec\u64cd\u4f5c\u7cfb\u7edf\u7684\u4efb\u52a1\u680f\u6216\u6d4f\u89c8\u5668\u5de5\u5177\u680f\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  console.log(screen.availWidth);  \/\/ \u83b7\u53d6\u5c4f\u5e55\u7684\u53ef\u7528\u5bbd\u5ea6<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>screen.availHeight<\/code><\/strong>\uff1a\u8fd4\u56de\u5c4f\u5e55\u7684\u53ef\u7528\u9ad8\u5ea6\uff0c\u901a\u5e38\u4e0d\u5305\u62ec\u64cd\u4f5c\u7cfb\u7edf\u7684\u4efb\u52a1\u680f\u6216\u6d4f\u89c8\u5668\u5de5\u5177\u680f\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  console.log(screen.availHeight);  \/\/ \u83b7\u53d6\u5c4f\u5e55\u7684\u53ef\u7528\u9ad8\u5ea6<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>screen.colorDepth<\/code><\/strong>\uff1a\u8fd4\u56de\u5c4f\u5e55\u663e\u793a\u7684\u989c\u8272\u6df1\u5ea6\uff08\u5355\u4f4d\u4e3a\u4f4d\uff09\u3002\u4f8b\u5982\uff0c24 \u8868\u793a\u6bcf\u4e2a\u50cf\u7d20\u53ef\u4ee5\u663e\u793a 24 \u4e2a\u989c\u8272\uff088 \u4f4d\u7ea2\u8272\u30018 \u4f4d\u7eff\u8272\u30018 \u4f4d\u84dd\u8272\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  console.log(screen.colorDepth);  \/\/ \u83b7\u53d6\u5c4f\u5e55\u7684\u989c\u8272\u6df1\u5ea6<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>screen.pixelDepth<\/code><\/strong>\uff1a\u8fd4\u56de\u5c4f\u5e55\u7684\u989c\u8272\u6df1\u5ea6\u7684\u53e6\u4e00\u4e2a\u5c5e\u6027\uff0c\u901a\u5e38\u4e0e <code>colorDepth<\/code> \u76f8\u540c\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  console.log(screen.pixelDepth);  \/\/ \u83b7\u53d6\u5c4f\u5e55\u7684\u50cf\u7d20\u6df1\u5ea6<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>screen.orientation<\/code><\/strong>\uff1a\u8fd4\u56de\u5c4f\u5e55\u7684\u65b9\u5411\u4fe1\u606f\uff0c\u5982 &#8220;landscape&#8221;\uff08\u6a2a\u5c4f\uff09\u6216 &#8220;portrait&#8221;\uff08\u7ad6\u5c4f\uff09\u3002\u8fd9\u4e2a\u5c5e\u6027\u5bf9\u79fb\u52a8\u8bbe\u5907\u5c24\u5176\u91cd\u8981\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  console.log(screen.orientation.type);  \/\/ \u83b7\u53d6\u5c4f\u5e55\u7684\u65b9\u5411<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong><code>screen<\/code> \u5bf9\u8c61\u7684\u5b9e\u6218\u6848\u4f8b<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u6848\u4f8b 1: \u83b7\u53d6\u5c4f\u5e55\u7684\u5c3a\u5bf8\u548c\u53ef\u7528\u7a7a\u95f4<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u7528\u6237\u7684\u5c4f\u5e55\u5c3a\u5bf8\u52a8\u6001\u5730\u8c03\u6574\u7f51\u9875\u7684\u5e03\u5c40\uff0c\u786e\u4fdd\u7f51\u9875\u5728\u5404\u79cd\u5c4f\u5e55\u5206\u8fa8\u7387\u4e0b\u90fd\u6709\u826f\u597d\u7684\u8868\u73b0\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u83b7\u53d6\u5c4f\u5e55\u7684\u5bbd\u5ea6\u548c\u9ad8\u5ea6\nconst screenWidth = screen.width;\nconst screenHeight = screen.height;\n\n\/\/ \u83b7\u53d6\u5c4f\u5e55\u7684\u53ef\u7528\u5bbd\u5ea6\u548c\u53ef\u7528\u9ad8\u5ea6\uff08\u6392\u9664\u4efb\u52a1\u680f\u7b49\u5360\u7528\u7a7a\u95f4\uff09\nconst availWidth = screen.availWidth;\nconst availHeight = screen.availHeight;\n\n\/\/ \u6253\u5370\u8fd9\u4e9b\u4fe1\u606f\nconsole.log(`Screen Width: ${screenWidth}px`);\nconsole.log(`Screen Height: ${screenHeight}px`);\nconsole.log(`Available Width: ${availWidth}px`);\nconsole.log(`Available Height: ${availHeight}px`);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u6848\u4f8b 2: \u5224\u65ad\u5c4f\u5e55\u7684\u65b9\u5411<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u53ef\u4ee5\u6839\u636e\u5c4f\u5e55\u7684\u65b9\u5411\u52a8\u6001\u6539\u53d8\u9875\u9762\u7684\u5e03\u5c40\u6216\u8bbe\u8ba1\uff0c\u4ee5\u9002\u5e94\u6a2a\u5c4f\u6216\u7ad6\u5c4f\u7684\u8bbe\u5907\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u83b7\u53d6\u5c4f\u5e55\u65b9\u5411\nconst screenOrientation = screen.orientation.type;\n\n\/\/ \u6839\u636e\u65b9\u5411\u505a\u4e0d\u540c\u7684\u5904\u7406\nif (screenOrientation === 'landscape-primary' || screenOrientation === 'landscape-secondary') {\n  console.log('Screen is in landscape mode!');\n} else if (screenOrientation === 'portrait-primary' || screenOrientation === 'portrait-secondary') {\n  console.log('Screen is in portrait mode!');\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u6848\u4f8b 3: \u6839\u636e\u5c4f\u5e55\u5927\u5c0f\u8c03\u6574\u9875\u9762\u6837\u5f0f<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u53ef\u4ee5\u4f7f\u7528\u5c4f\u5e55\u7684\u5c3a\u5bf8\u6765\u52a8\u6001\u8c03\u6574\u7f51\u9875\u7684\u6837\u5f0f\uff0c\u4f7f\u5176\u66f4\u52a0\u9002\u914d\u4e0d\u540c\u7684\u5c4f\u5e55\u5c3a\u5bf8\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u83b7\u53d6\u5c4f\u5e55\u7684\u5bbd\u5ea6\nconst screenWidth = screen.width;\n\n\/\/ \u6839\u636e\u5c4f\u5e55\u7684\u5bbd\u5ea6\u8c03\u6574\u7f51\u9875\u5185\u5bb9\nif (screenWidth &lt; 768) {\n  document.body.style.fontSize = '12px';  \/\/ \u5c0f\u5c4f\u5e55\u5b57\u4f53\u5927\u5c0f\n} else if (screenWidth &gt;= 768 &amp;&amp; screenWidth &lt; 1200) {\n  document.body.style.fontSize = '16px';  \/\/ \u4e2d\u7b49\u5c4f\u5e55\u5b57\u4f53\u5927\u5c0f\n} else {\n  document.body.style.fontSize = '20px';  \/\/ \u5927\u5c4f\u5e55\u5b57\u4f53\u5927\u5c0f\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong><code>screen<\/code> \u5bf9\u8c61\u4e0e\u6d4f\u89c8\u5668\u7a97\u53e3\u5c3a\u5bf8\u7684\u5173\u7cfb<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>screen<\/code> \u5bf9\u8c61\u63d0\u4f9b\u7684\u662f\u5c4f\u5e55\u7684\u7269\u7406\u5c3a\u5bf8\uff0c<code>window<\/code> \u5bf9\u8c61\u63d0\u4f9b\u7684\u662f\u6d4f\u89c8\u5668\u7a97\u53e3\u7684\u5c3a\u5bf8\u3002\u4e24\u8005\u7684\u533a\u522b\u5728\u4e8e\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>screen.width<\/code><\/strong> \u548c <strong><code>screen.height<\/code><\/strong> \u8fd4\u56de\u7684\u662f\u6574\u4e2a\u5c4f\u5e55\u7684\u5bbd\u9ad8\uff0c\u4e0d\u5305\u62ec\u4efb\u52a1\u680f\u6216\u5de5\u5177\u680f\u3002<\/li>\n\n\n\n<li><strong><code>window.innerWidth<\/code><\/strong> \u548c <strong><code>window.innerHeight<\/code><\/strong> \u8fd4\u56de\u7684\u662f\u6d4f\u89c8\u5668\u7684\u53ef\u89c6\u533a\u57df\u7684\u5bbd\u9ad8\uff0c\u4e0d\u5305\u62ec\u5916\u90e8\u7a97\u53e3\u7684\u88c5\u9970\u90e8\u5206\uff08\u5982\u6d4f\u89c8\u5668\u5de5\u5177\u680f\u3001\u6eda\u52a8\u6761\u7b49\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>screen<\/code> \u5bf9\u8c61\u662f JavaScript \u4e2d\u7528\u4e8e\u83b7\u53d6\u7528\u6237\u5c4f\u5e55\u4fe1\u606f\u7684\u5173\u952e\u5bf9\u8c61\uff0c\u5e38\u7528\u4e8e\u8c03\u6574\u7f51\u9875\u5e03\u5c40\u3001\u68c0\u67e5\u5c4f\u5e55\u5206\u8fa8\u7387\u548c\u65b9\u5411\u7b49\u3002\u901a\u8fc7 <code>screen<\/code> \u5bf9\u8c61\u63d0\u4f9b\u7684\u5c5e\u6027\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5b9e\u73b0\u54cd\u5e94\u5f0f\u8bbe\u8ba1\uff0c\u786e\u4fdd\u7f51\u9875\u80fd\u591f\u9002\u914d\u5404\u79cd\u8bbe\u5907\u548c\u5c4f\u5e55\u5206\u8fa8\u7387\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u638c\u63e1 <code>screen<\/code> \u5bf9\u8c61\u7684\u5e38\u7528\u5c5e\u6027\uff0c\u80fd\u5e2e\u52a9\u5f00\u53d1\u8005\u6839\u636e\u5c4f\u5e55\u5c3a\u5bf8\u3001\u53ef\u7528\u7a7a\u95f4\u4ee5\u53ca\u8bbe\u5907\u7684\u65b9\u5411\u4fe1\u606f\u8fdb\u884c\u4f18\u5316\uff0c\u63d0\u4f9b\u66f4\u597d\u7684\u7528\u6237\u4f53\u9a8c\u3002\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u6d4f\u89c8\u5668\u4e2d\uff0cscreen \u5bf9\u8c61\u63d0\u4f9b\u4e86\u5173\u4e8e\u7528\u6237\u5c4f\u5e55\u7684\u4fe1\u606f\uff0c\u6bd4\u5982\u5c4f\u5e55\u7684\u5bbd\u5ea6\u3001\u9ad8\u5ea6\u3001\u989c\u8272\u6df1\u5ea6\u7b49\u3002\u8fd9\u4e9b\u4fe1\u606f\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[],"class_list":["post-2143","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2143","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=2143"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2143\/revisions"}],"predecessor-version":[{"id":2144,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/2143\/revisions\/2144"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=2143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=2143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=2143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}