使用Doctype让scrollTop返回0,为什么? [英] Using Doctype let scrollTop return 0, why?
问题描述
当我把这个Doctype放在我的文件 document.body.scrollTop
中返回零。
When I put this Doctype in my documents document.body.scrollTop
returns zero.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
为什么这样?
推荐答案
当您使用该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 providedocument.body.scrollLeft/Top
. In strict mode, IE 6 and a few other browsers, providedocument.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.
这篇关于使用Doctype让scrollTop返回0,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!