{"id":53555,"date":"2024-12-03T11:57:56","date_gmt":"2024-12-03T03:57:56","guid":{"rendered":"https:\/\/fwq.ai\/blog\/53555\/"},"modified":"2024-12-03T11:57:56","modified_gmt":"2024-12-03T03:57:56","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-javascript-%e6%8e%a7%e5%88%b6%e9%a1%b5%e9%9d%a2%e6%bb%9a%e5%8a%a8%e9%80%9f%e5%ba%a6%e5%92%8c%e8%b7%9d%e7%a6%bb%ef%bc%9f-3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/53555\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 JavaScript \u63a7\u5236\u9875\u9762\u6eda\u52a8\u901f\u5ea6\u548c\u8ddd\u79bb\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 JavaScript \u63a7\u5236\u9875\u9762\u6eda\u52a8\u901f\u5ea6\u548c\u8ddd\u79bb\uff1f <\/h1>\n<p>\u4f60\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\u5417\uff1f\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u4f7f\u7528 JavaScript \u63a7\u5236\u9875\u9762\u6eda\u52a8\u901f\u5ea6\u548c\u8ddd\u79bb\uff1f \u300b<\/span>\uff0c\u4e3b\u8981\u4ecb\u7ecd\u7684\u5185\u5bb9\u5c31\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u5982\u679c\u4f60\u60f3\u63d0\u5347\u81ea\u5df1\u7684\u5f00\u53d1\u80fd\u529b\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u5927\u5bb6\u8981\u77e5\u9053\u7f16\u7a0b\u7406\u8bba\u57fa\u7840\u548c\u5b9e\u6218\u64cd\u4f5c\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u54e6\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241201\/1733064687674c77ef96a66.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528 JavaScript \u63a7\u5236\u9875\u9762\u6eda\u52a8\u901f\u5ea6\u548c\u8ddd\u79bb\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528 JavaScript \u63a7\u5236\u9875\u9762\u6eda\u52a8\u901f\u5ea6\u548c\u8ddd\u79bb\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 JavaScript \u4fee\u6539\u9875\u9762\u7684\u6eda\u52a8\u901f\u5ea6\u548c\u8ddd\u79bb\uff1f<\/strong><\/p>\n<p>\u5728\u9875\u9762\u4e2d\uff0c\u4f7f\u7528\u9f20\u6807\u6ed1\u8f6e\u6eda\u52a8\u65f6\uff0c\u5982\u4f55\u4fee\u6539\u9875\u9762\u7684\u6eda\u52a8\u901f\u5ea6\u548c\u8ddd\u79bb\uff1f\u4f8b\u5982\uff0c\u5c06\u9ed8\u8ba4\u6eda\u52a8\u8ddd\u79bb\uff08200px\uff09\u4fee\u6539\u4e3a\u60a8\u5e0c\u671b\u7684\u6eda\u52a8\u8ddd\u79bb\uff08400px\uff09\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528\u539f\u751f JavaScript \u5b9e\u73b0\u6b64\u529f\u80fd\u7684\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li><strong>\u83b7\u53d6\u8d77\u59cb\u4f4d\u7f6e\u548c\u7ec8\u70b9\u4f4d\u7f6e\uff1a<\/strong>\u4f7f\u7528 scrollTop \u5c5e\u6027\u83b7\u53d6\u9875\u9762\u5f53\u524d\u4f4d\u7f6e\u548c\u60a8\u8981\u6eda\u52a8\u7684\u76ee\u6807\u4f4d\u7f6e\u3002<\/li>\n<li><strong>\u8ba1\u7b97\u6eda\u52a8\u8ddd\u79bb\uff1a<\/strong>\u8ba1\u7b97\u6eda\u52a8\u8ddd\u79bb\uff0c\u65b9\u6cd5\u662f\u5c06\u7ec8\u70b9\u4f4d\u7f6e\u51cf\u53bb\u8d77\u59cb\u4f4d\u7f6e\u3002<\/li>\n<li><strong>\u8bbe\u7f6e\u6eda\u52a8\u901f\u5ea6\uff1a<\/strong>\u4f7f\u7528 requestAnimationFrame \u65b9\u6cd5\u548c\u65f6\u95f4\u5dee\u8ba1\u7b97\u51fa\u6bcf\u4e00\u5e27\u6eda\u52a8\u7684\u8ddd\u79bb\uff0c\u4ee5\u5b9e\u73b0\u5e73\u6ed1\u7684\u6eda\u52a8\u6548\u679c\u3002<\/li>\n<\/ol>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u6837\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5c06\u6bcf\u6b21\u6eda\u52a8\u7684\u8ddd\u79bb\u4ece 200px \u4fee\u6539\u4e3a 400px\uff1a<\/p>\n<pre>function smoothScroll(targetPosition) {\n  const startPosition = window.pageYOffset;\n  const distance = targetPosition - startPosition;\n  const duration = 1000; \/\/ \u6eda\u52a8\u65f6\u95f4\n  let start = null;\n\n  function step(timestamp) {\n    if (!start) start = timestamp;\n    const progress = timestamp - start;\n    const scrollY = progress \/ duration * distance + startPosition;\n    window.scrollTo(0, scrollY);\n    if (progress &lt; duration) {\n      window.requestAnimationFrame(step);\n    }\n  }\n\n  window.requestAnimationFrame(step);\n}\n\n\/\/ \u5728\u6eda\u52a8\u4e8b\u4ef6\u4e2d\u8c03\u7528 smoothScroll \u51fd\u6570\uff0c\u4f20\u5165\u76ee\u6807\u4f4d\u7f6e\nwindow.addEventListener('wheel', (e) =&gt; {\n  e.preventDefault();\n  let targetPosition = window.pageYOffset + e.deltaY * 400;\n  smoothScroll(targetPosition);\n});<\/pre>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4fee\u6539\u539f\u751f\u7684\u6eda\u52a8\u8ddd\u79bb\u53ef\u80fd\u4f1a\u5f71\u54cd\u7528\u6237\u4f53\u9a8c\uff0c\u56e0\u6b64\u5efa\u8bae\u8c28\u614e\u4f7f\u7528\u6b64\u529f\u80fd\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u4f7f\u7528 JavaScript \u63a7\u5236\u9875\u9762\u6eda\u52a8\u901f\u5ea6\u548c\u8ddd\u79bb\uff1f \u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u7c73\u4e91\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   Go select case \u4e2d\u4e3a\u4ec0\u4e48\u5b9a\u65f6\u5668\u6267\u884c\u9891\u7387\u964d\u4f4e\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528 JavaScript \u63a7\u5236\u9875\u9762\u6eda\u52a8\u901f\u5ea6\u548c\u8ddd\u79bb\uff1f \u4f60\u5728\u5b66\u4e60\u6587\u7ae0\u76f8\u5173\u7684\u77e5\u8bc6\u5417\uff1f\u672c\u6587\u300a\u5982\u4f55\u4f7f\u7528 JavaScript \u63a7\u5236\u9875\u9762\u6eda\u52a8\u901f\u5ea6\u548c\u8ddd\u79bb\uff1f \u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u7684\u5185\u5bb9\u5c31\u6d89\u53ca\u5230\uff0c\u5982\u679c\u4f60\u60f3\u63d0\u5347\u81ea\u5df1\u7684\u5f00\u53d1\u80fd\u529b\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u5927\u5bb6\u8981\u77e5\u9053\u7f16\u7a0b\u7406\u8bba\u57fa\u7840\u548c\u5b9e\u6218\u64cd\u4f5c\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u54e6\uff01 \u5982\u4f55\u4f7f\u7528 JavaScript \u4fee\u6539\u9875\u9762\u7684\u6eda\u52a8\u901f\u5ea6\u548c\u8ddd\u79bb\uff1f \u5728\u9875\u9762\u4e2d\uff0c\u4f7f\u7528\u9f20\u6807\u6ed1\u8f6e\u6eda\u52a8\u65f6\uff0c\u5982\u4f55\u4fee\u6539\u9875\u9762\u7684\u6eda\u52a8\u901f\u5ea6\u548c\u8ddd\u79bb\uff1f\u4f8b\u5982\uff0c\u5c06\u9ed8\u8ba4\u6eda\u52a8\u8ddd\u79bb\uff08200px\uff09\u4fee\u6539\u4e3a\u60a8\u5e0c\u671b\u7684\u6eda\u52a8\u8ddd\u79bb\uff08400px\uff09\u3002 \u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528\u539f\u751f JavaScript \u5b9e\u73b0\u6b64\u529f\u80fd\u7684\u6b65\u9aa4\uff1a \u83b7\u53d6\u8d77\u59cb\u4f4d\u7f6e\u548c\u7ec8\u70b9\u4f4d\u7f6e\uff1a\u4f7f\u7528 scrollTop \u5c5e\u6027\u83b7\u53d6\u9875\u9762\u5f53\u524d\u4f4d\u7f6e\u548c\u60a8\u8981\u6eda\u52a8\u7684\u76ee\u6807\u4f4d\u7f6e\u3002 \u8ba1\u7b97\u6eda\u52a8\u8ddd\u79bb\uff1a\u8ba1\u7b97\u6eda\u52a8\u8ddd\u79bb\uff0c\u65b9\u6cd5\u662f\u5c06\u7ec8\u70b9\u4f4d\u7f6e\u51cf\u53bb\u8d77\u59cb\u4f4d\u7f6e\u3002 \u8bbe\u7f6e\u6eda\u52a8\u901f\u5ea6\uff1a\u4f7f\u7528 requestAnimationFrame \u65b9\u6cd5\u548c\u65f6\u95f4\u5dee\u8ba1\u7b97\u51fa\u6bcf\u4e00\u5e27\u6eda\u52a8\u7684\u8ddd\u79bb\uff0c\u4ee5\u5b9e\u73b0\u5e73\u6ed1\u7684\u6eda\u52a8\u6548\u679c\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u6837\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5c06\u6bcf\u6b21\u6eda\u52a8\u7684\u8ddd\u79bb\u4ece 200px \u4fee\u6539\u4e3a 400px\uff1a function smoothScroll(targetPosition) { 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 = [&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-53555","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53555","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=53555"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53555\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=53555"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=53555"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=53555"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}