且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Doctype让scrollTop返回0,为什么?

更新时间:2023-02-19 23:37:08

当您使用该Doctype时,您将把所有当前浏览器都放在所谓的几乎标准模式,没有它,你将处于怪癖模式。

When you use that Doctype, you'll put every current browser in the so-called Almost Standards mode, without it you'll be in Quirks Mode.

正如你可以在此页面


[m] ost浏览器提供 window.pageXOffset / pageYOffset 。这些是完全可靠的。再次,Internet Explorer是奇怪的,因为它不提供这些属性。 Internet Explorer和其他浏览器将提供 document.body.scrollLeft / Top 。在严格模式下,IE 6和其他一些浏览器提供 document.documentElement.scrollLeft / Top

[m]ost browsers provide window.pageXOffset/pageYOffset. These are completely reliable. Once again, Internet Explorer is the odd one out, as it does not provide these properties. Internet Explorer and some other browsers will provide document.body.scrollLeft/Top. In strict mode, IE 6 and a few other browsers, provide document.documentElement.scrollLeft/Top.

提供的一个脚本计算您想要的值:

A script provided there calculates the value you want:

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

另一个有趣的文章出现在QuirksMode上,两个视口的故事

Another interesting article appeared on QuirksMode, A tale of two viewports.