{"id":235218,"date":"2024-05-12T10:16:08","date_gmt":"2024-05-12T02:16:08","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/235218.html"},"modified":"2024-05-12T10:16:18","modified_gmt":"2024-05-12T02:16:18","slug":"javascript%e5%a6%82%e4%bd%95%e5%85%b3%e9%97%ad%e6%89%8b%e6%9c%ba%e7%bd%91%e9%a1%b5%e5%8f%8c%e6%8c%87%e8%a7%a6%e5%b1%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/235218.html","title":{"rendered":"javascript\u5982\u4f55\u5173\u95ed\u624b\u673a\u7f51\u9875\u53cc\u6307\u89e6\u5c4f"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26234149\/f00dfdac-c678-4829-b6dd-6a001bb7efc4.webp\" alt=\"javascript\u5982\u4f55\u5173\u95ed\u624b\u673a\u7f51\u9875\u53cc\u6307\u89e6\u5c4f\" \/><\/p>\n<p><p>\u624b\u673a\u7f51\u9875\u4e0a\u7684\u53cc\u6307\u89e6\u5c4f\u7f29\u653e\u662f\u4e00\u79cd\u4ea4\u4e92\u529f\u80fd\uff0c\u901a\u5e38\u7528\u4e8e\u653e\u5927\u6216\u7f29\u5c0f\u9875\u9762\u5185\u5bb9\u3002\u5728JavaScript\u4e2d\uff0c<strong>\u7981\u7528\u53cc\u6307\u89e6\u5c4f\u7f29\u653e<\/strong>\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\u4e24\u79cd\uff1a<strong>\u901a\u8fc7meta\u6807\u7b7e\u8bbe\u7f6e\u7528\u6237\u89c6\u53e3\u5c5e\u6027\u3001\u5728JavaScript\u4e2d\u6355\u6349\u548c\u963b\u6b62touch\u4e8b\u4ef6\u7684\u9ed8\u8ba4\u884c\u4e3a<\/strong>\u3002\u5982\u679c\u9700\u8981\u66f4\u8be6\u7ec6\u5730\u5e94\u7528JavaScript\u6765\u5173\u95ed\u53cc\u6307\u89e6\u63a7\u7f29\u653e\uff0c\u53ef\u4ee5\u5728\u9875\u9762\u521d\u59cb\u5316\u65f6\u901a\u8fc7\u52a8\u6001\u6dfb\u52a0\u89e6\u6478\u4e8b\u4ef6\u76d1\u542c\u5668\u6765\u5b9e\u73b0\uff0c\u4ece\u800c\u963b\u6b62\u4e24\u4e2a\u624b\u6307\u7684\u89e6\u6478\u64cd\u4f5c\u5e26\u6765\u7684\u9ed8\u8ba4\u7f29\u653e\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001META\u6807\u7b7e\u7981\u7528\u7f29\u653e<\/h3>\n<\/p>\n<p><p>\u5728HTML\u7684<code>&lt;head&gt;<\/code>\u90e8\u5206\uff0c\u53ef\u4ee5\u76f4\u63a5\u6dfb\u52a0\u4e00\u4e2a\u540d\u4e3a<code>viewport<\/code>\u7684meta\u6807\u7b7e\u6765\u63a7\u5236\u89c6\u53e3\u7684\u884c\u4e3a\uff0c\u7981\u7528\u7528\u6237\u7684\u7f29\u653e\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no&quot;&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u884c\u4ee3\u7801\u4f1a\u5c06\u6700\u5927\u7f29\u653e\u6bd4\u4f8b\u8bbe\u7f6e\u4e3a1\uff0c\u5e76\u4e14\u5c06\u7528\u6237\u7684\u53ef\u7f29\u653e\u6027\u8bbe\u4e3a\u4e0d\u5141\u8bb8\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5728\u52a0\u8f7d\u9875\u9762\u65f6\u76f4\u63a5\u7981\u6b62\u53cc\u6307\u89e6\u5c4f\u7f29\u653e\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001JAVASCRIPT\u4e2d\u76d1\u542cTOUCH\u4e8b\u4ef6<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u7528\u5230JavaScript\u4ee3\u7801\u6765\u63a7\u5236\uff0c\u4e00\u4e2a\u5e38\u89c1\u7684\u505a\u6cd5\u662f\u76d1\u542ctouch\u4e8b\u4ef6\uff0c\u7136\u540e\u963b\u6b62\u5176\u9ed8\u8ba4\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><h4>1. \u6dfb\u52a0\u4e8b\u4ef6\u76d1\u542c\u5668<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4e3a<code>document<\/code>\u5bf9\u8c61\u6216\u7279\u5b9a\u7684DOM\u5143\u7d20\u6dfb\u52a0<code>touchstart<\/code>\u548c<code>touchmove<\/code>\u4e8b\u4ef6\u7684\u76d1\u542c\u5668\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">document.addEventListener(&#039;touchstart&#039;, preventZoom, {passive: false});<\/p>\n<p>document.addEventListener(&#039;touchmove&#039;, preventZoom, {passive: false});<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5b9a\u4e49\u963b\u6b62\u7f29\u653e\u7684\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u5728\u5b9a\u4e49\u7684\u4e8b\u4ef6\u5904\u7406\u51fd\u6570<code>preventZoom<\/code>\u4e2d\uff0c\u4f1a\u5224\u65ad\u89e6\u6478\u70b9\u7684\u6570\u91cf\u3002\u5982\u679c\u662f\u4e24\u4e2a\u6216\u4e24\u4e2a\u4ee5\u4e0a\u7684\u89e6\u6478\u70b9\uff0c\u901a\u5e38\u8868\u793a\u7528\u6237\u60f3\u8981\u6267\u884c\u7f29\u653e\u64cd\u4f5c\uff0c\u6b64\u65f6\u53ef\u4ee5\u8c03\u7528\u4e8b\u4ef6\u5bf9\u8c61\u7684<code>preventDefault<\/code>\u65b9\u6cd5\u6765\u963b\u6b62\u8fd9\u4e00\u9ed8\u8ba4\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function preventZoom(e) {<\/p>\n<p>    if (e.touches.length &gt; 1) {<\/p>\n<p>        e.preventDefault();<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u7ed3\u5408\u4ee5\u4e0a\u4e24\u79cd\u65b9\u6cd5<\/strong>\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u7981\u7528\u79fb\u52a8\u7f51\u9875\u4e2d\u7684\u53cc\u6307\u89e6\u5c4f\u7f29\u653e\u529f\u80fd\u3002\u4e0d\u8fc7\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u7981\u7528\u7528\u6237\u7684\u7f29\u653e\u53ef\u80fd\u4f1a\u5f71\u54cd\u5230\u7f51\u9875\u7684\u53ef\u8bbf\u95ee\u6027\uff0c\u56e0\u6b64\u5728\u51b3\u5b9a\u662f\u5426\u7981\u7528\u53cc\u6307\u89e6\u63a7\u529f\u80fd\u65f6\u5e94\u614e\u91cd\u8003\u8651\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u76f8\u5173\u5b9e\u8df5\uff0c\u5e76\u89e3\u6790\u5728\u4e0d\u540c\u60c5\u666f\u4e0b\u5982\u4f55\u4f18\u96c5\u5730\u5e94\u7528\u8fd9\u4e9b\u7b56\u7565\uff0c\u786e\u4fdd\u4e0d\u4ec5\u7528\u6237\u4f53\u9a8c\u5f97\u5230\u4fdd\u969c\uff0c\u540c\u65f6\u7f51\u7ad9\u7684\u529f\u80fd\u4e5f\u5f97\u5230\u5168\u9762\u7684\u6ee1\u8db3\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u7981\u7528\u624b\u673a\u7f51\u9875\u7684\u53cc\u6307\u89e6\u5c4f\u64cd\u4f5c\uff1f<\/strong><\/p>\n<p>\u7981\u7528\u624b\u673a\u7f51\u9875\u7684\u53cc\u6307\u89e6\u5c4f\u64cd\u4f5c\u53ef\u4ee5\u901a\u8fc7JavaScript\u5b9e\u73b0\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5c06\u89e6\u6478\u4e8b\u4ef6\u7981\u7528\u6216\u4fee\u6539\u4e3a\u5176\u4ed6\u64cd\u4f5c\uff1a<\/p>\n<pre><code class=\"language-javascript\">document.addEventListener(&quot;touchstart&quot;, function(event) {\n  if (event.touches.length &gt; 1) {\n    event.preventDefault(); \/\/ \u963b\u6b62\u53cc\u6307\u89e6\u5c4f\u4e8b\u4ef6\n    \/\/ \u5728\u6b64\u5904\u6dfb\u52a0\u60a8\u7684\u81ea\u5b9a\u4e49\u64cd\u4f5c\uff0c\u4f8b\u5982\u663e\u793a\u63d0\u793a\u4fe1\u606f\u6216\u6267\u884c\u5176\u4ed6\u903b\u8f91\n  }\n}, { passive: false });\n\ndocument.addEventListener(&quot;gesturestart&quot;, function(event) {\n  event.preventDefault(); \/\/ \u963b\u6b62\u624b\u52bf\u7f29\u653e\u4e8b\u4ef6\n  \/\/ \u5728\u6b64\u5904\u6dfb\u52a0\u60a8\u7684\u81ea\u5b9a\u4e49\u64cd\u4f5c\uff0c\u4f8b\u5982\u663e\u793a\u63d0\u793a\u4fe1\u606f\u6216\u6267\u884c\u5176\u4ed6\u903b\u8f91\n}, { passive: false });\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u5f53\u7528\u6237\u5c1d\u8bd5\u4f7f\u7528\u53cc\u6307\u89e6\u5c4f\u64cd\u4f5c\u65f6\uff0c\u6d4f\u89c8\u5668\u5c06\u4e0d\u4f1a\u6267\u884c\u9ed8\u8ba4\u7684\u7f29\u653e\u884c\u4e3a\uff0c\u5e76\u4e14\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u6dfb\u52a0\u81ea\u5df1\u7684\u903b\u8f91\u3002<\/p>\n<p><strong>2. \u600e\u4e48\u4f7f\u7528JavaScript\u7981\u6b62\u624b\u673a\u7f51\u9875\u7684\u53cc\u6307\u7f29\u653e\uff1f<\/strong><\/p>\n<p>\u8981\u7981\u6b62\u624b\u673a\u7f51\u9875\u7684\u53cc\u6307\u7f29\u653e\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574viewport\u7684\u7f29\u653e\u7ea7\u522b\u6765\u5b9e\u73b0\u3002\u901a\u8fc7\u5c06<code>meta<\/code>\u6807\u7b7e\u7684<code>content<\/code>\u5c5e\u6027\u8bbe\u7f6e\u4e3a\u4e00\u4e2a\u56fa\u5b9a\u7684\u503c\uff0c\u53ef\u4ee5\u963b\u6b62\u7f51\u9875\u88ab\u7f29\u653e\u3002<\/p>\n<pre><code class=\"language-html\">&lt;meta name=&quot;viewport&quot; content=&quot;user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width&quot;&gt;\n<\/code><\/pre>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u4e2d\u7684<code>user-scalable=no<\/code>\u5c5e\u6027\u5c06\u7981\u7528\u7528\u6237\u5bf9\u9875\u9762\u8fdb\u884c\u7f29\u653e\u3002\u8fd9\u6837\uff0c\u65e0\u8bba\u7528\u6237\u5982\u4f55\u5c1d\u8bd5\u53cc\u6307\u7f29\u653e\uff0c\u9875\u9762\u90fd\u5c06\u4fdd\u6301\u4e0d\u53d8\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u4f7f\u7528JavaScript\u9650\u5236\u624b\u673a\u7f51\u9875\u7684\u53cc\u6307\u89e6\u5c4f\u64cd\u4f5c\uff1f<\/strong><\/p>\n<p>\u60f3\u8981\u9650\u5236\u624b\u673a\u7f51\u9875\u7684\u53cc\u6307\u89e6\u5c4f\u64cd\u4f5c\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528JavaScript\u6765\u8fc7\u6ee4\u6216\u62e6\u622a\u89e6\u6478\u4e8b\u4ef6\u3002\u901a\u8fc7\u5224\u65ad\u89e6\u6478\u4e8b\u4ef6\u4e2d\u7684\u89e6\u6478\u70b9\u6570\u91cf\uff0c\u60a8\u53ef\u4ee5\u51b3\u5b9a\u662f\u5426\u5141\u8bb8\u6216\u7981\u6b62\u53cc\u6307\u64cd\u4f5c\u3002<\/p>\n<pre><code class=\"language-javascript\">document.addEventListener(&quot;touchstart&quot;, function(event) {\n  if (event.touches.length &gt; 1) {\n    event.preventDefault(); \/\/ \u963b\u6b62\u53cc\u6307\u89e6\u5c4f\u4e8b\u4ef6\n    \/\/ \u5728\u6b64\u5904\u6dfb\u52a0\u60a8\u7684\u81ea\u5b9a\u4e49\u64cd\u4f5c\uff0c\u4f8b\u5982\u663e\u793a\u63d0\u793a\u4fe1\u606f\u6216\u6267\u884c\u5176\u4ed6\u903b\u8f91\n  }\n}, { passive: false });\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u963b\u6b62\u89e6\u6478\u4e8b\u4ef6\u4e2d\u7684\u4efb\u4f55\u591a\u6307\u64cd\u4f5c\uff0c\u5e76\u5141\u8bb8\u60a8\u5728\u89e6\u53d1\u65f6\u6267\u884c\u5176\u4ed6\u64cd\u4f5c\uff0c\u5982\u63d0\u793a\u7528\u6237\u6216\u6267\u884c\u7279\u5b9a\u7684\u529f\u80fd\u3002<\/p>\n<p>\u8bf7\u6ce8\u610f\uff0c\u7531\u4e8e\u4e0d\u540c\u7684\u6d4f\u89c8\u5668\u548c\u8bbe\u5907\u53ef\u80fd\u4f1a\u6709\u4e0d\u540c\u7684\u884c\u4e3a\uff0c\u4f7f\u7528JavaScript\u7981\u6b62\u6216\u9650\u5236\u624b\u673a\u7f51\u9875\u7684\u53cc\u6307\u89e6\u5c4f\u64cd\u4f5c\u53ef\u80fd\u4f1a\u6709\u4e00\u4e9b\u517c\u5bb9\u6027\u95ee\u9898\u3002\u5efa\u8bae\u5728\u4f7f\u7528\u4e4b\u524d\u8fdb\u884c\u6d4b\u8bd5\uff0c\u5e76\u8003\u8651\u4e3a\u4e0d\u540c\u7684\u6d4f\u89c8\u5668\u6216\u8bbe\u5907\u7f16\u5199\u7279\u5b9a\u7684\u903b\u8f91\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u624b\u673a\u7f51\u9875\u4e0a\u7684\u53cc\u6307\u89e6\u5c4f\u7f29\u653e\u662f\u4e00\u79cd\u4ea4\u4e92\u529f\u80fd\uff0c\u901a\u5e38\u7528\u4e8e\u653e\u5927\u6216\u7f29\u5c0f\u9875\u9762\u5185\u5bb9\u3002\u5728JavaScript\u4e2d\uff0c\u7981\u7528\u53cc\u6307\u89e6\u5c4f\u7f29\u653e [&hellip;]","protected":false},"author":3,"featured_media":235226,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/235218"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=235218"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/235218\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/235226"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=235218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=235218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=235218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}