Skip to content

Instantly share code, notes, and snippets.

@ziluo
Last active December 17, 2015 06:38
Show Gist options
  • Save ziluo/5566527 to your computer and use it in GitHub Desktop.
Save ziluo/5566527 to your computer and use it in GitHub Desktop.
文档坐标和视口坐标
//以一个对象的x和y属性的方式返回滚动条的偏移量
function(w){
var w = w || window;
if(w.pageXOffset != null) return {x: x.pageXOffset, y: w.pageYOffset};
var d = w.document;
if(document.compatMode == "CSS1Compat"){
return {x:d.documentElement.scrollLeft,y:d.documentElement.scrollTop};
}else{
return {x:d.body.scrllLeft,y:d.body.scrollTop};
}
}
//作为一个对象的w和h属性的方式返回视口的尺寸
function(w){
var w = w || window;
if(w.innerWidth != null) return {x: x.innerWidth, y: w.innerHeight};
var d = w.document;
if(document.compatMode == "CSS1Compat"){
return {x:d.documentElement.clientWidth,y:d.documentElement.clientHeight};
}else{
return {x:d.body.clientWidth,y:d.body.clientHeight};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment