{"id":2586,"date":"2013-12-15T14:29:29","date_gmt":"2013-12-15T06:29:29","guid":{"rendered":"https:\/\/sdeno.com\/?p=2586"},"modified":"2013-12-15T14:30:30","modified_gmt":"2013-12-15T06:30:30","slug":"%e5%a6%82%e4%bd%95%e6%ad%a3%e7%a1%ae%e7%9a%84%e8%8e%b7%e5%8f%96scrolltopscrollleft%e7%9a%84%e5%80%bc","status":"publish","type":"post","link":"https:\/\/sdeno.com\/?p=2586","title":{"rendered":"\u5982\u4f55\u6b63\u786e\u7684\u83b7\u53d6scrollTop\/scrollLeft\u7684\u503c"},"content":{"rendered":"<p>\u5355\u7eaf\u7684\u4f7f\u7528jQuery\u7684scrollTop\u76f8\u5173\u7684\u5bf9\u8c61\u6765\u83b7\u53d6\u503c\u662f\u4e0d\u517c\u5bb9\u7684\u3002<\/p>\n<p>scrollTop\u7684\u517c\u5bb9\u6027\u95ee\u9898<\/p>\n<p>Element.scrollTop \u4e0d\u662f W3C \u89c4\u8303\u7684\u6807\u51c6\u5c5e\u6027\uff0c\u6700\u521d\u88ab IE \u7684 DHTML Object Model \u5f15\u5165\uff0c\u4f46\u5df2\u88ab\u76ee\u524d\u5404\u4e3b\u6d41\u6d4f\u89c8\u5668\u6240\u652f\u6301\u3002\u66f4\u591a\u53c2\u89c1 MSDN\uff1ascrollTop Property\u3002<br \/>\nElement.scrollTop \u5c5e\u6027\u83b7\u53d6\u6216\u8005\u8bbe\u7f6e\u4e00\u4e2a\u5143\u7d20\u7684\u5185\u5bb9\u5df2\u7ecf\u6eda\u52a8\u5230\u5176\u4e0a\u8fb9\u754c\u7684\u50cf\u7d20\u6570\u3002\u53ea\u6709\u5728\u5143\u7d20\u5177\u5907\u5782\u76f4\u6eda\u52a8\u6761\u7684\u65f6\u5019\u6b64\u5c5e\u6027\u624d\u6709\u6548\u3002<br \/>\n\u800c\u65e0\u8bba\u662f MSDN \u8fd8\u662f Mozilla Developer Center\uff0c\u5747\u6ca1\u6709\u660e\u786e\u63d0\u53ca\u5bf9\u4e8e\u9875\u9762\uff08\u5373\u89c6\u53e3\u5143\u7d20\uff09\u7684\u6eda\u52a8\u6761\uff0c\u5176\u5782\u76f4\u4e0e\u6c34\u5e73\u7684\u4f4d\u7f6e\u9700\u8981\u901a\u8fc7\u54ea\u4e00\u4e2a DOM \u5bf9\u8c61\u83b7\u53d6\u3002<br \/>\n\u5206\u6790\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<p>[code]<code>&lt;<\/code><code>script<\/code><code>&gt;<\/code><\/p>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>var\u00a0d,\u00a0str;<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>window.onload\u00a0=\u00a0window.onscroll\u00a0=\u00a0function\u00a0()\u00a0{<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>d\u00a0=\u00a0document.getElementById(\"d\");<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>str\u00a0=\u00a0\"&lt;<\/code><code>strong<\/code><code>&gt;\"\u00a0+\u00a0((document.compatMode.toLowerCase().indexOf(\"back\")\u00a0&gt;=\u00a00)\u00a0?\u00a0\"Quirks\"\u00a0:\u00a0\"Standards\")\u00a0+\u00a0\"&lt;\/<\/code><code>strong<\/code><code>&gt;&lt;<\/code><code>br<\/code>\u00a0<code>\/&gt;\"<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>+\u00a0\"document.documentElement.scrollTop:\"\u00a0+\u00a0document.documentElement.scrollTop\u00a0+\u00a0\"&lt;<\/code><code>br<\/code>\u00a0<code>\/&gt;\"<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>+\u00a0\"document.body.scrollTop:\"\u00a0+\u00a0document.body.scrollTop;<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<\/code><code>d.innerHTML\u00a0=\u00a0str;<\/code><\/div>\n<div><code>\u00a0\u00a0\u00a0\u00a0<\/code><code>}<\/code><\/div>\n<div><code>&lt;\/<\/code><code>script<\/code><code>&gt;<\/code><\/div>\n<div><code>&lt;<\/code><code>body<\/code>\u00a0<code>style<\/code><code>=<\/code><code>\"font:12px\u00a0Arial;\u00a0_background-attachment:fixed;\u00a0_background-image:url(about:blank);\"<\/code><code>&gt;<\/code><\/div>\n<div><code>&lt;<\/code><code>div<\/code>\u00a0<code>style<\/code><code>=<\/code><code>\"height:10000px;\"<\/code><code>&gt;&lt;\/<\/code><code>div<\/code><code>&gt;<\/code><\/div>\n<div><code>&lt;<\/code><code>div<\/code>\u00a0<code>id<\/code><code>=<\/code><code>\"d\"<\/code>\u00a0<code>style<\/code><code>=<\/code><code>\"position:fixed;\u00a0top:0;\u00a0left:0;\u00a0_position:absolute;\u00a0_top:expression(offsetParent.scrollTop);\u00a0_left:expression(offsetParent.scrollLeft);\u00a0background:#ddd;\"<\/code><code>&gt;&lt;\/<\/code><code>div<\/code><code>&gt;<\/code><\/div>\n<div><code>&lt;\/<\/code><code>body<\/code><code>&gt;<\/code><\/div>\n<p>[\/code]<\/p>\n<p>\u5f53\u6d4f\u89c8\u5668\u7a97\u53e3\u6eda\u52a8\u7684\u65f6\u5019\uff0c\u4f1a\u5728\u5de6\u4e0a\u89d2\u663e\u793a\u51fa scrollTop\u7684\u503c\u3002<br \/>\n\u5728Opera\uff0cIE\uff0cFirefox\uff0cChrome\u548cSafari\u4e2d\u6d4b\u8bd5\u7684\u6c47\u603b\u8868\uff1a<br \/>\n[img=https:\/\/lh4.googleusercontent.com\/NltRxPVtK45Y2JafkjUzX3QqAxZ-c-EWyvRNGTM_clcGdFnui4hoRjxTIsgkyM-sqEHI_G_X4_a2noFRBJ5fj24Sz3sO4FIXd6qjxbX3TUymujk-][\/img]<br \/>\n\u53ef\u89c1\uff0cscrollTop\u53ef\u4ee5\u4ecedocument.body\u548c document.documentElement\u4e2d\u83b7\u53d6\u3002<br \/>\n\u5728\u6df7\u6742\u6a21\u5f0f\u4e0b\uff0c\u6240\u6709\u6d4f\u89c8\u5668\u5747\u4f7f\u7528 document.body.scrollTop \u83b7\u53d6\u9875\u9762\u7684\u5782\u76f4\u6eda\u52a8\u6761\u7684\u4f4d\u7f6e\u3002\u6240\u4ee5\uff0c\u5728\u6df7\u6742\u6a21\u5f0f\u4e0b\u4e0d\u4f1a\u51fa\u73b0\u517c\u5bb9\u6027\u95ee\u9898\u3002<br \/>\n\u800c\u5728\u6807\u51c6\u6a21\u5f0f\u4e0b\uff0cChrome \u4e0e Safari \u4ecd\u7136\u4f7f\u7528 document.body.scrollTop\uff0c\u540c\u65f6document.documentElement.scrollTop \u7684\u8fd4\u56de\u4e3a 0\u3002\u8fd9\u65f6\u5982\u679c\u4ec5\u4ec5\u4f7f\u7528document.documentElement.scrollTop \u83b7\u53d6\u9875\u9762\u5782\u76f4\u6eda\u52a8\u6761\u9876\u7aef\u4f4d\u7f6e\uff0c\u5728 Chrome \u548c Safari \u4e2d\u5c31\u4f1a\u56e0\u4e3a\u6c38\u4e45\u8fd4\u56de 0 \u5bfc\u81f4\u9875\u9762\u529f\u80fd\u5f02\u5e38\uff08\u5982\uff0c\u7edd\u5bf9\u5b9a\u4f4d\u7684\u5143\u7d20\u4e0d\u80fd\u968f\u9875\u9762\u7684\u6eda\u52a8\u6761\u800c\u6eda\u52a8\u6216\u4f4d\u7f6e\u6709\u8bef\uff09\u3002<\/p>\n<p>\u89e3\u51b3\u65b9\u5f0f<\/p>\n<p>\u6240\u4ee5\uff0c\u5728\u83b7\u53d6\u8fd9\u4e2a\u503c\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u65b9\u6cd5\u907f\u514d\u517c\u5bb9\u6027\u95ee\u9898\uff1a<\/p>\n<p>[code]var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;[\/code]<\/p>\n<p>\u53e6\u5916\uff0cscrollLeft\u7684\u60c5\u51b5\u8ddfscrollTop\u7c7b\u4f3c\uff0c\u6709\u5174\u8da3\u7684\u7ae5\u978b\u53ef\u4ee5\u7814\u7a76\u7814\u7a76\u3002<\/p>\n<p>\u5f15\u7533\u9605\u8bfb\uff1a<\/p>\n<p>\u540e\u6765\u8bf7\u8f83\u4e86\u4e00\u4f4d\u670b\u53cb\uff0c\u628a\u6e90\u7801\u7ed9\u6316\u51fa\u6765\u4e86\u3002<br \/>\n\u6211\u4eec\u901a\u8fc7 WebKit \u5185\u6838\u4e0e Gecko \u5185\u6838\u7684\u6e90\u4ee3\u7801\u4e2d\u4e5f\u53ef\u4ee5\u770b\u51fa Chrome\u3001Safari \u4e0e Firefox \u5bf9\u9875\u9762\u7684 scrollTop\u3001scrollTop \u83b7\u53d6\u65b9\u5f0f\u7684\u4e0d\u540c\uff1a<br \/>\nWebKit \u5185\u6838\uff1a\/WebCore\/html\/HTMLBodyElement.cpp<\/p>\n<p>C\/C++ code<\/p>\n<p>[code]int HTMLBodyElement::scrollTop() const<br \/>\n{<br \/>\n\/\/ Update the document&#8217;s layout.<br \/>\nDocument* doc = document();<br \/>\ndoc-&gt;updateLayoutIgnorePendingStylesheets();<br \/>\nFrameView* view = doc-&gt;view();<br \/>\nreturn view ? adjustForZoom(view-&gt;scrollY(), view) : 0;<br \/>\n}<br \/>\nint HTMLBodyElement::scrollLeft() const<br \/>\n{<br \/>\n\/\/ Update the document&#8217;s layout.<br \/>\nDocument* doc = document();<br \/>\ndoc-&gt;updateLayoutIgnorePendingStylesheets();<br \/>\nFrameView* view = doc-&gt;view();<br \/>\nreturn view ? adjustForZoom(view-&gt;scrollX(), view) : 0;<br \/>\n}[\/code]<\/p>\n<p>Gecko \u5185\u6838\uff1a\/content\/base\/src\/nsGenericElement.cpp<br \/>\nC\/C++ code<\/p>\n<p>[code]nsNSElementTearoff::GetScrollInfo(nsIScrollableView **aScrollableView, nsIFrame **aFrame)<br \/>\n{<br \/>\n&#8230;<br \/>\nif ((quirksMode &amp;&amp; mContent-&gt;NodeInfo()-&gt;Equals(nsGkAtoms::body)) ||<br \/>\n(!quirksMode &amp;&amp; mContent-&gt;NodeInfo()-&gt;Equals(nsGkAtoms::html))) {<br \/>\n\/\/ In quirks mode, the scroll info for the body element should map to the<br \/>\n\/\/ scroll info for the nearest scrollable frame above the body element<br \/>\n\/\/ (i.e. the root scrollable frame). This is what IE6 does in quirks<br \/>\n\/\/ mode. In strict mode the root scrollable frame corresponds to the<br \/>\n\/\/ html element in IE6, so we map the scroll info for the html element to<br \/>\n\/\/ the root scrollable frame.<br \/>\ndo {<br \/>\nframe = frame-&gt;GetParent();<br \/>\nif (!frame) {<br \/>\nbreak;<br \/>\n}<br \/>\nscrollFrame = do_QueryFrame(frame);<br \/>\n}<br \/>\n&#8230;<br \/>\n}[\/code]<br \/>\nFirefox \u7684 Gecko \u5185\u6838\u6e90\u4ee3\u7801\u4e2d\u901a\u8fc7\u5bf9\u6587\u6863\u6a21\u5f0f\u7684\u5224\u65ad\u51b3\u5b9a\u901a\u8fc7\u54ea\u4e2a\u5bf9\u8c61\u83b7\u53d6\u548c\u8bbe\u7f6e scrollTop\u3001scrollLeft \u5c5e\u6027\u3002\u4ece\u6ce8\u91ca\u4e2d\u53ef\u4ee5\u770b\u5230\uff0cFirefox \u8fd9\u4e48\u505a\u662f\u4e3a\u4e86\u517c\u5bb9 IE6 \u7684\u6807\u51c6\u6a21\u5f0f\u4e0e\u6df7\u6742\u6a21\u5f0f\u5bf9 scrollTop\u3001scrollLeft \u7684\u5904\u7406\u3002<\/p>\n<p>\u800c WebKit \u5185\u6838\u5219\u6ca1\u6709\u9488\u5bf9\u6587\u6863\u6a21\u5f0f\u8fdb\u884c\u5224\u65ad\uff0c\u5bf9\u9875\u9762\u6eda\u52a8\u6761\u4fe1\u606f\u901a\u8fc7 [Object HTMLBodyElement] \u5bf9\u8c61\u83b7\u53d6\u4e0e\u8bbe\u7f6e\u3002<br \/>\n<a href=\"http:\/\/bbs.csdn.net\/topics\/340198399\" target=\"_blank\">http:\/\/bbs.csdn.net\/topics\/340198399<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5355\u7eaf\u7684\u4f7f\u7528jQuery\u7684scrollTop\u76f8\u5173\u7684\u5bf9\u8c61\u6765\u83b7\u53d6\u503c\u662f\u4e0d\u517c\u5bb9\u7684\u3002 scrollTop\u7684\u517c\u5bb9\u6027\u95ee\u9898 El [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,13],"tags":[],"class_list":["post-2586","post","type-post","status-publish","format-standard","hentry","category-11","category-jquery"],"_links":{"self":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/2586","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2586"}],"version-history":[{"count":0,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/2586\/revisions"}],"wp:attachment":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2586"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2586"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2586"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}