{"id":53153,"date":"2024-12-03T16:42:18","date_gmt":"2024-12-03T08:42:18","guid":{"rendered":"https:\/\/fwq.ai\/blog\/53153\/"},"modified":"2024-12-03T16:42:18","modified_gmt":"2024-12-03T08:42:18","slug":"%e5%a6%82%e4%bd%95%e7%94%a8%e5%8e%9f%e7%94%9fjs%e4%bf%ae%e6%94%b9%e9%a1%b5%e9%9d%a2%e6%bb%9a%e5%8a%a8%e8%b7%9d%e7%a6%bb%e5%92%8c%e9%80%9f%e5%ba%a6%ef%bc%8c%e5%ae%9e%e7%8e%b0%e6%bb%91%e5%8a%a8-3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/53153\/","title":{"rendered":"\u5982\u4f55\u7528\u539f\u751fJS\u4fee\u6539\u9875\u9762\u6eda\u52a8\u8ddd\u79bb\u548c\u901f\u5ea6\uff0c\u5b9e\u73b0\u6ed1\u52a8\u4e00\u6b21\u79fb\u52a8400px\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u7528\u539f\u751fJS\u4fee\u6539\u9875\u9762\u6eda\u52a8\u8ddd\u79bb\u548c\u901f\u5ea6\uff0c\u5b9e\u73b0\u6ed1\u52a8\u4e00\u6b21\u79fb\u52a8400px\uff1f <\/h1>\n<p>\u5927\u5bb6\u597d\uff0c\u6211\u4eec\u53c8\u89c1\u9762\u4e86\u554a~\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u7528\u539f\u751fJS\u4fee\u6539\u9875\u9762\u6eda\u52a8\u8ddd\u79bb\u548c\u901f\u5ea6\uff0c\u5b9e\u73b0\u6ed1\u52a8\u4e00\u6b21\u79fb\u52a8400px\uff1f \u300b<\/span>\u7684\u5185\u5bb9\u4e2d\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u3002\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u77e5\u8bc6\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\uff0c\u4ee5\u540e\u4f1a\u7ed9\u5927\u5bb6\u5e26\u6765\u66f4\u591a<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u6587\u7ae0\uff0c\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u8fdb\u6b65\uff01\u4e0b\u9762\u5c31\u5f00\u59cb\u672c\u6587\u7684\u6b63\u5f0f\u5185\u5bb9~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241127\/17326944386746d1a687e68.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u7528\u539f\u751fJS\u4fee\u6539\u9875\u9762\u6eda\u52a8\u8ddd\u79bb\u548c\u901f\u5ea6\uff0c\u5b9e\u73b0\u6ed1\u52a8\u4e00\u6b21\u79fb\u52a8400px\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u7528\u539f\u751fJS\u4fee\u6539\u9875\u9762\u6eda\u52a8\u8ddd\u79bb\u548c\u901f\u5ea6\uff0c\u5b9e\u73b0\u6ed1\u52a8\u4e00\u6b21\u79fb\u52a8400px\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u4fee\u6539\u9875\u9762\u6eda\u52a8\u901f\u5ea6\uff1aJS\u5b9e\u73b0\u5e73\u6ed1\u6eda\u52a8<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u5982\u4f55\u4fee\u6539\u539f\u751fJS\u4e2d\u7684\u9875\u9762\u6eda\u52a8\u8ddd\u79bb\u548c\u901f\u5ea6\uff0c\u4f7f\u5176\u6ed1\u52a8\u4e00\u6b21\u7684\u8ddd\u79bb\u4ece200px\u53d8\u4e3a400px\uff1f<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<p>\u8981\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7684\u6eda\u52a8\u4f53\u9a8c\uff0c\u53ef\u4ee5\u5229\u7528\u539f\u751fJavaScript\u521b\u5efa\u5e73\u6ed1\u6eda\u52a8\u6548\u679c\u3002\u4ee5\u4e0b\u6b65\u9aa4\u6307\u5bfc\u5982\u4f55\u4fee\u6539\u6eda\u52a8\u8ddd\u79bb\uff1a<\/p>\n<ol>\n<li><strong>\u83b7\u53d6\u8d77\u59cb\u548c\u7ec8\u70b9\u4f4d\u7f6e\uff1a<\/strong>\u4f7f\u7528scrollTop\u5c5e\u6027\u83b7\u53d6\u5f53\u524d\u4f4d\u7f6e\u548c\u76ee\u6807\u4f4d\u7f6e\u3002<\/li>\n<li><strong>\u8ba1\u7b97\u6eda\u52a8\u8ddd\u79bb\uff1a<\/strong>\u7ec8\u70b9\u4f4d\u7f6e\u51cf\u53bb\u8d77\u59cb\u4f4d\u7f6e\u5373\u4e3a\u6eda\u52a8\u8ddd\u79bb\u3002<\/li>\n<li><strong>\u8bbe\u7f6e\u6eda\u52a8\u901f\u5ea6\uff1a<\/strong>\u4f7f\u7528requestAnimationFrame\u65b9\u6cd5\u548c\u65f6\u95f4\u5dee\u8ba1\u7b97\u6bcf\u4e00\u5e27\u7684\u6eda\u52a8\u8ddd\u79bb\uff0c\u786e\u4fdd\u5e73\u6ed1\u7684\u6eda\u52a8\u6548\u679c\u3002<\/li>\n<\/ol>\n<p><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/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\/\/ \u6dfb\u52a0\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>\u901a\u8fc7\u4e8b\u4ef6\u76d1\u542c\u5668\u4e2d\u7684smoothScroll\u51fd\u6570\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u6eda\u52a8\u884c\u4e3a\uff0c\u5e76\u5c06\u5176\u8bbe\u7f6e\u4e3a\u6240\u9700\u7684\u8ddd\u79bb\uff0c\u5982\u4e0a\u4f8b\u4e2d\u7684400px\u3002<\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6587\u7ae0\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   U\u76d8\u4e22\u4e86\uff0c\u7535\u8111\u8bfb\u53d6\u8fc7\u7684\u6587\u4ef6\u80fd\u627e\u5230\u5417\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u7528\u539f\u751fJS\u4fee\u6539\u9875\u9762\u6eda\u52a8\u8ddd\u79bb\u548c\u901f\u5ea6\uff0c\u5b9e\u73b0\u6ed1\u52a8\u4e00\u6b21\u79fb\u52a8400px\uff1f \u5927\u5bb6\u597d\uff0c\u6211\u4eec\u53c8\u89c1\u9762\u4e86\u554a~\u672c\u6587\u300a\u5982\u4f55\u7528\u539f\u751fJS\u4fee\u6539\u9875\u9762\u6eda\u52a8\u8ddd\u79bb\u548c\u901f\u5ea6\uff0c\u5b9e\u73b0\u6ed1\u52a8\u4e00\u6b21\u79fb\u52a8400px\uff1f \u300b\u7684\u5185\u5bb9\u4e2d\u5c06\u4f1a\u6d89\u53ca\u5230\u7b49\u7b49\u3002\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\uff0c\u4ee5\u540e\u4f1a\u7ed9\u5927\u5bb6\u5e26\u6765\u66f4\u591a\u6587\u7ae0\u76f8\u5173\u6587\u7ae0\uff0c\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u8fdb\u6b65\uff01\u4e0b\u9762\u5c31\u5f00\u59cb\u672c\u6587\u7684\u6b63\u5f0f\u5185\u5bb9~ \u4fee\u6539\u9875\u9762\u6eda\u52a8\u901f\u5ea6\uff1aJS\u5b9e\u73b0\u5e73\u6ed1\u6eda\u52a8 \u95ee\u9898\uff1a \u5982\u4f55\u4fee\u6539\u539f\u751fJS\u4e2d\u7684\u9875\u9762\u6eda\u52a8\u8ddd\u79bb\u548c\u901f\u5ea6\uff0c\u4f7f\u5176\u6ed1\u52a8\u4e00\u6b21\u7684\u8ddd\u79bb\u4ece200px\u53d8\u4e3a400px\uff1f \u89e3\u51b3\u65b9\u6848\uff1a \u8981\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7684\u6eda\u52a8\u4f53\u9a8c\uff0c\u53ef\u4ee5\u5229\u7528\u539f\u751fJavaScript\u521b\u5efa\u5e73\u6ed1\u6eda\u52a8\u6548\u679c\u3002\u4ee5\u4e0b\u6b65\u9aa4\u6307\u5bfc\u5982\u4f55\u4fee\u6539\u6eda\u52a8\u8ddd\u79bb\uff1a \u83b7\u53d6\u8d77\u59cb\u548c\u7ec8\u70b9\u4f4d\u7f6e\uff1a\u4f7f\u7528scrollTop\u5c5e\u6027\u83b7\u53d6\u5f53\u524d\u4f4d\u7f6e\u548c\u76ee\u6807\u4f4d\u7f6e\u3002 \u8ba1\u7b97\u6eda\u52a8\u8ddd\u79bb\uff1a\u7ec8\u70b9\u4f4d\u7f6e\u51cf\u53bb\u8d77\u59cb\u4f4d\u7f6e\u5373\u4e3a\u6eda\u52a8\u8ddd\u79bb\u3002 \u8bbe\u7f6e\u6eda\u52a8\u901f\u5ea6\uff1a\u4f7f\u7528requestAnimationFrame\u65b9\u6cd5\u548c\u65f6\u95f4\u5dee\u8ba1\u7b97\u6bcf\u4e00\u5e27\u7684\u6eda\u52a8\u8ddd\u79bb\uff0c\u786e\u4fdd\u5e73\u6ed1\u7684\u6eda\u52a8\u6548\u679c\u3002 \u4ee3\u7801\u793a\u4f8b\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 = timestamp; 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-53153","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53153","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=53153"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53153\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=53153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=53153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=53153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}