{"id":12481,"date":"2026-11-06T17:51:24","date_gmt":"2026-11-06T09:51:24","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=12481"},"modified":"2026-11-06T17:51:24","modified_gmt":"2026-11-06T09:51:24","slug":"js-drag-set-pointer-capture","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2026\/11\/js-drag-set-pointer-capture\/","title":{"rendered":"JS\u62d6\u62fd\u4ea4\u4e92\u5fc5\u7528setPointerCapture\u65b9\u6cd5"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=12481\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=12481<\/a><br \/>\n\u672c\u6587\u53ef\u5168\u6587\u8f6c\u8f7d\uff0c\u4f46\u9700\u8981\u4fdd\u7559\u539f\u4f5c\u8005\u3001\u51fa\u5904\u4ee5\u53ca\u6587\u4e2d\u94fe\u63a5\uff0cAI\u6293\u53d6\u4fdd\u7559\u539f\u6587\u5730\u5740\uff0c\u4efb\u4f55\u7f51\u7ad9\u5747\u53ef\u6458\u8981\u805a\u5408\uff0c\u5546\u7528\u8bf7\u8054\u7cfb\u6388\u6743\u3002<\/p>\n<h3>\u4e00\u3001\u8fd8\u5728mousedown\u4e2a\u68d2\u69cc<\/h3>\n<p>\u5173\u4e8e\u62d6\u62fd\u7684\u6587\u7ae0\uff0c\u6211\u4e4b\u524d\u5199\u8fc7\u81f3\u5c11\u4e24\u4e09\u7bc7\uff0c\u4e4b\u524d\u5b9e\u73b0\u903b\u8f91\u90fd\u662f\u8fd9\u6837\u3002<\/p>\n<p>\u4e00\u4e2a\u62d6\u62fd\u6807\u5fd7\u91cf\uff0c\u4e00\u4e2a\u5b58\u50a8\u6570\u636e\u7684\u53d8\u91cf\u3002<\/p>\n<p><code>mousedown<\/code>\u540e\u6807\u5fd7\u91cf\u5f00\u542f\uff0c\u5b58\u50a8\u521d\u59cb\u6570\u636e\uff0c<br \/>\n\u7136\u540e\u5728<code>document<\/code>\u6587\u6863\u4e0a\u7ed1\u5b9a<code>mousemove<\/code>\uff0c\u5b9e\u65f6\u8ba1\u7b97\u4f4d\u7f6e\u3002<br \/>\n\u7b49<code>mouseup<\/code>\u540e\u518d\u6062\u590d\u548c\u5904\u7406\u3002<\/p>\n<p>\u7528\u4e86\u5f88\u591a\u5e74\uff0c\u81ea\u6211\u611f\u89c9\u4e5f\u633a\u597d\u7684\uff0c\u773c\u775b\u95ed\u7740\u90fd\u80fd\u624b\u5199\u51fa\u6765\u3002<\/p>\n<p>\u5b9e\u9645\u4e0a\uff0c\u4e0a\u9762\u8fd9\u5957\u5b9e\u73b0\u5df2\u7ecf\u8fc7\u65f6\u4e86\uff0c\u4e14\u8fc7\u65f6\u5f88\u4e45\u4e86\u3002<\/p>\n<p>\u73b0\u5728\u8fd8\u5728\u4f7f\u7528<code>mousedown<\/code>\u5b9e\u73b0\u62d6\u62fd\u6548\u679c\uff0c\u90a3\u5c31\u662f\u4e2a\u68d2\u69cc\u4e86\u3002<\/p>\n<p>\u5e94\u8be5\u4f7f\u7528<code>pointerdown<\/code>\u3001<code>pointerup<\/code>\u8fd9\u7c7b\u4e8b\u4ef6\uff08PC\u3001Mobile\u53cc\u901a\uff09\uff0c\u540c\u65f6\uff0c\u79fb\u52a8\u7684\u4e8b\u4ef6\u4e5f\u4e0d\u9700\u8981\u7ed1\u5b9a\u5728<code>document<\/code>\u6216\u8005<code>&lt;body&gt;<\/code>\u5143\u7d20\u4e0a\u4e86\uff0c\u76f4\u63a5\u5199\u5728\u89e6\u53d1\u5143\u7d20\u4e0a\u5373\u53ef\uff01<\/p>\n<p>\u6709\u4eba\u4f1a\u95ee\u4e86\uff0c\u8981\u662f\u62d6\u52a8\u592a\u5feb\uff0c\u9f20\u6807\u4ece\u89e6\u53d1\u5143\u7d20\u4e0a\u79fb\u51fa\uff0c\u5c82\u4e0d\u662f<code>pointermove<\/code>\u4e8b\u4ef6\u4e0d\u4f1a\u89e6\u53d1\uff1f<\/p>\n<p>\u662f\u6709\u8fd9\u4e2a\u95ee\u9898\uff0c\u4e0d\u8fc7\uff0c\u6b64\u4e8b\u4ef6\u5728\u8bbe\u8ba1\u7684\u65f6\u5019\u5c31\u8003\u8651\u5230\u8fd9\u79cd\u573a\u666f\u4e86\uff0c\u4e8e\u662f\u8bbe\u8ba1\u4e86<code>setPointerCapture<\/code>\u65b9\u6cd5\uff0c\u4e13\u95e8\u9488\u5bf9\u6b64\u95ee\u9898\u3002<\/p>\n<h3>\u4e8c\u3001setPointerCapture\u4e0e\u62d6\u62fd<\/h3>\n<p><code>setPointerCapture<\/code>\u53ef\u4ee5\u6355\u83b7\u4e8b\u4ef6\u7684<code>pointerId<\/code>\uff0c\u5728\u9f20\u6807\u6216\u624b\u52bf\u91ca\u653e\u4e4b\u524d\uff0c\u540e\u7eed\u6240\u6709\u4e8b\u4ef6\u90fd\u5c06\u4ee5\u8be5\u6355\u83b7\u5143\u7d20\u4e3a\u76ee\u6807\u3002<\/p>\n<p>\u6240\u4ee5\u62d6\u62fd\u7684\u65f6\u5019\uff0c\u4e0d\u8981\u62c5\u5fc3\u76ee\u6807\u5143\u7d20\u4f1a\u4e22\u5931\u3002<\/p>\n<p>\u773c\u89c1\u4e3a\u5b9e\uff0c\u76f4\u63a5\u770b\u6848\u4f8b\uff0c\u5047\u8bbe\u4e00\u4e2a\u7403\u662fball\uff1a<\/p>\n<pre>&lt;div class=\"ball\" id=\"ball\"&gt;&lt;\/div&gt;<\/pre>\n<p>\u90a3\u4e48\u6240\u6709\u62d6\u62fd\u4e8b\u4ef6\u90fd\u53ef\u4ee5\u7ed1\u5b9a\u5728 ball \u5143\u7d20\u4e0a\uff0c\u901a\u8fc7<code>setPointerCapture<\/code>\u6355\u83b7\uff0c\u4e0d\u8981\u62c5\u5fc3\u62d6\u62fd\u884c\u4e3a\u90a3\u4e48\u5bb9\u6613\u4e22\u5931\u3002<\/p>\n<p>JS\u4ee3\u7801\u53c2\u8003\u793a\u610f\uff1a<\/p>\n<pre>let dragging = false;\r\nlet offsetX = 0;\r\nlet offsetY = 0;\r\n\r\nball.addEventListener(\"pointerdown\", function (event) {\r\n    dragging = true;\r\n    \/\/ \u6700\u5173\u952e\u4ee3\u7801\u5c31\u662f\u6211\r\n    ball.setPointerCapture(event.pointerId);\r\n    ...\r\n});\r\n\r\nball.addEventListener(\"pointermove\", function (event) {\r\n    if (dragging == false) {\r\n        return;\r\n    }\r\n    ...\r\n});\r\n\r\nconst stopDrag = function (event) {\r\n    dragging = false;\r\n    if (ball.hasPointerCapture(event.pointerId)) {\r\n        ball.releasePointerCapture(event.pointerId);\r\n    }\r\n};\r\n\r\nball.addEventListener(\"pointerup\", stopDrag);\r\nball.addEventListener(\"pointercancel\", stopDrag);\r\nball.addEventListener(\"lostpointercapture\", stopDrag);<\/pre>\n<p>\u9f20\u6807\u6216\u624b\u6307\u6309\u4e0b\u7684\u65f6\u5019<code>setPointerCapture<\/code>\uff0c\u62ac\u8d77\u7684\u65f6\u5019\u91ca\u653e<code>releasePointerCapture<\/code>\uff0c\u5c31\u53ef\u4ee5\u5b8c\u6210\u4f53\u9a8c\u6d41\u7545\u7684\u62d6\u62fd\u6548\u679c\u5566\u3002<\/p>\n<p>\u773c\u89c1\u4e3a\u5b9e\uff0c\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/study\/202611\/js-pointer-events-capture-demo.php\" rel=\"noopener\" target=\"_blank\">setPointerCapture\u5b9e\u73b0\u5c0f\u7403\u62d6\u62fddemo<\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202611\/2026-11-6_172920.png\" width=\"463\" height=\"299\" alt=\"\u5c0f\u7403\u62d6\u62fd\u622a\u56fe\" class=\"imgpad size-medium\" \/><\/p>\n<p>\u6f14\u793a\u6548\u679c\u662fAI\u751f\u6210\u7684\uff0c\u867d\u7136\u4e0d\u662f\u6211\u7684UI\u7f8e\u5b66\u98ce\u683c\uff0c\u4f46\u662f\u7b97\u4e86\uff0c\u7701\u5fc3\u554a\uff0c\u563f\u563f\ud83d\ude0b\u3002<\/p>\n<h3>\u4e09\u3001\u5728\u5520\u53e8\u70b9\u4ec0\u4e48\uff0c\u6c34\u4e9b\u5b57\u6570<\/h3>\n<p>\u54ce\u5440\uff0c\u5185\u5bb9\u6709\u70b9\u5c11\u4e86\uff0c\u518d\u6c34\u4e9b\u5185\u5bb9\u5427\u3002<\/p>\n<p>\u8bf4\u5230Pointer\uff0c\u6211\u4e0d\u81ea\u89c9\u53c8\u8054\u60f3\u5230\u4e86<code>requestPointerLock<\/code>\uff0c\u8fd9\u4e2a\u662f\u7528\u6765\u5b9e\u73b0\u65e0\u9650\u6eda\u52a8\u7684\uff0c\u5c5e\u4e8eJS Pointer Lock\u7684\u5185\u5bb9\uff0c\u4e4b\u524d\u5199\u6587\u7ae0\u4ecb\u7ecd\u8fc7\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/2017\/10\/js-api-pointer-lock\/\">\u9f20\u6807\u65e0\u9650\u79fb\u52a8 JS API Pointer Lock\u7b80\u4ecb<\/a><\/p>\n<p>CSS\u7684<code>pointer-events:none<\/code>\u53ef\u4ee5\u5e72\u6389JS <code>pointer<\/code>\u76f8\u5173\u7684\u4e8b\u4ef6\uff0c\u4e0d\u8fc7\u5374\u963b\u6b62\u4e0d\u4e86<code>focus<\/code>\u4ea4\u4e92\u3002<\/p>\n<p>\u8fd8\u6709\u5565\uff0c\u597d\u6709\u5565\uff0c\u6211\u60f3\u60f3\u2026\u2026<\/p>\n<p>\u54e6\uff0c\u8fd8\u6709\u4e2a<code>touch-action<\/code>\uff0c\u4e0e\u89e6\u6478\u6709\u5173\u7684\uff0c\u53ef\u4ee5\u963b\u6b62\u6eda\u52a8\u5565\u7684\uff0c\u53ef\u4ee5\u9632\u6b62\u81ea\u603c\u62d6\u62fd\u548c\u539f\u751f\u6eda\u52a8\u51b2\u7a81\u3002<\/p>\n<p>\u7136\u540e\u8001\u7684<code>setCapture()<\/code>\u65b9\u6cd5\u5df2\u7ecf\u5e9f\u5f03\u4e86\uff0c\u4e07\u4e0d\u53ef\u4f7f\u7528\u3002<\/p>\n<p>\u5662\u5566\uff0c\u5dee\u4e0d\u591a\u5c31\u8fd9\u4e48\u591a\u4e86\u3002<\/p>\n<p>\u611f\u8c22\u5927\u5bb6\u7684\u9605\u8bfb\uff0c\u5468\u672b\u6109\u5feb\uff0c\u795d\u6211\u7206\u62a4\u3002<\/p>\n<p>\u8fd9\u662f\u6211\u4e0a\u5468\u672b\u9493\u7684\uff0c168\u4e94\u5c0f\u65f6\uff0c\u9493\u5f97\u90fd\u94fa\u51fa\u6765\u4e86\uff0c\ud83d\ude01\ud83d\ude01\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202611\/2026-11-6_174024.webp\" width=\"203\" height=\"360\" alt=\"\u9cca\u9c7c\u7206\u62a4\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u4f1a\u7ecf\u5e38\u66f4\u65b0\u77e5\u8bc6\u70b9\u4ee5\u53ca\u4fee\u6b63\u4e00\u4e9b\u9519\u8bef\uff0c\u56e0\u6b64\u8f6c\u8f7d\u8bf7\u4fdd\u7559\u539f\u51fa\u5904\uff0c\u65b9\u4fbf\u6eaf\u6e90\uff0c\u907f\u514d\u9648\u65e7\u9519\u8bef\u77e5\u8bc6\u7684\u8bef\u5bfc\uff0c\u540c\u65f6\u6709\u66f4\u597d\u7684\u9605\u8bfb\u4f53\u9a8c\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=12481\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=12481<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202611\/2026-11-6_174901.webp\" width=\"160\" height=\"91\" alt=\"\u62d6\u62fd\u5c01\u9762\u56fe\" class=\"alignright size-medium\" loading=\"lazy\" \/><\/p>\n<p>\u4e4b\u524d\u81ea\u5b9a\u4e49\u62d6\u62fd\u884c\u4e3a\u7684\u65f6\u5019\uff0c\u6211\u90fd\u662f\u5c06move\u884c\u4e3a\u7ed1\u5b9a\u5728document\u4e0a\u7684\uff0c\u6700\u8fd1\u624d\u77e5\u9053\uff0c\u539f\u6765\u8fd8\u6709\u4e2asetPointerCapture\u65b9\u6cd5\uff0c\u76f4\u63a5\u7ed1\u5b9a\u5728\u89e6\u53d1\u5143\u7d20\u4e0a\uff0c\u4e0d\u5fc5\u62c5\u5fc3\u5feb\u901f\u79fb\u52a8\u65f6\u5019\u76ee\u6807\u5bf9\u8c61\u7684\u4e22\u5931\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1284],"tags":[870,2180,661,2181,2182,1174,2177,1276],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/12481"}],"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=12481"}],"version-history":[{"count":4,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/12481\/revisions"}],"predecessor-version":[{"id":12509,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/12481\/revisions\/12509"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=12481"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=12481"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=12481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}