获取窗口文档尺寸

获取窗口文档尺寸

js获取
取窗口滚动条滚动高度

function getScrollTop()
{
  var scrollTop=0;
  if(document.documentElement&&document.documentElement.scrollTop)  {
    scrollTop=document.documentElement.scrollTop;
  }  else if(document.body)  {
    scrollTop=document.body.scrollTop;
  }
  return scrollTop;
}

取窗口可视范围的高度

function getClientHeight()
{
  var clientHeight=0;
  if(document.body.clientHeight&&document.documentElement.clientHeight)  {
  var clientHeight = (document.body.clientHeight<document.documentElement.clientHeight)?document.body.clientHeight:document.documentElement.clientHeight;   
  }  else  {
    var clientHeight = (document.body.clientHeight>document.documentElement.clientHeight)?document.body.clientHeight:document.documentElement.clientHeight;   
  }
  return clientHeight;
}

取文档内容实际高度

function getScrollHeight(){
 return Math.max(document.body.scrollHeight,document.documentElement.scrollHeight);
}
//在IE中:
document.body.clientWidth ==> BODY对象宽度
document.body.clientHeight ==> BODY对象高度
document.documentElement.clientWidth ==> 可见区域宽度
document.documentElement.clientHeight ==> 可见区域高度
//在FireFox中:
document.body.clientWidth ==> BODY对象宽度
document.body.clientHeight ==> BODY对象高度
document.documentElement.clientWidth ==> 可见区域宽度
document.documentElement.clientHeight ==> 可见区域高度
//在Opera中:  
document.body.clientWidth ==> 可见区域宽度
document.body.clientHeight ==> 可见区域高度
document.documentElement.clientWidth ==> 页面对象宽度(即BODY对象宽度加上Margin宽)
document.documentElement.clientHeight ==> 页面对象高度(即BODY对象高度加上Margin高)

3.jquery获取屏幕的尺寸

//获取浏览器显示区域(可视区域)的高度 :   
$(window).height();   
//获取浏览器显示区域(可视区域)的宽度 :
$(window).width();   
//获取页面的文档高度   
$(document).height();   
//获取页面的文档宽度 :
$(document).width(); 
//浏览器当前窗口文档body的高度:  
$(document.body).height();
//浏览器当前窗口文档body的宽度: 
$(document.body).width();
//获取滚动条到顶部的垂直高度 (即网页被卷上去的高度)  
$(document).scrollTop();   
//获取滚动条到左边的垂直宽度 :
$(document).scrollLeft(); 
//获取或设置元素的宽度:
$(obj).width();
//获取或设置元素的高度:
$(obj).height();
//某个元素的上边界到body最顶部的距离:obj.offset().top;(在元素的包含元素不含滚动条的情况下)
//某个元素的左边界到body最左边的距离:obj.offset().left;(在元素的包含元素不含滚动条的情况下)
//返回当前元素的上边界到它的包含元素的上边界的偏移量:obj.offset().top(在元素的包含元素含滚动条的情况下)
//返回当前元素的左边界到它的包含元素的左边界的偏移量:obj.offset().left(在元素的包含元素含滚动条的

4.jquery获取位置

//获取页面某一元素的绝对X,Y坐标,可以用offset()方法:
var top = $('#ID').offset().top;
var left = $('#ID').offset().left;
//获取相对(父元素)位置:
var top = $('#ID').position().top;
var left = $('#ID').position().left;
//position()方法相对父元素是指拥有position为relative或absolute属性的父元素。
//让Div随滚动条移动:
<div id="oLayer" style="position: absolute; left: 0; top:80px; z-index: 2; background: a9c9ef; margin-left:6px;width: 140px; height: 70px">
</div>
//jquery代码:
$(window).scroll(function() {
    var init_pos = $('#oLayer').offset().top;
    $('#oLayer').css("top", $(window).scrollTop()+80)
});

5.常见的宽度和高度获取
网页可见区域宽: document.body.clientWidth
网页可见区域高: document.body.clientHeight
网页可见区域宽: document.body.offsetWidth (包括边线的宽)
网页可见区域高: document.body.offsetHeight (包括边线的高)
网页正文全文宽: document.body.scrollWidth
网页正文全文高: document.body.scrollHeight
网页被卷去的高: document.body.scrollTop
网页被卷去的左: document.body.scrollLeft
网页正文部分上: window.screenTop
网页正文部分左: window.screenLeft
屏幕分辨率的高: window.screen.height
屏幕分辨率的宽: window.screen.width
屏幕可用工作区高度: window.screen.availHeight
屏幕可用工作区宽度: window.screen.availWidth

<script language="javascript">
function screenInfo(){
    var  s = "";
   s += "\r\n网页可见区域宽:"+ document.body.clientWidth;
   s += "\r\n网页可见区域高:"+ document.body.clientHeight;
   s += "\r\n网页可见区域宽:"+ document.body.offsetWidth  +" (包括边线的宽)";
   s += "\r\n网页可见区域高:"+ document.body.offsetHeight +" (包括边线的宽)";
   s += "\r\n网页正文全文宽:"+ document.body.scrollWidth;
   s += "\r\n网页正文全文高:"+ document.body.scrollHeight;
   s += "\r\n网页被卷去的高:"+ document.body.scrollTop;
   s += "\r\n网页被卷去的左:"+ document.body.scrollLeft;
   s += "\r\n网页正文部分上:"+ window.screenTop;
   s += "\r\n网页正文部分左:"+ window.screenLeft;
   s += "\r\n屏幕分辨率的高:"+ window.screen.height;
   s += "\r\n屏幕分辨率的宽:"+ window.screen.width;
   s += "\r\n屏幕可用工作区高度:"+ window.screen.availHeight;
   s += "\r\n屏幕可用工作区宽度:"+ window.screen.availWidth;
   alert(s);
}
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值