{"id":31518,"date":"2024-11-25T09:56:45","date_gmt":"2024-11-25T01:56:45","guid":{"rendered":"https:\/\/fwq.ai\/blog\/31518\/"},"modified":"2024-11-25T09:56:45","modified_gmt":"2024-11-25T01:56:45","slug":"%e6%80%8e%e6%a0%b7%e4%bd%bf%e7%94%a8vue%e6%93%8d%e4%bd%9cdiv","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/31518\/","title":{"rendered":"\u600e\u6837\u4f7f\u7528Vue\u64cd\u4f5cDIV"},"content":{"rendered":"<p style=\"text-align: left\">\u8fd9\u6b21\u7ed9\u5927\u5bb6\u5e26\u6765\u600e\u6837\u4f7f\u7528Vue\u64cd\u4f5cDIV\uff0c\u4f7f\u7528Vue\u64cd\u4f5cDIV\u7684\u6709\u54ea\u4e9b\uff0c\u4e0b\u9762\u5c31\u662f\u5b9e\u6218\u6848\u4f8b\uff0c\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u3002<\/p>\n<p style=\"text-align: left\">\u6548\u679c\u56fe<\/p>\n<p style=\"text-align: left\"><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/061\/021\/451c63030e2fbebe811de155d327e8c6-0.gif\" class=\"aligncenter\" title=\"\u600e\u6837\u4f7f\u7528Vue\u64cd\u4f5cDIV\u63d2\u56fe\" alt=\"\u600e\u6837\u4f7f\u7528Vue\u64cd\u4f5cDIV\u63d2\u56fe\" \/>&nbsp;<\/p>\n<p style=\"text-align: left\">demo1.gif<\/p>\n<p style=\"text-align: left\"><strong>\u5206\u6e05clientY pageY screenY layerY offsetY\u7684\u533a\u522b<\/strong><\/p>\n<p style=\"text-align: left\">\u5728\u6211\u4eec\u60f3\u8981\u505a\u51fa\u62d6\u62fd\u8fd9\u4e2a\u6548\u679c\u7684\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u5206\u6e05\u8fd9\u51e0\u4e2a\u5c5e\u6027\u7684\u533a\u522b\uff0c\u8fd9\u51e0\u4e2a\u5c5e\u6027\u90fd\u662f\u8ba1\u7b97\u9f20\u6807\u70b9\u51fb\u7684\u504f\u79fb\u503c\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u5176\u8fdb\u884c\u4e86\u89e3\u624d\u53ef\u4ee5\u7ee7\u7eed\u5b9e\u73b0\u6211\u4eec\u7684\u62d6\u62fd\u6548\u679c<\/p>\n<p style=\"text-align: left\">clientY \u6307\u7684\u662f\u8ddd\u79bb\u53ef\u89c6\u9875\u9762\u5de6\u4e0a\u89d2\u7684\u8ddd\u79bb<br \/>pageY \u6307\u7684\u662f\u8ddd\u79bb\u53ef\u89c6\u9875\u9762\u5de6\u4e0a\u89d2\u7684\u8ddd\u79bb(\u4e0d\u53d7\u9875\u9762\u6eda\u52a8\u5f71\u54cd)<br \/>screenY \u6307\u7684\u662f\u8ddd\u79bb\u5c4f\u5e55\u5de6\u4e0a\u89d2\u7684\u8ddd\u79bb<br \/>layerY \u6307\u7684\u662f\u627e\u5230\u5b83\u6216\u5b83\u7236\u7ea7\u5143\u7d20\u4e2d\u6700\u8fd1\u5177\u6709\u5b9a\u4f4d\u7684\u5de6\u4e0a\u89d2\u8ddd\u79bb<br \/>offsetY \u6307\u7684\u662f\u8ddd\u79bb\u5b83\u81ea\u5df1\u5de6\u4e0a\u89d2\u7684\u8ddd\u79bb<br \/>\u4e00\u5f20\u56fe\u5e26\u5927\u5bb6\u7b80\u5355\u4e86\u89e3\u4e86\u89e3<\/p>\n<p style=\"text-align: left\"><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/061\/021\/451c63030e2fbebe811de155d327e8c6-1.png\" class=\"aligncenter\" title=\"\u600e\u6837\u4f7f\u7528Vue\u64cd\u4f5cDIV\u63d2\u56fe1\" alt=\"\u600e\u6837\u4f7f\u7528Vue\u64cd\u4f5cDIV\u63d2\u56fe1\" \/>&nbsp;<\/p>\n<p style=\"text-align: left\"><strong>\u533a\u522b<\/strong><\/p>\n<p style=\"text-align: left\">\u5728\u6211\u4eec\u7b80\u5355\u4e86\u89e3\u5b8c\u8fd9\u4e9b\u4e2a\u5c5e\u6027\u4ee5\u540e\uff0c\u6709\u51e0\u4e2a\u5c5e\u6027\u9700\u8981\u5206\u6e05\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th> &nbsp; &nbsp; &nbsp;\u76f8\u540c\u70b9<\/th>\n<th>\u4e0d\u540c\u70b9<\/th>\n<th><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>clientY<\/td>\n<td>\u8ddd\u79bb\u9875\u9762\u5de6\u4e0a\u89d2\u8ddd\u79bb<\/td>\n<td>\u53d7\u9875\u9762\u6eda\u52a8\u7684\u5f71\u54cd<\/td>\n<\/tr>\n<tr>\n<td>pageY<\/td>\n<td>\u8ddd\u79bb\u9875\u9762\u5de6\u4e0a\u89d2\u7684\u8ddd\u79bb<\/td>\n<td>\u4e0d\u53d7\u9875\u9762\u6eda\u52a8\u5f71\u54cd<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table>\n<thead>\n<tr>\n<th><\/th>\n<th>\u76f8\u540c\u70b9<\/th>\n<th>\u4e0d\u540c\u70b9<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>layerY<\/td>\n<td>\u8ddd\u79bb\u5143\u7d20\u7684\u5de6\u4e0a\u89d2\u8ddd\u79bb<\/td>\n<td>\u53d7\u5143\u7d20\u7684\u5b9a\u4f4d\u7684\u5f71\u54cd\uff0c\u4f1a\u4ece\u672c\u5143\u7d20\u5f80\u4e0a\u627e\u5230\u7b2c\u4e00\u4e2a\u5b9a\u4f4d\u7684\u5143\u7d20\u7684\u5de6\u4e0a\u89d2<\/td>\n<\/tr>\n<tr>\n<td>offsetY<\/td>\n<td>\u8ddd\u79bb\u5143\u7d20\u5de6\u4e0a\u89d2\u7684\u8ddd\u79bb<\/td>\n<td>\u8ba1\u7b97\u76f8\u5bf9\u4e8e\u672c\u5143\u7d20\u7684\u5de6\u4e0a\u89d2\uff0c\u4e0d\u5728\u4e4e\u5b9a\u4f4d\u95ee\u9898\uff0c\u8ba1\u7b97\u7684\u662f\u5185\u4ea4\u70b9\u3002\u662f\u7684\u7279\u6709\u5c5e\u6027<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p style=\"text-align: left\"><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/061\/021\/1c9823d884d1ae8cdad801a3d11bab60-2.png\" class=\"aligncenter\" title=\"\u600e\u6837\u4f7f\u7528Vue\u64cd\u4f5cDIV\u63d2\u56fe2\" alt=\"\u600e\u6837\u4f7f\u7528Vue\u64cd\u4f5cDIV\u63d2\u56fe2\" \/>&nbsp;<\/p>\n<p style=\"text-align: left\">layerY\u4e0eoffsetY\u533a\u522b<\/p>\n<p style=\"text-align: left\"><strong>\u5b9e\u73b0\u62d6\u62fd\u529f\u80fd<\/strong><\/p>\n<p style=\"text-align: left\">\u6211\u4eec\u65e2\u7136\u719f\u6089\u4e86\u8fd9\u51e0\u4e2a\u504f\u79fb\u5c5e\u6027\u7684\u610f\u601d\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u8fdb\u5165\u6211\u4eec\u7684\u91cd\u70b9\u3002\u8bdd\u4e0d\u591a\u8bf4\u76f4\u63a5\u4e0a\u4ee3\u7801<\/p>\n<p style=\"text-align: left\">\/\/ darg.html<\/p>\n<pre>&lt;style&gt;\r\n  #app{\r\n    position: relative;   \/*\u5b9a\u4f4d*\/\r\n    top: 10px;\r\n    left: 10px;\r\n    width: 200px;\r\n    height: 200px;\r\n    background: #666;    \/*\u8bbe\u7f6e\u4e00\u4e0b\u80cc\u666f*\/\r\n  }\r\n&lt;\/style&gt;\r\n&lt;body&gt;\r\n  &lt;p id=\"app\" @mousedown=\"move\"&gt;    &lt;!--\u7ed1\u5b9a\u6309\u4e0b\u4e8b\u4ef6--&gt;\r\n    {{positionX}}\r\n    {{positionY}}\r\n  &lt;\/p&gt;\r\n&lt;\/body&gt;\r\n\/\/main.js\r\nlet app = new Vue({\r\n  el:'#app',\r\n  data:{\r\n    positionX:0,\r\n    positionY:0,\r\n  },\r\n  methods:{\r\n    move(e){\r\n      let op = e.target;    \/\/\u83b7\u53d6\u76ee\u6807\u5143\u7d20\r\n      \r\n      \/\/\u7b97\u51fa\u9f20\u6807\u76f8\u5bf9\u5143\u7d20\u7684\u4f4d\u7f6e\r\n      let disX = e.clientX - op.offsetLeft;\r\n      let disY = e.clientY - op.offsetTop;\r\n      document.onmousemove = (e)=&gt;{    \/\/\u9f20\u6807\u6309\u4e0b\u5e76\u79fb\u52a8\u7684\u4e8b\u4ef6\r\n        \/\/\u7528\u9f20\u6807\u7684\u4f4d\u7f6e\u51cf\u53bb\u9f20\u6807\u76f8\u5bf9\u5143\u7d20\u7684\u4f4d\u7f6e\uff0c\u5f97\u5230\u5143\u7d20\u7684\u4f4d\u7f6e\r\n        let left = e.clientX - disX;  \r\n        let top = e.clientY - disY;\r\n        \r\n        \/\/\u7ed1\u5b9a\u5143\u7d20\u4f4d\u7f6e\u5230positionX\u548cpositionY\u4e0a\u9762\r\n        this.positionX = top;\r\n        this.positionY = left;\r\n        \r\n        \/\/\u79fb\u52a8\u5f53\u524d\u5143\u7d20\r\n        op.style.left = left + 'px';\r\n        op.style.top = top + 'px';\r\n      };\r\n      document.onmouseup = (e) =&gt; {\r\n        document.onmousemove = null;\r\n        document.onmouseup = null;\r\n      };\r\n    }  \r\n  \r\n  },\r\n  computed:{},\r\n});<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u5f53\u7136\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b83\u7ed1\u5b9a\u4e3a\u4e00\u4e2a\u81ea\u5b9a\u4e49\u6307\u4ee4\uff0c\u8fd9\u6837\u7684\u8bdd\u5c31\u53ef\u4ee5\u7528\u8c03\u7528\u6307\u4ee4\u7684\u5f62\u5f0f\u6765\u5b9e\u73b0\u62d6\u62fd\u6548\u679c\uff0c\u4e0b\u9762\u662f\u5b9a\u4e49\u81ea\u5b9a\u4e49\u6307\u4ee4\u7684\u4ee3\u7801<\/p>\n<p style=\"text-align: left\">\/\/ darg.html<\/p>\n<pre>&lt;style&gt;\r\n  #app{\r\n    position: relative;   \/*\u5b9a\u4f4d*\/\r\n    top: 10px;\r\n    left: 10px;\r\n    width: 200px;\r\n    height: 200px;\r\n    background: #666;    \/*\u8bbe\u7f6e\u4e00\u4e0b\u80cc\u666f*\/\r\n  }\r\n&lt;\/style&gt;\r\n&lt;body&gt;\r\n  &lt;p id=\"app\" v-drag&gt;    &lt;!--\u5b9e\u73b0\u7528\u6307\u4ee4\u5f62\u5f0f\u5b9e\u73b0\u62d6\u62fd\u6548\u679c--&gt;\r\n    \r\n  &lt;\/p&gt;\r\n&lt;\/body&gt;\r\n\/\/main.js\r\nlet app = new Vue({\r\n  el:'#app',\r\n  data:{},\r\n  methods:{},\r\n  directives: {\r\n    drag: {\r\n      \/\/ \u6307\u4ee4\u7684\u5b9a\u4e49\r\n      bind: function (el) {\r\n        let op = el;  \/\/\u83b7\u53d6\u5f53\u524d\u5143\u7d20\r\n        op.onmousedown = (e) =&gt; {\r\n          \/\/\u7b97\u51fa\u9f20\u6807\u76f8\u5bf9\u5143\u7d20\u7684\u4f4d\u7f6e\r\n          let disX = e.clientX - op.offsetLeft;\r\n          let disY = e.clientY - op.offsetTop;\r\n          \r\n          document.onmousemove = (e)=&gt;{\r\n            \/\/\u7528\u9f20\u6807\u7684\u4f4d\u7f6e\u51cf\u53bb\u9f20\u6807\u76f8\u5bf9\u5143\u7d20\u7684\u4f4d\u7f6e\uff0c\u5f97\u5230\u5143\u7d20\u7684\u4f4d\u7f6e\r\n            let left = e.clientX - disX;  \r\n            let top = e.clientY - disY;\r\n           \r\n            \/\/\u7ed1\u5b9a\u5143\u7d20\u4f4d\u7f6e\u5230positionX\u548cpositionY\u4e0a\u9762\r\n            this.positionX = top;\r\n            this.positionY = left;\r\n        \r\n            \/\/\u79fb\u52a8\u5f53\u524d\u5143\u7d20\r\n            op.style.left = left + 'px';\r\n            op.style.top = top + 'px';\r\n          };\r\n          document.onmouseup = (e) =&gt; {\r\n            document.onmousemove = null;\r\n            document.onmouseup = null;\r\n          };\r\n        };\r\n      }\r\n    }\r\n  }\r\n});<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u76f8\u4fe1\u770b\u4e86\u672c\u6587\u6848\u4f8b\u4f60\u5df2\u7ecf\u638c\u63e1\u4e86\u65b9\u6cd5\uff0c\u66f4\u591a\u7cbe\u5f69\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n<p>\u63a8\u8350\u9605\u8bfb\uff1a<\/p>\n<p><\/p>\n<p><\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u6837\u4f7f\u7528Vue\u64cd\u4f5cDIV\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u6b21\u7ed9\u5927\u5bb6\u5e26\u6765\u600e\u6837\u4f7f\u7528Vue\u64cd\u4f5cDIV\uff0c\u4f7f\u7528Vue\u64cd\u4f5cDIV\u7684\u6709\u54ea\u4e9b\uff0c\u4e0b\u9762\u5c31\u662f\u5b9e\u6218\u6848\u4f8b\uff0c\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u3002 \u6548\u679c\u56fe &nbsp; demo1.gif \u5206\u6e05clientY pageY screenY layerY offsetY\u7684\u533a\u522b \u5728\u6211\u4eec\u60f3\u8981\u505a\u51fa\u62d6\u62fd\u8fd9\u4e2a\u6548\u679c\u7684\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u5206\u6e05\u8fd9\u51e0\u4e2a\u5c5e\u6027\u7684\u533a\u522b\uff0c\u8fd9\u51e0\u4e2a\u5c5e\u6027\u90fd\u662f\u8ba1\u7b97\u9f20\u6807\u70b9\u51fb\u7684\u504f\u79fb\u503c\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u5176\u8fdb\u884c\u4e86\u89e3\u624d\u53ef\u4ee5\u7ee7\u7eed\u5b9e\u73b0\u6211\u4eec\u7684\u62d6\u62fd\u6548\u679c clientY \u6307\u7684\u662f\u8ddd\u79bb\u53ef\u89c6\u9875\u9762\u5de6\u4e0a\u89d2\u7684\u8ddd\u79bbpageY \u6307\u7684\u662f\u8ddd\u79bb\u53ef\u89c6\u9875\u9762\u5de6\u4e0a\u89d2\u7684\u8ddd\u79bb(\u4e0d\u53d7\u9875\u9762\u6eda\u52a8\u5f71\u54cd)screenY \u6307\u7684\u662f\u8ddd\u79bb\u5c4f\u5e55\u5de6\u4e0a\u89d2\u7684\u8ddd\u79bblayerY \u6307\u7684\u662f\u627e\u5230\u5b83\u6216\u5b83\u7236\u7ea7\u5143\u7d20\u4e2d\u6700\u8fd1\u5177\u6709\u5b9a\u4f4d\u7684\u5de6\u4e0a\u89d2\u8ddd\u79bboffsetY \u6307\u7684\u662f\u8ddd\u79bb\u5b83\u81ea\u5df1\u5de6\u4e0a\u89d2\u7684\u8ddd\u79bb\u4e00\u5f20\u56fe\u5e26\u5927\u5bb6\u7b80\u5355\u4e86\u89e3\u4e86\u89e3 &nbsp; \u533a\u522b \u5728\u6211\u4eec\u7b80\u5355\u4e86\u89e3\u5b8c\u8fd9\u4e9b\u4e2a\u5c5e\u6027\u4ee5\u540e\uff0c\u6709\u51e0\u4e2a\u5c5e\u6027\u9700\u8981\u5206\u6e05\u3002 &nbsp; &nbsp; &nbsp;\u76f8\u540c\u70b9 \u4e0d\u540c\u70b9 clientY \u8ddd\u79bb\u9875\u9762\u5de6\u4e0a\u89d2\u8ddd\u79bb \u53d7\u9875\u9762\u6eda\u52a8\u7684\u5f71\u54cd pageY \u8ddd\u79bb\u9875\u9762\u5de6\u4e0a\u89d2\u7684\u8ddd\u79bb \u4e0d\u53d7\u9875\u9762\u6eda\u52a8\u5f71\u54cd \u76f8\u540c\u70b9 \u4e0d\u540c\u70b9 layerY \u8ddd\u79bb\u5143\u7d20\u7684\u5de6\u4e0a\u89d2\u8ddd\u79bb \u53d7\u5143\u7d20\u7684\u5b9a\u4f4d\u7684\u5f71\u54cd\uff0c\u4f1a\u4ece\u672c\u5143\u7d20\u5f80\u4e0a\u627e\u5230\u7b2c\u4e00\u4e2a\u5b9a\u4f4d\u7684\u5143\u7d20\u7684\u5de6\u4e0a\u89d2 offsetY \u8ddd\u79bb\u5143\u7d20\u5de6\u4e0a\u89d2\u7684\u8ddd\u79bb \u8ba1\u7b97\u76f8\u5bf9\u4e8e\u672c\u5143\u7d20\u7684\u5de6\u4e0a\u89d2\uff0c\u4e0d\u5728\u4e4e\u5b9a\u4f4d\u95ee\u9898\uff0c\u8ba1\u7b97\u7684\u662f\u5185\u4ea4\u70b9\u3002\u662f\u7684\u7279\u6709\u5c5e\u6027 &nbsp; layerY\u4e0eoffsetY\u533a\u522b \u5b9e\u73b0\u62d6\u62fd\u529f\u80fd \u6211\u4eec\u65e2\u7136\u719f\u6089\u4e86\u8fd9\u51e0\u4e2a\u504f\u79fb\u5c5e\u6027\u7684\u610f\u601d\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u8fdb\u5165\u6211\u4eec\u7684\u91cd\u70b9\u3002\u8bdd\u4e0d\u591a\u8bf4\u76f4\u63a5\u4e0a\u4ee3\u7801 \/\/ darg.html &lt;style&gt; #app{ position: relative; \/*\u5b9a\u4f4d*\/ top: 10px; left: 10px; width: 200px; height: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-31518","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/31518","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=31518"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/31518\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=31518"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=31518"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=31518"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}