{"id":7351,"date":"2018-02-02T01:08:56","date_gmt":"2018-02-01T17:08:56","guid":{"rendered":"http:\/\/www.zhangxinxu.com\/wordpress\/?p=7351"},"modified":"2018-02-02T01:15:11","modified_gmt":"2018-02-01T17:15:11","slug":"container-scroll-position-hold","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2018\/02\/container-scroll-position-hold\/","title":{"rendered":"\u5c0ftips: \u6eda\u52a8\u5bb9\u5668\u5c3a\u5bf8\u53d8\u5316\u5b50\u5143\u7d20\u89c6\u89c9\u4e0a\u4f4d\u7f6e\u4e0d\u53d8JS\u5b9e\u73b0"},"content":{"rendered":"<p>by <a href=\"http:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"http:\/\/www.zhangxinxu.com\/wordpress\/?p=7351\">http:\/\/www.zhangxinxu.com\/wordpress\/?p=7351<\/a><br \/>\n\u672c\u6587\u53ef\u5168\u6587\u8f6c\u8f7d\uff0c\u4f46\u9700\u8981\u4fdd\u7559\u539f\u4f5c\u8005\u548c\u51fa\u5904\u3002<\/p>\n<h3>\u4e00\u3001\u5148\u4ece\u9700\u6c42\u8bf4\u8d77<\/h3>\n<p>\u5bf9\u4e8e\u4e00\u4e2a\u5bbd\u5ea6\u4e0d\u56fa\u5b9a\u7684\u6eda\u52a8\u5bb9\u5668\uff0c\u5982\u679c\u91cc\u9762\u5185\u5bb9\u5df2\u7ecf\u6eda\u52a8\u5230\u4e86\u4e00\u5b9a\u7684\u9ad8\u5ea6\uff0c\u8fd9\u4e2a\u65f6\u5019\u6eda\u52a8\u5bb9\u5668\u7684\u5bbd\u5ea6\u53d1\u751f\u53d8\u5316\uff0c\u5219\u91cc\u9762\u5185\u5bb9\u7684\u4f4d\u7f6e\u4f1a\u8fdb\u884c\u91cd\u5b9a\u4f4d\uff0c\u4e00\u4e0d\u7559\u795e\u5c31\u4e0d\u77e5\u9053\u521a\u624d\u7684\u4f4d\u7f6e\u662f\u54ea\u91cc\u4e86\u3002<\/p>\n<p>\u5c24\u5176\u662f\u770b\u5c0f\u8bf4\u8fd9\u79cd\u975e\u5e38\u8003\u9a8c\u773c\u529b\u7684\u573a\u666f\u3002<\/p>\n<p>\u4f8b\u5982\u4e0b\u9762\u7684GIF\u622a\u56fe\u6f14\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201802\/scroll-position-woo-s.gif\" width=\"442\" height=\"421\" alt=\"\u4f4d\u7f6e\u53d1\u751f\u660e\u663e\u53d8\u5316\" class=\"alignnone\" \/><\/p>\n<p>\u4e8e\u662f\uff0c\u5c31\u6709\u9700\u6c42\u3002<\/p>\n<p>\u5f53\u6eda\u52a8\u5bb9\u5668\u5c3a\u5bf8\u53d1\u751f\u53d8\u5316\u7684\u65f6\u5019\uff08\u5982\u5bbd\u5c4f\u7a84\u5c4f\u5207\u6362\uff0c\u6216\u8005\u9ed8\u8ba4\u5c3a\u5bf8\u53d8\u5168\u5c4f\u65f6\u5019\uff09\uff0c\u6700\u4e0a\u9762\u5143\u7d20\u4f4d\u7f6e\u8981\u4fdd\u6301\u4e0d\u53d8\uff0c\u8fd9\u6837\u89c6\u89c9\u4f53\u9a8c\u5c31\u5f88\u597d\u3002\u4e0d\u4f1a\u56e0\u4e3a\u7a81\u7136\u7684\u5c3a\u5bf8\u53d8\u5316\u800c\u4e0d\u77e5\u9053\u521a\u624d\u770b\u5230\u54ea\u91cc\u4e86\u3002<\/p>\n<p>\u90a3\u4e48\u8be5\u5982\u4f55\u5b9e\u73b0\u5462\uff1f<\/p>\n<p>\u9700\u8981\u501f\u52a9JavaScript\u624b\u52a8\u4fee\u6b63\u6eda\u52a8\u4f4d\u7f6e\u3002<\/p>\n<h3>\u4e8c\u3001\u6eda\u52a8\u5bb9\u5668\u5c3a\u5bf8\u53d8\u5316\u5b50\u5143\u7d20\u89c6\u89c9\u4e0a\u4f4d\u7f6e\u4e0d\u53d8\u7684JS\u5904\u7406<\/h3>\n<p>\u6211\u4eec\u5148\u770b\u5b9e\u73b0\u540e\u7684\u622a\u5c4fgif\u6548\u679c\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/wx3.sinaimg.cn\/mw690\/4b4d632fgy1fo1ez80wr5g20cx0d041w.gif\" width=\"465\" height=\"468\" alt=\"\u6eda\u52a8\u4f4d\u7f6e\u7279\u522b\u5904\u7406\u540e\u7684gif\u622a\u5c4f\u6548\u679c\" class=\"alignnone\" onerror=\"this.src='http:\/\/image.zhangxinxu.com\/image\/blog\/201802\/scroll-position-fixed-s.gif';\" \/><\/p>\n<p>\u60a8\u53ef\u4ee5\u72e0\u72e0\u5730\u70b9\u51fb\u8fd9\u91cc\uff1a<a href=\"http:\/\/www.zhangxinxu.com\/study\/201801\/store-scroll-position.html\" rel=\"noopener\" target=\"_blank\">\u6eda\u52a8\u5bb9\u5668\u5c3a\u5bf8\u53d8\u5316\u65f6\u5019\u6700\u4e0a\u65b9\u5143\u7d20\u4f4d\u7f6e\u4e0d\u53d8demo<\/a><\/p>\n<p>\u6eda\u52a8\u5bb9\u5668\u5230\u5408\u9002\u4f4d\u7f6e\uff0c\u7136\u540e\u6539\u53d8\u6d4f\u89c8\u5668\u7a97\u53e3\u5c3a\u5bf8\uff0c\u53ef\u4ee5\u4f53\u9a8c\u5230\u5fae\u4e1d\u4e0d\u52a8\u7684\u975e\u5e38\u53cb\u597d\u7684\u4ea4\u4e92\u4f53\u9a8c\u6548\u679c\u3002<\/p>\n<h4>JS\u5b9e\u73b0\u7684\u539f\u7406<\/h4>\n<ol>\n<li>\u83b7\u5f97\u6700\u9760\u8fd1\u6eda\u52a8\u5bb9\u5668\u4e0a\u8fb9\u7f18\u7684\u5143\u7d20\uff1b<\/li>\n<li>\u83b7\u5f97\u6700\u9760\u8fd1\u6eda\u52a8\u5bb9\u5668\u4e0a\u8fb9\u7f18\u7684\u5143\u7d20\u8ddd\u79bb\u4e0a\u8fb9\u7f18\u7684\u8ddd\u79bb\uff1b<\/li>\n<li>\u5f53\u6eda\u52a8\u5bb9\u5668\u5c3a\u5bf8\u6539\u53d8\u540e\uff0c\u83b7\u5f97\u4e4b\u524d\u6700\u9760\u8fd1\u4e0a\u8fb9\u7f18\u5143\u7d20\u73b0\u5728\u8ddd\u79bb\u4e0a\u8fb9\u7f18\u7684\u8ddd\u79bb\uff0c\u6839\u636e\u524d\u540e\u7684\u5dee\u503c\u4fee\u6b63\u6b64\u65f6\u7684scrollTop\u5927\u5c0f\uff1b<\/li>\n<\/ol>\n<p>\u539f\u7406\u7b2c\u4e00\u6b65\u662f\u6700\u96be\u70b9\uff0c\u5982\u4f55\u83b7\u5f97\u6700\u9760\u8fd1\u6eda\u52a8\u5bb9\u5668\u4e0a\u8fb9\u7f18\u7684\u5143\u7d20\u5462\uff1f<\/p>\n<p>\u6211\u8fd9\u91cc\u4f7f\u7528\u7684\u662f<code>document.elementsFromPoint<\/code>\u65b9\u6cd5\uff0c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>var elements = document.elementsFromPoint(x, y);<\/pre>\n<p>\u8868\u793a\u8fd4\u56de\u6240\u6709\u8ddd\u79bb\u6d4f\u89c8\u5668\u53ef\u89c6\u7a97\u53e3<code>x, y<\/code>\u5750\u6807\u7684DOM\u5143\u7d20\u96c6\u5408\u3002\u56e0\u6b64\uff0c<code>elements<\/code>\u662f\u4e00\u4e2a\u4ece\u6700\u5b50\u5143\u7d20\u5f00\u59cb\uff0c\u4f9d\u6b21\u5411\u4e0a\uff0c\u4e00\u76f4\u5230<code>&lt;body&gt;<\/code>\uff0c<code>&lt;html&gt;<\/code>\u5143\u7d20\u7684\u7c7b\u6570\u7ec4\u96c6\u5408\u3002<\/p>\n<p>\u5728\u672c\u4f8b\u4e2d\uff0c<code>elements[0]<\/code>\u5c31\u662f\u6211\u4eec\u9700\u8981\u7684\u5143\u7d20\u3002<\/p>\n<p>\u5047\u8bbe\u6eda\u52a8\u5bb9\u5668\u5143\u7d20\u7684<code>id<\/code>\u662f<code>box<\/code>\uff0c\u5219\u4f7f\u7528JavaScript\u4ee3\u7801\u8868\u793a\u5c31\u662f\uff1a<\/p>\n<pre>var box = document.getElementById('box');\n\n<span style=\"color:green;\">\/\/ x\u5c31\u53d6\u6eda\u52a8\u5bb9\u5668\u6c34\u5e73\u4e2d\u5fc3\u70b9<\/span>\nvar x = box.getBoundingClientRect().left + box.clientWidth \/ 2;\n<span style=\"color:green;\">\/\/ + 12\u4e3a\u4e86\u8ba9\u66f4\u9760\u4e0b\u4e00\u70b9\u7684\u5143\u7d20\u4f5c\u4e3a\u8fb9\u7f18\u5143\u7d20<\/span>\nvar y = box.getBoundingClientRect().top + 12;\n<span style=\"color:green;\">\/\/ target\u5c31\u662f\u6211\u4eec\u83b7\u53d6\u7684\u6700\u63a5\u8fd1\u6eda\u52a8\u5bb9\u5668\u4e0a\u8fb9\u7f18\u7684\u5143\u7d20<\/span>\nvar target = document.elementsFromPoint(x, y)[0];<\/pre>\n<p>\u7136\u540e<code>target<\/code>\u8ddd\u79bb\u4e0a\u8fb9\u7f18\u7684\u8ddd\u79bb\u4e5f\u5f88\u597d\u5b9e\u73b0\uff08\u5bb9\u5668<code>scroll<\/code>\u4e8b\u4ef6\u65f6\u5019\u6267\u884c\uff09\uff1a<\/p>\n<pre>var offsetTop = target.getBoundingClientRect().top - box.getBoundingClientRect().top<\/pre>\n<p>\u6700\u540e\uff0c\u6839\u636e\u6eda\u52a8\u540e\u7684\u8fb9\u7f18\u8ddd\u79bb\u8fdb\u884c\u6eda\u52a8\u4fee\u6b63\uff08\u7a97\u4f53<code>resize<\/code>\u4e8b\u4ef6\u65f6\u5019\u6267\u884c\uff09\uff1a<\/p>\n<pre>var currentOffsetTop = box.getBoundingClientRect().top - target.getBoundingClientRect().top;\n<span style=\"color:green;\">\/\/ \u6eda\u52a8\u4fee\u6b63\uff0c\u6548\u679c\u8fbe\u6210<\/span>\nbox.scrollTop = box.scrollTop - currentOffsetTop - offsetTop;<\/pre>\n<p>\u66f4\u5b8c\u6574JS\u4ee3\u7801\u53c2\u89c1<a href=\"http:\/\/www.zhangxinxu.com\/study\/201801\/store-scroll-position.html\" rel=\"noopener\" target=\"_blank\">demo\u9875\u9762<\/a>\uff08\u5c31\u51e0\u5341\u884c\uff09\uff0c\u5927\u5bb6\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u5b9e\u9645\u9879\u76ee\u573a\u666f\u8fdb\u884c\u4fee\u6539\uff0c\u8fd9\u91cc\u4e0d\u91cd\u590d\u5c55\u793a\u3002<\/p>\n<h4>\u5173\u4e8edocument.elementsFromPoint API\u7684\u517c\u5bb9\u6027<\/h4>\n<p>\u6839\u636e<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/DocumentOrShadowRoot\/elementsFromPoint\" rel=\"nofollow\" target=\"_blank\">MDN<\/a>\u4e0a\u7684\u6570\u636e\uff0c<code>document.elementsFromPoint<\/code> IE10+\u624d\u652f\u6301\uff0c\u800c\u4e14\u9700\u8981<code>ms<\/code>\u79c1\u6709\u524d\u7f00\uff0c\u56e0\u6b64\uff0c\u5982\u679c\u60f3\u8981\u517c\u5bb9IE\u6d4f\u89c8\u5668\uff0c\u53ef\u4ee5\u52a0\u4e00\u53e5\uff1a<\/p>\n<pre>if (!document.elementsFromPoint) {\n    document.elementsFromPoint = document.msElementsFromPoint;\n}<\/pre>\n<p>\u7531\u4e8e\u672c\u6587\u5b9e\u4f8b\u5c5e\u4e8e\u4f53\u9a8c\u589e\u5f3a\u7684\u529f\u80fd\uff0c\u56e0\u6b64\uff0c\u5c31\u7b97\u6d4f\u89c8\u5668\u4e0d\u652f\u6301\u4e5f\u65e0\u4f24\u5927\u96c5\uff0c\u80fd\u591f\u8ba990+%\u6d4f\u89c8\u5668\u6709\u66f4\u597d\u4f53\u9a8c\u5df2\u7ecf\u5f88\u68d2\u4e86\uff0c\u56e0\u6b64\u662f\u53ef\u4ee5\u653e\u5fc3\u5927\u80c6\u4f7f\u7528\u7684\u4e00\u4e2a\u6280\u672f\u6848\u4f8b\u3002<\/p>\n<h4>IE\u6d4f\u89c8\u5668\u4e0b\u66f4\u591a\u7ec6\u8282<\/h4>\n<p>\u5728IE\u6d4f\u89c8\u5668\u4e0b\u6d4b\u8bd5\uff0c<code>resize<\/code>\u65f6\u5019\u6eda\u52a8\u5185\u5bb9\u6709\u6643\u52a8\uff0c\u6211\u731c\u6d4b\u53ef\u80fd\u4e0e\u8fb9\u7f18\u4f4d\u7f6e\u8ba1\u7b97\u5e26\u5c0f\u6570\u6709\u5173\uff0c\u56e0\u6b64\uff0c<code>offsetTop<\/code>\u7684\u8ba1\u7b97\u90fd\u662f\u53d6\u6574\u7684\uff0c\u5982\u4e0b\u793a\u610f\uff1a<\/p>\n<pre>var currentOffsetTop = Math.round(box.getBoundingClientRect().top) - Math.round(target.getBoundingClientRect().top);<\/pre>\n<p>\u4f53\u9a8c\u679c\u7136\u597d\u4e86\u5f88\u591a\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u5728Chrome\u7b49\u6d4f\u89c8\u5668\u4e0b\uff0c<code>target<\/code>\u7684\u83b7\u53d6\u548c\u504f\u79fb\u8ba1\u7b97\u53ef\u4ee5\u76f4\u63a5\u5728<code>window<\/code> <code>resize<\/code>\u65f6\u5019\u6267\u884c\u3002\u4f46\u662fIE\u6d4f\u89c8\u5668\u4e0d\u884c\uff0c\u4f3c\u4e4eIE\u6d4f\u89c8\u5668\u5148\u89e6\u53d1\u6d4f\u89c8\u5668\u884c\u4e3a\uff0c\u518d\u6267\u884c\u4e86<code>resize<\/code>\u4e8b\u4ef6\uff0c\u6240\u4ee5\uff0c\u7efc\u5408\u4e0b\u6765\uff0c\u8fd8\u662f\u5efa\u8bae\u6700\u4e0a\u8fb9\u7f18\u5143\u7d20\u83b7\u53d6\u653e\u5728<code>scroll<\/code>\u4e8b\u4ef6\u4e2d\u5904\u7406\u3002<\/p>\n<h3>\u4e09\u3001\u7ed3\u675f\u8bed<\/h3>\n<p>\u4e00\u4e2aJS\u4f53\u9a8c\u4f18\u5316\u5b9e\u4f8b\uff0c\u6ca1\u4ec0\u4e48\u5176\u4ed6\u597d\u5c55\u5f00\u7684\u3002<\/p>\n<p>\u611f\u8c22\u9605\u8bfb\uff01<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/image.zhangxinxu.com\/image\/blog\/201611\/14.png\" 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=\"http:\/\/www.zhangxinxu.com\/wordpress\/?p=7351\">http:\/\/www.zhangxinxu.com\/wordpress\/?p=7351<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5bf9\u4e8e\u4e00\u4e2a\u5bbd\u5ea6\u4e0d\u56fa\u5b9a\u7684\u6eda\u52a8\u5bb9\u5668\uff0c\u5982\u679c\u91cc\u9762\u5185\u5bb9\u5df2\u7ecf\u6eda\u52a8\u5230\u4e86\u4e00\u5b9a\u7684\u9ad8\u5ea6\uff0c\u8fd9\u4e2a\u65f6\u5019\u6eda\u52a8\u5bb9\u5668\u7684\u5bbd\u5ea6\u53d1\u751f\u53d8\u5316\uff0c\u5219\u91cc\u9762\u5185\u5bb9\u7684\u4f4d\u7f6e\u4f1a\u8fdb\u884c\u91cd\u5b9a\u4f4d\uff0c\u4e00\u4e0d\u7559\u795e\u5c31\u4e0d\u77e5\u9053\u521a\u624d\u7684\u4f4d\u7f6e\u662f\u54ea\u91cc\u4e86\u3002<\/p>\n<p>\u5c24\u5176\u662f\u770b\u5c0f\u8bf4\u8fd9\u79cd\u975e\u5e38\u8003\u9a8c\u773c\u529b\u7684\u573a\u666f\u3002\u4e8e\u662f\uff0c\u5c31\u6709\u9700\u6c42\uff1a\u5f53\u6eda\u52a8\u5bb9\u5668\u5c3a\u5bf8\u53d1\u751f\u53d8\u5316\u7684\u65f6\u5019\uff08\u5982\u5bbd\u5c4f\u7a84\u5c4f\u5207\u6362\uff0c\u6216\u8005\u9ed8\u8ba4\u5c3a\u5bf8\u53d8\u5168\u5c4f\u65f6\u5019\uff09\uff0c\u6700\u4e0a\u9762\u5143\u7d20\u4f4d\u7f6e\u8981\u4fdd\u6301\u4e0d\u53d8\uff0c\u8fd9\u6837\u89c6\u89c9\u4f53\u9a8c\u5c31\u5f88\u597d\u3002\u4e0d\u4f1a\u56e0\u4e3a\u7a81\u7136\u7684\u5c3a\u5bf8\u53d8\u5316\u800c\u4e0d\u77e5\u9053\u521a\u624d\u770b\u5230\u54ea\u91cc\u4e86\u3002<\/p>\n<p>\u90a3\u4e48\u8be5\u5982\u4f55\u5b9e\u73b0\u5462\uff1f<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[180],"tags":[1211,623,774,633,49,252,124],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/7351"}],"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=7351"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/7351\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=7351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=7351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=7351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}