首页技术文章正文

WEB前端培训之javaScript 计算网页内容的宽与高 (浏览器的标准模式与怪异模式)二

更新时间:2017-04-14 来源:黑马程序员Web前端培训学院 浏览量:


 
如何判定现在是标准模式还是怪异模式:
 
方法一:执行以下代码
alert(window.top.document.compatMode) ;
//BackCompat  表示怪异模式
//CSS1Compat  表示标准模式
方法二:jquery为我们提供的方法,如下:
alert($.boxModel)
alert($.support.boxModel)
 
 
 
IE6,7,8浏览器的标准模式还是怪异模式 盒子模型的 差异
(由于火狐的始终表现的很一致,不种我们操心。所以这里我们重点说IE浏览器)






 
 
 
 
 
-------------------------------------------------模态窗口----------------------------------------------------
 
我们想做一个DIV蒙层,中间放一个iframe,做一个像模态窗口的dialog工具
 
 思路如下:
取出页面 网页可见区域 的宽与高, 进行蒙层,
通过CSS的固定定位属性{position:fixed}来实现,可以让HTML元素脱离文档流固定在浏览器的某个位置,
这样拖动滚动条时, 蒙层不会移动,一直在中心位置。
中心位置放一个iframe,用来显示其它网页,并可以提交表单。
 
可以用以下代码计算 蒙层的宽与高:
Js代码  
  1. //计算窗口的高宽和滚动条的位置  
  2. alert(window.top.document.compatMode) ;//区分怪异模式或标准模式  
  3. var cw = window.top.document.compatMode == "BackCompat" ?window.top.document.body.clientWidth:window.top.document.documentElement.clientWidth;//窗体高度  
  4. var ch = window.top.document.compatMode == "BackCompat" ?window.top.document.body.clientHeight:window.top.document.documentElement.clientHeight;//窗体宽度//必须考虑文本框处于页面边缘处,控件显示不全的问题  
  5. var sw = Math.max(window.top.document.documentElement.scrollLeft, window.top.document.body.scrollLeft);//横向滚动条位置  
  6. var sh = Math.max(window.top.document.documentElement.scrollTop, window.top.document.body.scrollTop);//纵向滚动条位置//考虑滚动的情况           
  7. alert("cw:"+cw+"  ch:"+ch+"  sw:"+sw+"  sh"+sh);  
 
 
 
 
----------------------------------------------参考 1-----------------------------------------------------
 
我们先来认识一下有哪些属性可以使用:
 
scrollLeft:设置或获取位于对象左边界和窗口中目前可见内容的最左端之间的距离
scrollTop:设置或获取位于对象最顶端和窗口中可见内容的最顶端之间的距离
scrollWidth:获取对象的滚动宽度
scrollHeight: 获取对象的滚动高度。
 
obj.offsetTop 指 obj 相对于版面或由 offsetParent 属性指定的父坐标的计算上侧位置,整型,单位像素。
obj.offsetLeft 指 obj 相对于版面或由 offsetParent 属性指定的父坐标的计算左侧位置,整型,单位像素。
obj.offsetWidth 指 obj 控件自身的绝对宽度,不包括因 overflow 而未显示的部分,也就是其实际占据的宽度,整型,单位像素。
obj.offsetHeight 指 obj 控件自身的绝对高度,不包括因 overflow 而未显示的部分,也就是其实际占据的高度,整型,单位像素
 
event.clientX 相对文档的水平座标
event.clientY 相对文档的垂直座标
event.offsetX 相对容器的水平坐标
event.offsetY 相对容器的垂直坐标
 
clientWidth  内容可视区域的宽度
clientHeight 内容可视区域的高度
clientTop    内容可视区域相对容器的垂直坐标
clientLeft   内容可视区域相对容器的水平坐标 
 

 
 
 
 
 
 
 
----------------------------------------------参考 2-----------------------------------------------------
 
Js代码  
  1. var Width_1=document.body.scrollWidth;    //body滚动条总宽    
  2. var Height_1=document.body.scrollHeight;  //body滚动条总高    
  3.     
  4. var Width_2=document.documentElement.scrollWidth;    //documentElement滚动条总宽  
  5. var Height_2=document.documentElement.scrollHeight;  //documentElement滚动条总宽     
  6.   
  7. //------------------------------  
  8. var Width_3=document.body.clientWidth;   //body网页可见区域宽,滚动条隐藏部分不算       
  9. var Height_3=document.body.clientHeight; //body网页可见区域高,滚动条隐藏部分不算    
  10.     
  11. var Width_4=document.documentElement.clientWidth;   //documentElement网页可见区域宽,滚动条隐藏部分不算     
  12. var Height_4=document.documentElement.clientHeight; //documentElement网页可见区域高,滚动条隐藏部分不算   
  13.   
  14. //------------------------------  
  15. var Width_5=window.screen.availWidth;  //屏幕可用工作区宽度(用处不大)     
  16. var Height_5=window.screen.availHeight;//屏幕可用工作区高度     
  17.   
  18. //------------------------------  
  19. var scrollLeft_7=window.top.document.body.scrollLeft;//body水平滚动条位置   
  20. var scrollTop_7=window.top.document.body.scrollTop;//body垂直滚动条位置  
  21.   
  22. var scrollLeft_8=window.top.document.documentElement.scrollLeft;//documentElement水平滚动条位置  
  23. var scrollTop_8=window.top.document.documentElement.scrollTop;//documentElement垂直滚动条位置  
  24.     
  25. alert(" body滚动条总宽:"+Width_1+",body滚动条总高:"+Height_1+     
  26.     ",\n documentElement滚动条总宽:"+Width_2+",documentElement滚动条总高:"+Height_2+    
  27.     ",\n"+   
  28.     "\n body网页可见区域宽:"+Width_3+",body网页可见区域高:"+Height_3+     
  29.     ",\n documentElement网页可见区域宽:"+ Width_4+",documentElement网页可见区域高:"+Height_4+  
  30.     ",\n"+     
  31.     "\n 屏幕可用工作区宽度:"+Width_5+", 屏幕可用工作区高度:"+Height_5+  
  32.     ",\n"+  
  33.     "\n body水平滚动条位置:"+scrollLeft_7+",body垂直滚动条位置:"+scrollTop_7+  
  34.     ",\n documentElement水平滚动条位置:"+scrollLeft_8+",documentElement垂直滚动条位置:"+scrollTop_8  
  35. );            
  javaScript 计算网页内容的宽与高 (浏览器的标准模式与怪异模式)二
 
 
 (需要提一下:CSS中的margin属性,与clientWidth、offsetWidth、clientHeight、offsetHeight均无关)
 

下面是从w3school查到的,说的不是很详细

根节点 
有两种特殊的文档属性可用来访问根节点: 

document.documentElement 
document.body 
第一个属性可返回存在于 XML 以及 HTML 文档中的文档根节点。 

第二个属性是对 HTML 页面的特殊扩展,提供了对 <body> 标签的直接访问。 

http://www.w3school.com.cn/htmldom/dom_nodes_access.asp 
 
 
本文版权归黑马程序员web前端开发学院所有,欢迎转载,转载请注明作者出处,谢谢!
作者:黑马程序员Web前端培训学院;
首发:http://www.itheima.com/news/web.html
分享到:
在线咨询 我要报名
和我们在线交谈!