{"id":2555,"date":"2024-11-10T13:32:41","date_gmt":"2024-11-10T05:32:41","guid":{"rendered":"https:\/\/fwq.ai\/blog\/2555\/"},"modified":"2024-11-10T13:32:41","modified_gmt":"2024-11-10T05:32:41","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e5%8e%9f%e7%94%9fjavascript%e6%8e%a7%e5%88%b6%e7%bd%91%e9%a1%b5%e6%bb%9a%e5%8a%a8%e8%b7%9d%e7%a6%bb%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/2555\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528\u539f\u751fJavaScript\u63a7\u5236\u7f51\u9875\u6eda\u52a8\u8ddd\u79bb\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173079432322351.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528\u539f\u751fJavaScript\u63a7\u5236\u7f51\u9875\u6eda\u52a8\u8ddd\u79bb\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528\u539f\u751fJavaScript\u63a7\u5236\u7f51\u9875\u6eda\u52a8\u8ddd\u79bb\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u63a7\u5236\u9f20\u6807\u6eda\u52a8\u8ddd\u79bb\uff0c\u6253\u9020\u5e73\u6ed1\u6eda\u52a8\u4f53\u9a8c<\/strong><\/p>\n<p>\u5728\u7f51\u9875\u6d4f\u89c8\u4e2d\uff0c\u9f20\u6807\u6eda\u52a8\u662f\u5b9e\u73b0\u9875\u9762\u6d4f\u89c8\u7684\u91cd\u8981\u4ea4\u4e92\u3002\u60f3\u8981\u63d0\u5347\u7528\u6237\u4f53\u9a8c\uff0c\u6709\u65f6\u9700\u8981\u5bf9\u9ed8\u8ba4\u7684\u6eda\u52a8\u901f\u5ea6\u548c\u8ddd\u79bb\u8fdb\u884c\u8c03\u6574\u3002\u672c\u6587\u5c06\u63a2\u8ba8\u5982\u4f55\u4f7f\u7528\u539f\u751fJavaScript\u8f7b\u677e\u4fee\u6539\u9875\u9762\u7684\u6eda\u52a8\u901f\u5ea6\u548c\u8ddd\u79bb\u3002<\/p>\n<p><strong>\u5b9e\u73b0\u5e73\u6ed1\u6eda\u52a8\u6548\u679c<\/strong><\/p>\n<p>\u8981\u60f3\u5b9a\u5236\u6eda\u52a8\u884c\u4e3a\uff0c\u9700\u8981\u4f7f\u7528\u5e73\u6ed1\u6eda\u52a8\u6280\u672f\u3002\u5148\u83b7\u53d6\u8d77\u59cb\u70b9\u548c\u7ec8\u70b9\u7684\u4f4d\u7f6e\uff0c\u518d\u8ba1\u7b97\u6eda\u52a8\u8ddd\u79bb\u3002\u6700\u540e\uff0c\u5229\u7528requestAnimationFrame\u65b9\u6cd5\u548c\u65f6\u95f4\u5dee\u8ba1\u7b97\u51fa\u6bcf\u4e00\u5e27\u6eda\u52a8\u7684\u8ddd\u79bb\uff0c\u4ee5\u5b9e\u73b0\u6d41\u7545\u7684\u6eda\u52a8\u52a8\u753b\u3002<\/p>\n<p><strong>\u4fee\u6539\u6eda\u52a8\u8ddd\u79bb<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u5c06\u6bcf\u6b21\u6eda\u52a8\u7684\u8ddd\u79bb\u4ece200px\u4fee\u6539\u4e3a400px\uff1a<\/p>\n<pre>function smoothScroll(targetPosition) {\n  \/\/ \u83b7\u53d6\u8d77\u59cb\u4f4d\u7f6e\u548c\u7ec8\u70b9\u4f4d\u7f6e\n  const startPosition = window.pageYOffset;\n  const distance = targetPosition - startPosition;\n  const duration = 1000; \/\/ \u6eda\u52a8\u65f6\u95f4\n\n  let start = null;\n\n  function step(timestamp) {\n    if (!start) start = timestamp;\n    \n    \/\/ \u8ba1\u7b97\u6bcf\u4e00\u5e27\u6eda\u52a8\u7684\u8ddd\u79bb\u548c\u5f53\u524d\u6eda\u52a8\u4f4d\u7f6e\n    const progress = timestamp - start;\n    const scrollY = progress \/ duration * distance + startPosition;\n    \n    \/\/ \u6eda\u52a8\u9875\u9762\n    window.scrollTo(0, scrollY);\n    \n    \/\/ \u6301\u7eed\u6eda\u52a8\u76f4\u5230\u5230\u8fbe\u7ec8\u70b9\n    if (progress &lt; duration) {\n      window.requestAnimationFrame(step);\n    }\n  }\n  \n  \/\/ \u5f00\u59cb\u6eda\u52a8\u52a8\u753b\n  window.requestAnimationFrame(step);\n}\n\n\/\/ \u6eda\u52a8\u4e8b\u4ef6\u76d1\u542c\u5668\nwindow.addEventListener('wheel', (e) =&gt; {\n  e.preventDefault();\n  let targetPosition = window.pageYOffset + e.deltaY * 400;\n  smoothScroll(targetPosition);\n});<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<p>\u4fee\u6539\u6eda\u52a8\u8ddd\u79bb\u53ef\u80fd\u4f1a\u5f71\u54cd\u7528\u6237\u4f53\u9a8c\uff0c\u56e0\u6b64\u5728\u4f7f\u7528\u65f6\u52a1\u5fc5\u52a0\u4ee5\u8003\u91cf\u3002\u5efa\u8bae\u6839\u636e\u5b9e\u9645\u573a\u666f\u5408\u7406\u8c03\u6574\u6eda\u52a8\u901f\u5ea6\u548c\u8ddd\u79bb\uff0c\u4ee5\u8fbe\u5230\u6700\u4f73\u7684\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528\u539f\u751fJavaScript\u63a7\u5236\u7f51\u9875\u6eda\u52a8\u8ddd\u79bb\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>\u63a7\u5236\u9f20\u6807\u6eda\u52a8\u8ddd\u79bb\uff0c\u6253\u9020\u5e73\u6ed1\u6eda\u52a8\u4f53\u9a8c \u5728\u7f51\u9875\u6d4f\u89c8\u4e2d\uff0c\u9f20\u6807\u6eda\u52a8\u662f\u5b9e\u73b0\u9875\u9762\u6d4f\u89c8\u7684\u91cd\u8981\u4ea4\u4e92\u3002\u60f3\u8981\u63d0\u5347\u7528\u6237\u4f53\u9a8c\uff0c\u6709\u65f6\u9700\u8981\u5bf9\u9ed8\u8ba4\u7684\u6eda\u52a8\u901f\u5ea6\u548c\u8ddd\u79bb\u8fdb\u884c\u8c03\u6574\u3002\u672c\u6587\u5c06\u63a2\u8ba8\u5982\u4f55\u4f7f\u7528\u539f\u751fJavaScript\u8f7b\u677e\u4fee\u6539\u9875\u9762\u7684\u6eda\u52a8\u901f\u5ea6\u548c\u8ddd\u79bb\u3002 \u5b9e\u73b0\u5e73\u6ed1\u6eda\u52a8\u6548\u679c \u8981\u60f3\u5b9a\u5236\u6eda\u52a8\u884c\u4e3a\uff0c\u9700\u8981\u4f7f\u7528\u5e73\u6ed1\u6eda\u52a8\u6280\u672f\u3002\u5148\u83b7\u53d6\u8d77\u59cb\u70b9\u548c\u7ec8\u70b9\u7684\u4f4d\u7f6e\uff0c\u518d\u8ba1\u7b97\u6eda\u52a8\u8ddd\u79bb\u3002\u6700\u540e\uff0c\u5229\u7528requestAnimationFrame\u65b9\u6cd5\u548c\u65f6\u95f4\u5dee\u8ba1\u7b97\u51fa\u6bcf\u4e00\u5e27\u6eda\u52a8\u7684\u8ddd\u79bb\uff0c\u4ee5\u5b9e\u73b0\u6d41\u7545\u7684\u6eda\u52a8\u52a8\u753b\u3002 \u4fee\u6539\u6eda\u52a8\u8ddd\u79bb \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u4ee5\u4e0b\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u5c06\u6bcf\u6b21\u6eda\u52a8\u7684\u8ddd\u79bb\u4ece200px\u4fee\u6539\u4e3a400px\uff1a function smoothScroll(targetPosition) { \/\/ \u83b7\u53d6\u8d77\u59cb\u4f4d\u7f6e\u548c\u7ec8\u70b9\u4f4d\u7f6e const startPosition = window.pageYOffset; const distance = targetPosition &#8211; startPosition; const duration = 1000; \/\/ \u6eda\u52a8\u65f6\u95f4 let start = null; function step(timestamp) { if (!start) start = timestamp; \/\/ \u8ba1\u7b97\u6bcf\u4e00\u5e27\u6eda\u52a8\u7684\u8ddd\u79bb\u548c\u5f53\u524d\u6eda\u52a8\u4f4d\u7f6e const progress = timestamp &#8211; start; const scrollY = progress \/ duration * [&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-2555","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2555","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=2555"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2555\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=2555"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=2555"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=2555"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}