{"id":8073,"date":"2018-10-21T01:27:10","date_gmt":"2018-10-20T17:27:10","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=8073"},"modified":"2019-12-16T15:17:47","modified_gmt":"2019-12-16T07:17:47","slug":"scroll-behavior-scrollintoview-%e5%b9%b3%e6%bb%91%e6%bb%9a%e5%8a%a8","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2018\/10\/scroll-behavior-scrollintoview-%e5%b9%b3%e6%bb%91%e6%bb%9a%e5%8a%a8\/","title":{"rendered":"CSS scroll-behavior\u548cJS scrollIntoView\u8ba9\u9875\u9762\u6eda\u52a8\u5e73\u6ed1"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=8073\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=8073<\/a><br \/>\n\u672c\u6587\u53ef\u5168\u6587\u8f6c\u8f7d\uff0c\u4e2a\u4eba\u7f51\u7ad9\u65e0\u9700\u6388\u6743\uff0c\u53ea\u8981\u4fdd\u7559\u539f\u4f5c\u8005\u3001\u51fa\u5904\u4ee5\u53ca\u6587\u4e2d\u94fe\u63a5\u5373\u53ef\uff0c\u4efb\u4f55\u7f51\u7ad9\u5747\u53ef\u6458\u8981\u805a\u5408\uff0c\u5546\u7528\u8bf7\u8054\u7cfb\u6388\u6743\u3002<\/p>\n<h3>\u4e00\u3001\u5f15\u8a00\u5f00\u59cb\u8a00\u5f00\u5934\u8a00<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201810\/scroll-smooth-thumb.jpg\" width=\"484\" height=\"300\" alt=\"\u6d4f\u89c8\u5668\u539f\u751f\u5e73\u6ed1\u6eda\u52a8\u7f29\u7565\u56fe\" class=\"alignnone size-medium\" style=\"position:absolute;clip:rect(0 0 0 0);\" \/><\/p>\n<h4>1. \u8fd9\u4e9b\u5e74\u81ea\u5df1\u6b65\u5b50\u6162\u4e86<\/h4>\n<p>\u8fd9\u4e9b\u5e74\u81ea\u5df1\u5728\u65e0\u969c\u788d\u8bbf\u95ee\uff0cSVG\u548cCanvas\u8fd9\u4e9b\u504f\u5c0f\u4f17\u7684\u9886\u57df\u82b1\u4e86\u5927\u91cf\u7684\u5b66\u4e60\u7cbe\u529b\uff0c\u4ee5\u81f3\u4e8e\u5f88\u591a\u524d\u7aef\u65b0\u7279\u6027\uff0c\u65b0\u6280\u672f\u6ca1\u80fd\u53ca\u65f6\u5173\u6ce8\u548c\u4e86\u89e3\uff0c\u6709CSS3\u9886\u57df\u7684\u65b0\u5c5e\u6027\uff0c\u6709JS\u9886\u57df\u7684\u65b0API\uff0c\u5305\u62ec\u5168\u65b0\u7684ES6\/ES7\u8bed\u6cd5\u7b49\uff0c\u76f8\u6bd4\u4ee5\u524d\u7684\u5b66\u4e60\uff0c\u6b65\u5b50\u786e\u5b9e\u6162\u4e86\u3002\u6bd4\u65b9\u8bf4\uff0c\u672c\u6587\u8981\u4ecb\u7ecd\u7684\u5e73\u6ed1\u6eda\u52a8\uff0c\u65e0\u8bba\u662fCSS\u8fd8\u662fJS\uff0c\u73b0\u4ee3\u6d4f\u89c8\u5668\u90fd\u63d0\u4f9b\u4e86\u539f\u751f\u7684\u5c5e\u6027\u6216\u65b9\u6cd5\u652f\u6301\uff0c\u800c\u4e14\u5dee\u4e0d\u591a\u5df2\u7ecf\u6709\u4e00\u5e74\u65f6\u95f4\uff0c\u800c\u6211\u6700\u8fd1\u624d\u77e5\u9053\uff0c\u548c\u6570\u5e74\u524d\u5b9e\u65f6\u5173\u6ce8\u65b0\u6280\u672f\u7684\u81ea\u5df1\u5f62\u6210\u4e86\u660e\u663e\u7684\u5bf9\u6bd4\u3002<\/p>\n<p>\u4e0d\u8fc7\u968f\u7740SVG\u548cCanvas\u7684\u57fa\u7840\u8d8a\u6765\u8d8a\u7262\u56fa\uff0c\u7ec6\u8282\u8d8a\u6765\u8d8a\u6df1\u5165\uff0c\u6211\u7684\u5b66\u4e60\u91cd\u5fc3\u5df2\u7ecf\u5f00\u59cb\u5f80\u5e7f\u5ea6\u65b9\u5411\u8f6c\u79fb\uff0c\u6162\u6162\u62fe\u8d77\u90a3\u4e9b\u9057\u843d\u7684\u9ea6\u7a57\u3002<\/p>\n<h4>2. \u4ece\u5e72\u5df4\u5df4\u5230\u53c8\u6e7f\u53c8\u6ed1<\/h4>\n<p>\u5047\u8bbe\u9875\u9762\u4e2d\u6709\u4e0b\u9762\u8fd9\u4e00\u6bb5HTML\uff1a<\/p>\n<pre>&lt;a href=\"#\" rel=\"internal\"&gt;\u8fd4\u56de\u9876\u90e8&lt;\/a&gt;<\/pre>\n<p>\u5e2e\u6211\u4eec\u70b9\u51fb\u201c\u8fd4\u56de\u9876\u90e8\u201d\u8fd9\u4e2a\u6587\u5b57\u94fe\u63a5\u7684\u65f6\u5019\uff0c\u9875\u9762\u5c31\u4f1a\u201c\u5530\u201d\u5730\u4e00\u58f0\u77ac\u95f4\u5b9a\u4f4d\u5230\u6d4f\u89c8\u5668\u9876\u90e8\uff0c\u901f\u5ea6\u4e4b\u5feb\uff0c\u5347\u52bf\u4e4b\u731b\uff0c\u4ee5\u81f3\u4e8e\u4f60\u6ca1\u6709\u4efb\u4f55\u53cd\u5e94\uff0c\u5c31\u50cf\u66b4\u98ce\u96e8\u822c\u7684\u7231\u60c5\uff0c\u8ba9\u4f60\u731d\u4e0d\u53ca\u9632\uff0c\u8111\u4e2d\u4e0d\u65ad\u56de\u60f3\uff1a\u201c\u6211\u5728\u54ea\u91cc\uff1f\u6211\u662f\u8c01\uff1f\u6211\u521a\u521a\u505a\u4e86\u4ec0\u4e48\uff1f\u201d<\/p>\n<p>\u6240\u6709\u8fd9\u4e9b\u53cd\u5e94\u7684\u4ea7\u751f\uff0c\u5f52\u6839\u7ed3\u5e95\u5728\u4e8e\u6211\u4eec\u8fd9\u4e2a\u6eda\u52a8\u6548\u679c\u5b9e\u5728\u662f\u592a\u5e72\u5df4\u5df4\u4e86\uff0c\u673a\u68b0\u751f\u786c\u6ca1\u6709\u7075\u6027\u4e0d\u89e3\u98ce\u60c5\u3002<\/p>\n<p>\u4eba\u662f\u6709\u60c5\u611f\u7684\u52a8\u7269\uff0c\u65e2\u7136\u6211\u4eec\u505a\u7684\u7f51\u9875\u662f\u7ed9\u4eba\u7528\u7684\uff0c\u90a3\u81ea\u7136\u5145\u6ee1\u60c5\u611f\uff0c\u4e0d\u8981\u90a3\u4e48\u5e72\u5df4\u5df4\uff0c\u8ba9\u6eda\u52a8\u6548\u679c\u53c8\u6e7f\u53c8\u6ed1\uff0c\u7528\u6237\u4f53\u9a8c\u5c31\u4f1a\u597d\u3002<\/p>\n<p>\u5149\u6ed1\u5982\u96ea\u7684\u808c\u80a4\uff0c\u9971\u6ee1\u6e7f\u6da6\u7684\u7ea2\u5507\uff0c\u662f\u4e0d\u662f\u603b\u80fd\u8ba9\u4eba\u5fc3\u52a8\u4e0d\u5df2\uff1f\u5982\u679c\u6211\u4eec\u7684\u4ea4\u4e92\u6548\u679c\u4e5f\u80fd\u505a\u5f97\u5982\u6b64\uff0c\u4e5f\u540c\u6837\u4e5f\u80fd\u8ba9\u7528\u6237\u5fc3\u52a8\u3002<\/p>\n<p>\u6d4f\u89c8\u5668\u4f3c\u4e4e\u4e5f\u610f\u8bc6\u5230\u8fd9\u4e00\u70b9\uff0c\u4ece\u53bb\u5e74\u5e74\u5e95\u5f00\u59cb\uff0c\u5df2\u7ecf\u5f00\u59cb\u652f\u6301\u6d4f\u89c8\u5668\u7684\u539f\u751f\u5e73\u6ed1\u6eda\u52a8\u5b9a\u4f4d\uff0cCSS <code>scroll-behavior<\/code>\u5c5e\u6027\u548cJS <code>scrollIntoView()<\/code>\u65b9\u6cd5\u90fd\u53ef\u4ee5\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201810\/2018-10-20_101654.png\" width=\"537\" height=\"251\" alt=\"scroll-behavior\u6d4f\u89c8\u5668\u517c\u5bb9\u6027\" class=\"alignnone size-medium\" \/><\/p>\n<p>OK\uff0c\u4e0b\u9762\u5f00\u59cb\u8fdb\u5165\u6b63\u9898\u3002<img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/emtion\/emoji\/1f61d.svg\" width=\"20\" height=\"20\" align=\"absmiddle\"><\/p>\n<h3>\u4e8c\u3001CSS scroll-behavior\u4e0e\u5e73\u6ed1\u6eda\u52a8<\/h3>\n<p><code>scroll-behavior:smooth<\/code>\u5199\u5728\u6eda\u52a8\u5bb9\u5668\u5143\u7d20\u4e0a\uff0c\u53ef\u4ee5\u8ba9\u5bb9\u5668\uff08\u975e\u9f20\u6807\u624b\u52bf\u89e6\u53d1\uff09\u7684\u6eda\u52a8\u53d8\u5f97\u5e73\u6ed1\u3002<\/p>\n<h4>\u8bed\u6cd5<\/h4>\n<pre>scroll-behavior: auto;\r\nscroll-behavior: smooth;<\/pre>\n<p>\u521d\u59cb\u503c\u662f<code>'auto'<\/code>\u3002<\/p>\n<p>\u6211\u4eec\u5148\u770b\u4e00\u4e2a\u5b9e\u9645\u7684\u6848\u4f8b\u3002<\/p>\n<p>8\u5e74\u524d\u201c<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=1108\">\u65e0JavaScript\u7eafCSS\u5b9e\u73b0\u9009\u9879\u5361\u8f6e\u8f6c\u5207\u6362\u6548\u679c<\/a>\u201d\u8fd9\u7bc7\u6587\u7ae0\u4ecb\u7ecd\u4e86\u4e00\u79cd\u5229\u7528\u951a\u70b9\u5b9a\u4f4d\u7eafCSS\u5b9e\u73b0\u9009\u9879\u5361\u5207\u6362\u7684\u6280\u672f\uff08\u672c\u8d28\u4e0a\u662f\u89e6\u53d1\u6eda\u52a8\u6761\u6eda\u52a8\uff09\u3002<\/p>\n<p>\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/study\/201009\/anchor-and-div-show.html\" rel=\"noopener\" target=\"_blank\">\u65e0JavaScript\u5b9e\u73b0\u7684\u5207\u6362\u6548\u679cdemo<\/a><\/p>\n<p>\u5b9e\u73b0\u540e\u7684\u6548\u679c\u53c2\u89c1\u4e0bGIF\u622a\u5c4f\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201810\/anchor-tab.gif\" width=\"217\" height=\"143\" alt=\"\u951a\u70b9\u9009\u9879\u5361\u5207\u6362\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u57fa\u672c\u529f\u80fd\u53ef\u4ee5\u6ee1\u8db3\uff0c\u4f46\u6709\u4e24\u4e2a\u95ee\u9898\uff0c\u4e00\u662f\u7531\u4e8e\u6539\u53d8location\u7684hash\u5b9e\u73b0\u7684\u5b9a\u4f4d\uff0c\u4f1a\u89e6\u53d1\u6d4f\u89c8\u5668\u539f\u751f\u6eda\u52a8\u884c\u4e3a\uff0c\u4f53\u9a8c\u4e0d\u597d\uff1b\u4e8c\u662f\u9009\u9879\u5361\u5185\u5bb9\u7684\u5207\u6362\u201c\u90a6\u90a6\u90a6\u201d\u8fc7\u4e8e\u751f\u786c\u3002<\/p>\n<p>\u4e8e\u662f\uff0c\u540e\u6765\uff0c\u6211\u53d1\u660e\u4e86\u4e00\u79cd\u57fa\u4e8e\u63a7\u4ef6\u5143\u7d20focus\u89e6\u53d1\u6eda\u52a8\u91cd\u5b9a\u4f4d\u7684\u7279\u6027\u5b9e\u73b0\u7684\u7eafCSS\u9009\u9879\u5361\u5207\u6362\u6548\u679c\uff0c\u5728\u300aCSS\u4e16\u754c\u300b\u8fd9\u672c\u4e66overflow\u7ae0\u8282\u6709\u63d0\u5230\uff0c\u60a8\u53ef\u4ee5\u72e0\u72e0\u7684\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"https:\/\/demo.cssworld.cn\/6\/4-3.php\" rel=\"noopener\" target=\"_blank\" class=\"a_link\">focus\u951a\u70b9\u5b9a\u4f4d\u548coverflow\u7684\u9009\u9879\u5361\u5207\u6362demo<\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201810\/css-focus-tab.gif\" width=\"318\" height=\"222\" alt=\"focus\u8f93\u5165\u6846\u4e0e\u9009\u9879\u5361\u5207\u6362\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u4e5f\u662f\u7eafCSS\u5b9e\u73b0\uff0c\u6ca1\u6709\u4efb\u4f55JavaScript\u4ee3\u7801\uff0c\u76f8\u6bd4\u76f4\u63a5\u5229\u7528<code>&lt;a&gt;<\/code>\u5143\u7d20\u7684<code>href<\/code>\u951a\u70b9\u8df3\u8f6c\u65b9\u6cd5\uff0c\u6b64\u65b9\u6cd5\u4e0d\u4f1a\u89e6\u53d1\u6d4f\u89c8\u5668\u5916\u90e8\u7a97\u4f53\u7684\u6eda\u52a8\uff0c\u4f53\u9a8c\u66f4\u4e0a\u4e00\u5c42\u697c\uff0c\u4f46\u662f\u8fd8\u6709\u4e00\u4e2a\u95ee\u9898\uff0c\u90a3\u5c31\u662f\u9009\u9879\u5361\u5185\u5bb9\u5207\u6362\u7684\u65f6\u5019\uff0c\u8fd8\u662f\u201c\u90a6\u90a6\u90a6\u201d\u8fd9\u79cd\u5e72\u5df4\u5df4\u7684\u6548\u679c\uff0c\u5e76\u6ca1\u6709\u6ed1\u6765\u6ed1\u53bb\u90a3\u79cd\u6e7f\u6e7f\u7684\u6548\u679c\u3002<\/p>\n<p>\u4e0d\u8981\u62c5\u5fc3\uff0c\u73b0\u5728\u6709\u4e86CSS scroll-behavior\uff0c\u5219\u5e73\u6ed1\u6eda\u52a8\u7684\u95ee\u9898\u4e5f\u53ef\u4ee5\u89e3\u51b3\u4e86\u3002\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/study\/201810\/css-scroll-behavior.php\" rel=\"noopener\" target=\"_blank\">CSS scroll-behavior\u9009\u9879\u5361\u5e73\u6ed1\u6eda\u52a8demo<\/a><\/p>\n<p>\u76f8\u6bd4\u4e4b\u524d\u5e72\u5df4\u5df4\u7684\u5b9e\u73b0\uff0c\u5c31\u591a\u4e86\u8fd9\u4e48\u4e00\u53e5CSS\u2014\u2014<code>scroll-behavior:smooth<\/code>\uff1a<\/p>\n<pre>.box {\r\n    <span style=\"color:#cd0000;\">scroll-behavior: smooth;<\/span> \r\n    overflow: hidden; \r\n}<\/pre>\n<p>\u7ed3\u679c\u4e00\u80a1\u5982\u6c90\u6625\u98ce\u7684\u4ea4\u4e92\u6548\u679c\u6251\u9762\u800c\u6765\uff0c\u53c2\u89c1\u4e0b\u9762\u89c6\u9891\u622a\u5c4f\u6548\u679c<span class=\"hidden\">\uff08\u82e5\u65e0\u6548\u679c\uff0c\u8bbf\u95ee<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=8073\">\u539f\u6587\u5730\u5740<\/a>\uff09<\/span>\uff1a<\/p>\n<p><video src=\/\/image.zhangxinxu.com\/video\/blog\/201810\/tab-switch-smooth.mp4 type=\"video\/mp4\" width=\"330\" height=\"248\" preload=\"auto\" autoplay autobuffer loop x-webkit-airplay=\"allow\" x5-video-player-type=\"h5\" x5-video-player-fullscreen=\"true\" x5-video-orientation=\"portraint\" webkit-playsinline=\"true\" playsinline=\"true\" poster=\"\/\/image.zhangxinxu.com\/image\/blog\/201806\/poster.png\"><\/video><\/p>\n<p>\u4e00\u884cCSS\u5c31\u53ef\u4ee5\u9526\u4e0a\u6dfb\u82b1\u3002<\/p>\n<h4>\u66f4\u7b80\u5355\u66f4\u5b9e\u9645\u7684\u7528\u9014<\/h4>\n<p>\u5176\u5b9e<code>scroll-behavior<\/code>\u7684\u4f7f\u7528\u6ca1\u6709\u90a3\u4e48\u591a\u82b1\u5934\uff0c\u4f60\u5c31\u8bb0\u4f4f\u8fd9\u4e48\u4e00\u53e5\u8bdd\u2014\u2014<\/p>\n<p><strong style=\"font-size:150%; border: 1px dotted; font-family: STkaiti, Kaiti;\">\u51e1\u662f\u9700\u8981\u6eda\u52a8\u7684\u5730\u65b9\u90fd\u52a0\u4e00\u53e5scroll-behavior:smooth\u5c31\u597d\u4e86\uff01<\/strong><\/p>\n<p>\u4f60\u522b\u7ba1\u4ed6\u7528\u4e0d\u7528\u5f97\u5230\uff0c\u4e5f\u4e0d\u7528\u7ba1\u6d4f\u89c8\u5668\u517c\u5bb9\u6027\u5982\u4f55\uff0c\u4f60\u90fd\u52a0\u4e0a\u3002\u8fd9\u5c31\u50cf\u4e00\u4e2a\u4e0d\u8981\u94b1\u7684\u514d\u8d39\u62bd\u5956\uff0c\u6ca1\u6709\u4e2d\u5956\uff0c\u6ca1\u5173\u7cfb\uff0c\u53c8\u6ca1\u4ec0\u4e48\u635f\u5931\uff0c\u4e2d\u5956\u4e86\u81ea\u7136\u597d\uff0c\u9526\u4e0a\u6dfb\u82b1\uff01<code>scroll-behavior:smooth<\/code>\u5c31\u662f\u8fd9\u79cd\u5c3f\u6027\u3002<\/p>\n<p>\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5728PC\u6d4f\u89c8\u5668\u4e2d\uff0c\u7f51\u9875\u9ed8\u8ba4\u6eda\u52a8\u662f\u5728<code>&lt;html&gt;<\/code>\u6807\u7b7e\u4e0a\u7684\uff0c\u79fb\u52a8\u7aef\u5927\u591a\u6570\u5728<code>&lt;body&gt;<\/code>\u6807\u7b7e\u4e0a\uff0c\u4e8e\u662f\uff0c\u6211\u52a0\u4e0a\u8fd9\u4e48\u4e00\u53e5\uff1a<\/p>\n<pre>html, body { scroll-behavior:smooth; }<\/pre>\n<style>html, body { scroll-behavior:smooth; }<\/style>\n<p>\u6b64\u65f6\uff0c\u70b9\u51fb\u4e0b\u9762\u8fd9\u4e2a\u201c\u8fd4\u56de\u9876\u90e8\u201d\u94fe\u63a5\uff0c\u5c31\u4f1a\u5e73\u6ed1\u6eda\u52a8\u5230\u9876\u90e8\uff08\u771f\u00b7\u5b9e\u65f6\u6548\u679c\uff0c\u53ef\u4ee5\u70b9\u51fb\u5c1d\u8bd5\uff09\u3002<\/p>\n<p><a href=\"#\" style=\"background-color: #ffffe0;\">\u8fd4\u56de\u9876\u90e8<\/a><\/p>\n<p>HTML\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;a href=\"#\"&gt;\u8fd4\u56de\u9876\u90e8&lt;\/a&gt;<\/pre>\n<hr>\n<p>\u4ece\u8fd9\u4e00\u70b9\u6765\u770b\uff0c\u4e1a\u754c\u6d4f\u89c8\u5668\u7684CSS reset\u90fd\u53ef\u4ee5\u52a0\u4e0a\u8fd9\u4e48\u4e00\u6761\u89c4\u5219\uff1a<\/p>\n<pre>html, body { scroll-behavior:smooth; }<\/pre>\n<h3>\u4e09\u3001JS scrollIntoView\u4e0e\u5e73\u6ed1\u6eda\u52a8<\/h3>\n<p>DOM\u5143\u7d20\u7684<code>scrollIntoView()<\/code>\u65b9\u6cd5\u662f\u4e00\u4e2aIE6\u6d4f\u89c8\u5668\u4e5f\u652f\u6301\u7684\u539f\u751fJS API\uff0c\u53ef\u4ee5\u8ba9\u5143\u7d20\u8fdb\u5165\u89c6\u533a\uff0c\u901a\u8fc7\u89e6\u53d1\u6eda\u52a8\u5bb9\u5668\u7684\u5b9a\u4f4d\u5b9e\u73b0\u3002<\/p>\n<p>\u968f\u7740Chrome\u548cFirefox\u6d4f\u89c8\u5668\u5f00\u59cb\u652f\u6301CSS <code>scroll-behavior<\/code>\u5c5e\u6027\uff0c\u987a\u4fbf\u5bf9\uff0c<code>scrollIntoView()<\/code>\u65b9\u6cd5\u8fdb\u884c\u4e86\u5347\u7ea7\uff0c\u4f7f\u652f\u6301\u66f4\u591a\u53c2\u6570\uff0c\u5176\u4e2d\u4e00\u4e2a\u53c2\u6570\u5c31\u662f\u53ef\u4ee5\u4f7f\u6eda\u52a8\u5e73\u6ed1\u3002<\/p>\n<p>\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>target.scrollIntoView({\r\n    behavior: \"smooth\"\r\n});<\/pre>\n<p>\u6211\u4eec\u968f\u4fbf\u6253\u5f00\u4e00\u4e2a\u6709\u94fe\u63a5\u7684\u9875\u9762\uff0c\u628a\u9996\u4e2a\u94fe\u63a5\u6eda\u52a8\u5230\u5c4f\u5e55\u5916\uff0c\u7136\u540e\u63a7\u5236\u53f0\u8f93\u5165\u7c7b\u4f3c\u4e0b\u9762\u4ee3\u7801\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u770b\u5230\u9875\u9762\u5e73\u6ed1\u6eda\u52a8\u5b9a\u4f4d\u4e86\uff1a<\/p>\n<pre>document.links[0].scrollIntoView({\r\n    behavior: \"smooth\"\r\n});<\/pre>\n<p>\u5982\u4e0b\u89c6\u9891\u622a\u5c4f\uff1a<\/p>\n<p><video src=\/\/image.zhangxinxu.com\/video\/blog\/201810\/link-smooth-scroll.mp4 type=\"video\/mp4\" width=\"610\" height=\"374\" preload=\"auto\" autoplay autobuffer loop x-webkit-airplay=\"allow\" x5-video-player-type=\"h5\" x5-video-player-fullscreen=\"true\" x5-video-orientation=\"portraint\" webkit-playsinline=\"true\" playsinline=\"true\" poster=\"\/\/image.zhangxinxu.com\/image\/blog\/201806\/poster.png\"><\/video><\/p>\n<p><strong>\u5176\u5b83\uff1a<\/strong><\/p>\n<ul>\n<li><code>scrollIntoView()<\/code>\u5347\u7ea7\u540e\u7684\u65b9\u6cd5\uff0c\u9664\u4e86\u652f\u6301<code>'behavior'<\/code>\uff0c\u8fd8\u6709<code>'block'<\/code>\u548c<code>'inline'<\/code>\u7b49\u53c2\u6570\uff0c\u6709\u5174\u8da3\u53ef\u4ee5\u53c2\u9605<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Element\/scrollIntoView\" rel=\"noopener nofollow\" target=\"_blank\">MDN\u76f8\u5173\u6587\u6863<\/a>\u3002<\/li>\n<li>\u5982\u679c\u6211\u4eec\u7684\u7f51\u9875\u5df2\u7ecf\u901a\u8fc7CSS\u8bbe\u7f6e\u4e86<code>scroll-behavior:smooth<\/code>\u58f0\u660e\uff0c\u5219\u6211\u4eec\u76f4\u63a5\u6267\u884c<code>target.scrollIntoView()<\/code>\u65b9\u6cd5\u5c31\u4f1a\u6709\u5e73\u6ed1\u6eda\u52a8\uff0c\u65e0\u9700\u518d\u989d\u5916\u8bbe\u7f6e<code>behavior<\/code>\u53c2\u6570\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u662f\u5728\u946b\u7a7a\u95f4\u539f\u7ad9\u6d4f\u89c8\u7684\u6b64\u6587\u7ae0\uff0c\u6253\u5f00\u63a7\u5236\u53f0\uff0c\u6267\u884c\u4e0b\u9762\u4ee3\u7801\uff0c\u5c31\u53ef\u4ee5\u770b\u5230\u5e73\u6ed1\u6eda\u52a8\u6548\u679c\u4e86\uff1a\n<pre>document.forms[0].scrollIntoView();<\/pre>\n<\/li>\n<\/ul>\n<h3>\u56db\u3001JS\u5e73\u6ed1\u6eda\u52a8\u5411\u4e0b\u517c\u5bb9\u5904\u7406<\/h3>\n<p>JS\u5b9e\u73b0\u5e73\u6ed1\u6eda\u52a8\u5e76\u4e0d\u96be\uff0cjQuery\u4e2d<code>animate()<\/code>\u65b9\u6cd5\uff1a<\/p>\n<pre>scrollContainer.animate({\r\n    scrollTop: 0\r\n});<\/pre>\n<p>\u6216\u8005\u4f7f\u7528<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=3695\" rel=\"noopener\" target=\"_blank\">requestAnimationFrame API<\/a>\u8fd9\u7c7b\u539f\u751fJS\u4e5f\u80fd\u5b9e\u73b0\u3002\u4f8b\u5982\u4e0b\u9762\u8fd9\u4e2a\u6211\u901f\u5199\u7684\u4e2a\u65b9\u6cd5\uff1a<\/p>\n<pre><span style=\"color:green;\">\/**\r\n @description \u9875\u9762\u5782\u76f4\u5e73\u6ed1\u6eda\u52a8\u5230\u6307\u5b9a\u6eda\u52a8\u9ad8\u5ea6\r\n @author zhangxinxu(.com)\r\n*\/<\/span>\r\nvar scrollSmoothTo = function (position) {\r\n    if (!window.requestAnimationFrame) {\r\n        window.requestAnimationFrame = function(callback, element) {\r\n            return setTimeout(callback, 17);\r\n        };\r\n    }\r\n    <span style=\"color:green;\">\/\/ \u5f53\u524d\u6eda\u52a8\u9ad8\u5ea6<\/span>\r\n    var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;\r\n    <span style=\"color:green;\">\/\/ \u6eda\u52a8step\u65b9\u6cd5<\/span>\r\n    var step = function () {\r\n        <span style=\"color:green;\">\/\/ \u8ddd\u79bb\u76ee\u6807\u6eda\u52a8\u8ddd\u79bb<\/span>\r\n        var distance = position - scrollTop;\r\n        <span style=\"color:green;\">\/\/ \u76ee\u6807\u6eda\u52a8\u4f4d\u7f6e<\/span>\r\n        scrollTop = scrollTop + distance \/ 5;\r\n        if (Math.abs(distance) &lt; 1) {\r\n            window.scrollTo(0, position);\r\n        } else {\r\n            window.scrollTo(0, scrollTop);\r\n            requestAnimationFrame(step);\r\n        }\r\n    };\r\n    step();\r\n};<\/pre>\n<p>\u4f7f\u7528\u7684\u662f<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/2017\/01\/share-a-animation-algorithm-js\/\">\u81ea\u5df1\u79c1\u85cf\u7f13\u52a8\u52a8\u753bJS\u5c0f\u7b97\u6cd5<\/a>\uff0c\u6eda\u52a8\u5148\u5feb\u540e\u6162\u3002<\/p>\n<p>\u4f7f\u7528\u5982\u4e0b\uff0c\u4f8b\u5982\uff0c\u6211\u4eec\u5e0c\u671b\u7f51\u9875\u5e73\u6ed1\u6eda\u52a8\u5230\u9876\u90e8\uff0c\u76f4\u63a5\uff1a<\/p>\n<pre>scrollSmoothTo(0);<\/pre>\n<p>\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<hr>\n<p>\u96be\u7684\u662f\u5982\u4f55\u652f\u6301\u5e73\u6ed1\u6eda\u52a8\u7684\u6d4f\u89c8\u5668\u539f\u751f\u5904\u7406\uff0c\u4e0d\u652f\u6301\u7684\u6d4f\u89c8\u5668\u8fd8\u662f\u4f7f\u7528\u8001\u7684JS\u65b9\u6cd5\u5904\u7406\u3002<\/p>\n<p>\u6211\u662f\u8fd9\u4e48\u5904\u7406\u7684JS\u5982\u4e0b\u5224\u65ad\uff1a<\/p>\n<pre>if (typeof window.getComputedStyle(document.body).scrollBehavior == 'undefined') {\r\n   <span style=\"color:green;\">\/\/ \u4f20\u7edf\u7684JS\u5e73\u6ed1\u6eda\u52a8\u5904\u7406\u4ee3\u7801...<\/span>\r\n}<\/pre>\n<p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u65e0\u7f1d\u5bf9\u63a5\u4e86\u3002<\/p>\n<h3>\u4e94\u3001\u7ed3\u8bed\u7ed3\u5c3e\u8bed\u7ed3\u675f\u8bed<\/h3>\n<p>\u6ca1\u6709\u4e2a\u4eba\u7279\u8d28\u7684\u6587\u7ae0\u5c31\u50cf\u6ca1\u6709\u7075\u9b42\u7684\u673a\u5668\u4e00\u6837\u3002\u6000\u5ff5\u5f20\u542b\u97f5\u7684\u7167\u7247\uff0c\u5bb6\u91cc\u9886\u5bfc\u4e0d\u653e\u5fc3\uff0c\u6015\u4fb5\u72af\u7248\u6743\u627e\u4e0a\u95e8\uff0c\u90fd\u7ed9\u4e0b\u6389\u4e86\uff0c\u73b0\u5728\u7684\u90a3\u4e9b\u7f8e\u5973\u7167\u7247\uff0c\u5c31\u50cf\u767d\u5f00\u6c34\u4e00\u6837\uff0c\u7d22\u7136\u65e0\u5473\u2026\u2026<\/p>\n<p>\u6211\u7422\u78e8\u7740\u8981\u6362\u4e2a\u89d2\u8272\u6216\u8005\u6807\u7b7e\uff0c\u4f8b\u5982\u2026\u2026\u4e00\u5339\u9a6c\uff01<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201810\/2018-10-21_010855.png\" width=\"287\" height=\"163\" alt=\"\u4e00\u5339\u9a6c\u7684\u547c\u5524\" class=\"alignnone size-medium\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/emtion\/emoji\/1f644.svg\" width=\"20\" height=\"20\" align=\"absmiddle\"><\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u4f1a\u7ecf\u5e38\u66f4\u65b0\u77e5\u8bc6\u70b9\u4ee5\u53ca\u4fee\u6b63\u4e00\u4e9b\u9519\u8bef\uff0c\u56e0\u6b64\u8f6c\u8f7d\u8bf7\u4fdd\u7559\u539f\u51fa\u5904\uff0c\u65b9\u4fbf\u6eaf\u6e90\uff0c\u907f\u514d\u9648\u65e7\u9519\u8bef\u77e5\u8bc6\u7684\u8bef\u5bfc\uff0c\u540c\u65f6\u6709\u66f4\u597d\u7684\u9605\u8bfb\u4f53\u9a8c\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=8073\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=8073<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"\/\/image.zhangxinxu.com\/image\/blog\/201810\/scroll-smooth-thumb.jpg\" width=\"161\" height=\"100\" alt=\"\u6d4f\u89c8\u5668\u539f\u751f\u5e73\u6ed1\u6eda\u52a8\u7f29\u7565\u56fe\" class=\"alignright size-medium\"\/><\/p>\n<p>\u4ece\u53bb\u5e74\u5e74\u5e95\u5f00\u59cb\uff0c\u6d4f\u89c8\u5668\u5df2\u7ecf\u5f00\u59cb\u652f\u6301\u7684\u539f\u751f\u5e73\u6ed1\u6eda\u52a8\u5b9a\u4f4d\uff0cCSS scroll-behavior\u5c5e\u6027\u548cJS scrollIntoView()\u65b9\u6cd5\u90fd\u53ef\u4ee5\u3002\u672c\u6587\u5c31\u5c06\u634b\u4e00\u634b\u8fd9\u4e9b\u5e73\u6ed1\u6eda\u52a8\u65b0\u7279\u6027\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u5b9e\u9645\u6548\u679c\uff0c\u4ee5\u53ca\u5982\u4f55\u5728\u5b9e\u9645\u4e2d\u5e94\u7528\u7b49\u3002\u4e00\u5982\u65e2\u5f80\uff0c\u4e30\u5bcc\u914d\u56fe\uff0c\u542bGIF\u6f14\u793a\u548c\u89c6\u9891\u622a\u5c4f\uff0c\u5fc5\u8981\u7684\u6e90\u4ee3\u7801\u5c55\u793a\u548c\u8be6\u5c3d\u7684demo\uff0c\u5e0c\u671b\u672c\u6587\u5185\u5bb9\u80fd\u591f\u5bf9\u60a8\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,1284],"tags":[177,1271,1273,867,1327,624,1328,987,124],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/8073"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=8073"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/8073\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=8073"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=8073"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=8073"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}