确定窗口相对于屏幕左边和上边的位置。
window.screenLeft 和 window.screenTop;//IE,Safari,Opera,Chrome
window.screenX 和 window.screenY;//Firefox,Safari,Chrome
注:Opera虽然也支持window.screenX和window.screenY,但是与他们并不对应。建议大家不要再Oprea中使用。
通常兼容性写法。
var scrernLeft = (typeof window.screenLeft == "number") ? window.screenLeft : window.screenX;
var screenTop = (typeof window.screenTop == "number") ? window.screenTop : window.screenY;
确定窗口大小:
IE9+ Safari Opera Chrome Firefox 为我们提供了四种属性 innerWidth、innerHeight、outerWidth、outerHeight。
innerWidth innerHeight视图区的宽高,outerWidth、outerHeight整个浏览器的宽高包括工具栏顶部。
IE8及更早的版本中没有提供可以取得当前浏览器窗口尺寸的属性,但通过DOM提供了页面可视区的相关信息。IE,Safari,Opera,Chrome,Firefoxdocument.documentElement.clientHeight、document.documentElement.clientWidth保存着浏览是视图区大小(即可视区)
IE6中这些属性必须在标准模式才能取得相同信息,如果是混杂模式,必须通过document.body.clientWidth、document.body.clientWidth来获取。而对于混杂模式下的Chrome ,两种方法都可以。
所以在考虑兼容性问题上,根本没有办法确定浏览器窗口本身的大小,但可以获得页面可视区大小:兼容性写法:
var pageWidth = window.innerWidth;var pageHeight = window.innerHeight;
if( typeof pageWidth !="number") {
if(document.compatMode == "CSSlCompat") {
pageWidth = document.documentElement.clientWidth;
pageHeight = document.documentElement.clientHeight;
}
else {
pageWidth = document.body.clientWidth;
pageHeight = document.body.clientHeight;
}
}