{"id":152775,"date":"2020-09-07T18:13:54","date_gmt":"2020-09-07T10:13:54","guid":{"rendered":"http:\/\/4563.org\/?p=152775"},"modified":"2020-09-07T18:13:54","modified_gmt":"2020-09-07T10:13:54","slug":"%e5%8f%aa%e7%94%a8-javascript-%e6%80%8e%e4%b9%88%e5%ae%9e%e7%8e%b0%e5%b9%b3%e6%bb%91%e7%9a%84-scroll-snap%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=152775","title":{"rendered":"\u53ea\u7528 JavaScript \u600e\u4e48\u5b9e\u73b0\u5e73\u6ed1\u7684 scroll snap\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u53ea\u7528 JavaScript \u600e\u4e48\u5b9e\u73b0\u5e73\u6ed1\u7684 scroll snap\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : AlphaRobert <\/span>  <span><i><\/i> 0<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p><strong>\u6392\u9664 CSS \u7684 <code>scroll-snap<\/code><\/strong>\uff0c\u5728 Safari \u4e0b\u4f1a\u5947\u602a\u5730\u5e26\u52a8 <code>body<\/code> \u6216\u5176\u4ed6\u5148\u7956\u5143\u7d20\u6eda\u52a8\uff0c\u4e14\u6d4f\u89c8\u5668\u517c\u5bb9\u4e5f\u4e0d\u597d\u3002<\/p>\n<p>\u5982\u679c\u6709\u5f3a\u8feb\u75c7\u7684\u8bdd\uff0c\u5b9a\u65f6\u4e00\u6bb5\u65f6\u95f4\u68c0\u6d4b\u6eda\u52a8\u505c\u6b62\u540e\uff0c\u518d\u6eda\u52a8\u5230\u6700\u8fd1\u7684\u4f4d\u7f6e\u4e0d\u662f\u5f88\u4e1d\u6ed1\u3002\u5047\u8bbe\u5185\u5bb9\u4e3a\u4e00\u5806 <code>24px<\/code> \u9ad8\u7684\u5217\u8868\u9879\u76ee\uff1a<\/p>\n<p>\u5bf9\u4e8e\u89e6\u63a7\uff0c\u5728 <code>scrollend<\/code> \u65f6\u8ba1\u7b97\u51fa\u6700\u540e\u4e00\u5c0f\u6bb5\u65f6\u95f4\u7684\u52a0\u901f\u5ea6\uff0c\u7136\u540e\u5e73\u6ed1\u6eda\u52a8\u5230\u5bf9\u5e94\u7684\u4f4d\u7f6e\u5c31\u53ef\u4ee5\u3002<\/p>\n<p>\u5bf9\u4e8e\u9f20\u6807\uff0c\u56e0\u4e3a\u4e0d\u77e5\u9053\u6eda\u52a8\u4ec0\u4e48\u65f6\u5019\u505c\u6b62\uff0c\u4ee5\u53ca\u89e6\u63a7\u677f\u548c\u9f20\u6807\u7684\u6eda\u52a8\u65b9\u5f0f\u4e5f\u5343\u5947\u767e\u602a\uff0c\u60f3\u5230\u4e00\u4e2a\u57fa\u672c\u4e0a\u4e0d\u53ef\u884c\u7684\u65b9\u6cd5\uff1a\u6bcf\u6b21\u89e6\u53d1 <code>wheel<\/code> \u4e8b\u4ef6\u540e\uff0c\u6839\u636e\u6700\u540e\u4e00\u5c0f\u6bb5\u65f6\u95f4\u7684 <code>deltaY<\/code> \u548c\u65f6\u95f4\u7684\u5173\u7cfb\u63d2\u503c\uff0c\u63a8\u6d4b\u51fa\u6eda\u52a8\u7ed3\u675f\u7684\u65f6\u95f4\u548c\u7ec8\u70b9\u4f4d\u7f6e\uff0c\u8bb0 <code>positionOffset<\/code> \u4e3a\u9884\u671f\u7ec8\u70b9\u4f4d\u7f6e\u548c\u6700\u8fd1\u5143\u7d20\u5750\u6807\u95f4\u7684\u8ddd\u79bb\uff0c<code>time<\/code> \u4e3a\u5141\u8bb8\u7684\u7f13\u51b2\u65f6\u95f4\uff0c\u4ee5 <code>1px<\/code> \u4e3a\u6700\u5c0f\u5206\u5ea6\uff0c\u6bcf\u9694 <code>Math.abs(positionOffset) \/ time<\/code> \u8ba9 <code>scrollTop++<\/code> \u6216 <code>--<\/code>\u3002\u6570\u503c\u53d1\u751f\u53d8\u5316\u65f6\uff08\u4ec0\u4e48\u6570\u503c\uff1f\uff09\u53d6\u6d88\u4e4b\u524d\u7684\u8ba1\u65f6\u5e76\u91cd\u65b0\u8ba1\u65f6\u3002\u663e\u7136\u8fd9\u5f88\u9ebb\u70e6\u800c\u4e14\u6839\u672c\u6ca1\u6709\u5934\u7eea\u3002<\/p>\n<p>\u6709\u53ef\u4ee5\u652f\u7684\u62db\u5417\uff1f<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>6<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3201492\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : w3cfed <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             &#8220;`javascript<br \/>const scrollToTop = () =&gt; {<br \/> const c = document.documentElement.scrollTop || document.body.scrollTop;<br \/> if (c &gt; 0) {<br \/> window.requestAnimationFrame(scrollToTop);<br \/> window.scrollTo(0, c &#8211; c \/ 8);<br \/> }<br \/>}<\/p>\n<p>scrollToTop() <\/p>\n<p>&#8220;`<\/p>\n<p>\/\/ \u4e8b\u4f8b<br \/>`window.requestAnimationFrame()` <\/p>\n<p>\u544a\u8bc9\u6d4f\u89c8\u5668\u2014\u2014\u4f60\u5e0c\u671b\u6267\u884c\u4e00\u4e2a\u52a8\u753b\uff0c\u5e76\u4e14\u8981\u6c42\u6d4f\u89c8\u5668\u5728\u4e0b\u6b21\u91cd\u7ed8\u4e4b\u524d\u8c03\u7528\u6307\u5b9a\u7684\u56de\u8c03\u51fd\u6570\u66f4\u65b0\u52a8\u753b\u3002\u8be5\u65b9\u6cd5\u9700\u8981\u4f20\u5165\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u8be5\u56de\u8c03\u51fd\u6570\u4f1a\u5728\u6d4f\u89c8\u5668\u4e0b\u4e00\u6b21\u91cd\u7ed8\u4e4b\u524d\u6267\u884c\u3002<\/p>\n<p>`requestAnimationFrame`<\/p>\n<p>\u4f18\u52bf\uff1a\u7531\u7cfb\u7edf\u51b3\u5b9a\u56de\u8c03\u51fd\u6570\u7684\u6267\u884c\u65f6\u673a\u300260Hz \u7684\u5237\u65b0\u9891\u7387\uff0c\u90a3\u4e48\u6bcf\u6b21\u5237\u65b0\u7684\u95f4\u9694\u4e2d\u4f1a\u6267\u884c\u4e00\u6b21\u56de\u8c03\u51fd\u6570\uff0c\u4e0d\u4f1a\u5f15\u8d77\u4e22\u5e27\uff0c\u4e0d\u4f1a\u5361\u987f\u3002<\/p>\n<p>\u6765\u4e4b\u6574\u7406\u535a\u5ba2\u5185\u5bb9 [How to scroll to the top of the page smoothly? ]( https:\/\/github.com\/martinageradams\/blog\/issues\/11)                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3201493\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : w3cfed <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e0d\u77e5\u9053\u8fd9\u4e2a\u6709\u6ca1\u6709\u7528<\/p>\n<p>window.scrollTo({<br \/> behavior: &#8220;smooth&#8221;<br \/>})                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3201494\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : AlphaRobert <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @w3cfed \u8fd9\u4e2a\u53ea\u662f\u5e73\u6ed1\u6eda\u52a8\uff0c\u4f46\u662f\u610f\u56fe\u5728\u6301\u7eed\u89e6\u53d1 wheel \u4e8b\u4ef6\u65f6\uff0c\u9884\u6d4b\u6eda\u52a8\u7ec8\u70b9\uff0c\u5e76\u5728\u4e00\u8fde\u4e32\u4e8b\u4ef6\u7ed3\u675f\u540e\u80fd\u5438\u9644\u5230\u6700\u8fd1\u7684\u6574\u683c\uff08\u4f8b\u5982 Apple Watch \u7684 TableView \uff09                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3201495\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : w3cfed <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @AlphaRobert \u8fd9\u4e2a\u6211\u5c31\u4e0d\u4f1a\u5566                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3201496\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xiaoming1992 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u201c\u5bf9\u4e86\uff0cScroll Snap \u6709\u4e2a polyfill\uff1acss-scroll-snap-polyfill<\/p>\n<p>\u201c\u53ef\u4ee5\u8ba9 Chrome 63\uff0cFirefox 57\uff0cSafari 11 \u6d4f\u89c8\u5668\u90fd\u6709\u6548\u679c\u3002\u201d<\/p>\n<p>\u8282\u9009\u81ea https:\/\/www.zhangxinxu.com\/wordpress\/2018\/11\/know-css-scroll-snap\/                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3201497\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ccraohng <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u53ef\u4ee5\u89e6\u63a7\u7684 \u6709 start \u548c end \u4e8b\u4ef6\uff0c\u53ef\u4ee5\u5bf9\u52a8\u753b\u7ed3\u675f\u3002<br \/>wheel \u4e8b\u4ef6\uff0c\u7528 setTimeout \u6765\u6a21\u62df\u7ed3\u675f\u4e8b\u4ef6                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u53ea\u7528 JavaScript \u600e\u4e48\u5b9e&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/152775"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=152775"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/152775\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=152775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=152775"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=152775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}