{"id":1416,"date":"2024-11-10T17:02:22","date_gmt":"2024-11-10T09:02:22","guid":{"rendered":"https:\/\/fwq.ai\/blog\/1416\/"},"modified":"2024-11-10T17:02:22","modified_gmt":"2024-11-10T09:02:22","slug":"%e7%bd%91%e9%a1%b5%e6%89%b9%e6%b3%a8%e9%97%b4%e8%b7%9d%e9%9a%be%e9%a2%98%ef%bc%9a%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-javascript-%e5%ae%9e%e7%8e%b0%e8%87%aa%e9%80%82%e5%ba%94%e5%ae%9a%e4%bd%8d","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/1416\/","title":{"rendered":"\u7f51\u9875\u6279\u6ce8\u95f4\u8ddd\u96be\u9898\uff1a\u5982\u4f55\u4f7f\u7528 JavaScript \u5b9e\u73b0\u81ea\u9002\u5e94\u5b9a\u4f4d\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173042805076929.jpg\" class=\"aligncenter\" title=\"\u7f51\u9875\u6279\u6ce8\u95f4\u8ddd\u96be\u9898\uff1a\u5982\u4f55\u4f7f\u7528 JavaScript \u5b9e\u73b0\u81ea\u9002\u5e94\u5b9a\u4f4d\uff1f\u63d2\u56fe\" alt=\"\u7f51\u9875\u6279\u6ce8\u95f4\u8ddd\u96be\u9898\uff1a\u5982\u4f55\u4f7f\u7528 JavaScript \u5b9e\u73b0\u81ea\u9002\u5e94\u5b9a\u4f4d\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u7f51\u9875\u6279\u6ce8\u95f4\u8ddd\u96be\u9898\uff1a\u81ea\u9002\u5e94\u5b9a\u4f4d\u6c42\u89e3<\/strong><\/p>\n<p>\u968f\u7740\u7528\u6237\u6279\u6ce8\u6570\u91cf\u7684\u589e\u52a0\uff0c\u5982\u4f55\u4f18\u96c5\u5730\u6279\u6ce8\u4ee5\u907f\u514d\u91cd\u53e0\u6210\u4e3a\u4e86\u4e00\u5927\u96be\u9898\u3002\u5bf9\u4e8e\u6b64\u7c7b\u9700\u6c42\uff0c\u4e00\u4e2a\u6709\u6548\u4e14\u9ad8\u6548\u7684\u89e3\u51b3\u65b9\u6848\u662f\u4f7f\u7528\u5e76\u4f9d\u9760 JavaScript \u8fdb\u884c\u6279\u6ce8\u4f4d\u7f6e\u7684\u81ea\u9002\u5e94\u8ba1\u7b97\u3002<\/p>\n<ol>\n<li>\n<p><strong>\u6570\u636e\u7ed3\u6784\uff1a<\/strong><\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u6279\u6ce8\u76f8\u5173\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5176\u4e2d\u5305\u62ec\u6279\u6ce8\u9876\u90e8\u4f4d\u7f6e (top) \u548c\u9ad8\u5ea6 (height)\u3002\u8fd9\u4e9b\u503c\u5c06\u5b58\u50a8\u5728\u4e00\u4e2a\u6570\u7ec4\u4e2d\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>[\n    {top: 100, height: 200},\n    {top: 800, height: 200},\n    {top: 820, height: 200},\n    {top: 1020, height: 200},\n]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p><strong>\u81ea\u9002\u5e94\u7b97\u6cd5\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u7f16\u5199\u4e00\u4e2a\u7b97\u6cd5\u6765\u81ea\u9002\u5e94\u5730\u8ba1\u7b97\u6bcf\u4e2a\u6279\u6ce8\u7684\u5782\u76f4\u4f4d\u7f6e\u3002\u5176\u6838\u5fc3\u601d\u60f3\u662f\u6bd4\u8f83\u5f53\u524d\u6279\u6ce8\u7684\u9876\u90e8\u4f4d\u7f6e\u4e0e\u6570\u7ec4\u4e2d\u6240\u6709\u5df2\u5b58\u5728\u6279\u6ce8\u7684\u5e95\u90e8\u4f4d\u7f6e\uff08\u5373 top + height\uff09\u3002<\/p>\n<p>\u4f7f\u7528 reduce() \u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u8fed\u4ee3\u6570\u7ec4\u5e76\u8ba1\u7b97\u6bcf\u4e2a\u6279\u6ce8\u7684 currentTop\uff1a<\/p>\n<pre>arr.reduce((s, n, i) =&gt; {\n    n.currentTop = Math.max(n.top, (s.currentTop || s.top) + s.height)\n    return n\n})<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u901a\u8fc7\u4f7f\u7528 Math.max()\uff0c\u6211\u4eec\u786e\u4fdd\u6bcf\u4e2a\u6279\u6ce8\u90fd\u653e\u7f6e\u5728\u79bb\u5176\u4e0a\u65b9\u6279\u6ce8\u5e95\u90e8\u6700\u8fdc\u7684\u4f4d\u7f6e\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u7ed3\u679c\uff1a<\/strong><\/p>\n<p>\u7ecf\u8fc7\u81ea\u9002\u5e94\u8ba1\u7b97\u540e\uff0c\u6bcf\u4e2a\u6279\u6ce8\u7684 currentTop \u5c06\u88ab\u66f4\u65b0\u3002\u8fd9\u8868\u793a\u6279\u6ce8\u7684\u5782\u76f4\u4f4d\u7f6e\uff0c\u5b83\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u5177\u6709\u6b63\u786e\u95f4\u8ddd\u7684\u6279\u6ce8\u5143\u7d20\u3002\u8f93\u51fa\u7684\u6570\u636e\u7ed3\u6784\u7c7b\u4f3c\u4e8e\uff1a<\/p>\n<pre>[\n    {top: 100, height: 200, currentTop: 100},\n    {top: 800, height: 200, currentTop: 1020},\n    {top: 820, height: 200, currentTop: 1220},\n    {top: 1020, height: 200, currentTop: 1420},\n    {top: 1430, height: 180, currentTop: 1600},\n]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ol>\n<p>\u901a\u8fc7\u4f7f\u7528\u6b64\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u6279\u6ce8\u7684\u81ea\u52a8\u95f4\u8ddd\uff0c\u65e2\u4fdd\u8bc1\u6279\u6ce8\u4e4b\u95f4\u4e0d\u4f1a\u91cd\u53e0\uff0c\u4e5f\u80fd\u6700\u5927\u7a0b\u5ea6\u5730\u5229\u7528\u53ef\u7528\u7a7a\u95f4\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u7f51\u9875\u6279\u6ce8\u95f4\u8ddd\u96be\u9898\uff1a\u5982\u4f55\u4f7f\u7528 JavaScript \u5b9e\u73b0\u81ea\u9002\u5e94\u5b9a\u4f4d\uff1f\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>\u7f51\u9875\u6279\u6ce8\u95f4\u8ddd\u96be\u9898\uff1a\u81ea\u9002\u5e94\u5b9a\u4f4d\u6c42\u89e3 \u968f\u7740\u7528\u6237\u6279\u6ce8\u6570\u91cf\u7684\u589e\u52a0\uff0c\u5982\u4f55\u4f18\u96c5\u5730\u6279\u6ce8\u4ee5\u907f\u514d\u91cd\u53e0\u6210\u4e3a\u4e86\u4e00\u5927\u96be\u9898\u3002\u5bf9\u4e8e\u6b64\u7c7b\u9700\u6c42\uff0c\u4e00\u4e2a\u6709\u6548\u4e14\u9ad8\u6548\u7684\u89e3\u51b3\u65b9\u6848\u662f\u4f7f\u7528\u5e76\u4f9d\u9760 JavaScript \u8fdb\u884c\u6279\u6ce8\u4f4d\u7f6e\u7684\u81ea\u9002\u5e94\u8ba1\u7b97\u3002 \u6570\u636e\u7ed3\u6784\uff1a \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u6279\u6ce8\u76f8\u5173\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5176\u4e2d\u5305\u62ec\u6279\u6ce8\u9876\u90e8\u4f4d\u7f6e (top) \u548c\u9ad8\u5ea6 (height)\u3002\u8fd9\u4e9b\u503c\u5c06\u5b58\u50a8\u5728\u4e00\u4e2a\u6570\u7ec4\u4e2d\uff0c\u4f8b\u5982\uff1a [ {top: 100, height: 200}, {top: 800, height: 200}, {top: 820, height: 200}, {top: 1020, height: 200}, ] \u767b\u5f55\u540e\u590d\u5236 \u81ea\u9002\u5e94\u7b97\u6cd5\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u7f16\u5199\u4e00\u4e2a\u7b97\u6cd5\u6765\u81ea\u9002\u5e94\u5730\u8ba1\u7b97\u6bcf\u4e2a\u6279\u6ce8\u7684\u5782\u76f4\u4f4d\u7f6e\u3002\u5176\u6838\u5fc3\u601d\u60f3\u662f\u6bd4\u8f83\u5f53\u524d\u6279\u6ce8\u7684\u9876\u90e8\u4f4d\u7f6e\u4e0e\u6570\u7ec4\u4e2d\u6240\u6709\u5df2\u5b58\u5728\u6279\u6ce8\u7684\u5e95\u90e8\u4f4d\u7f6e\uff08\u5373 top + height\uff09\u3002 \u4f7f\u7528 reduce() \u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u8fed\u4ee3\u6570\u7ec4\u5e76\u8ba1\u7b97\u6bcf\u4e2a\u6279\u6ce8\u7684 currentTop\uff1a arr.reduce((s, n, i) =&gt; { n.currentTop = Math.max(n.top, (s.currentTop || s.top) + s.height) return n }) \u767b\u5f55\u540e\u590d\u5236 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-1416","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1416","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=1416"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1416\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=1416"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=1416"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=1416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}