`

javascript:浮动div,可拖拽div,遮罩层(div和iframe实现)

阅读更多

在网页中,经常需要标题里提到的几种效果,实现比较简单,这里总结一下(IE测试通过),以备日后再用。
预备知识:网页中的高和宽


//获取浏览器的宽和高(多种浏览器)
functiongetNavWidthandHeightInfo()
{
vars="";
s
+="网页可见区域宽:"+document.body.clientWidth;
s
+="网页可见区域高:"+document.body.clientHeight;
s
+="网页可见区域宽:"+document.body.offsetWidth+"(包括边线和滚动条的宽)";
s
+="网页可见区域高:"+document.body.offsetHeight+"(包括边线的宽)";
s
+="网页正文全文宽:"+document.body.scrollWidth;
s
+="网页正文全文高:"+document.body.scrollHeight;
s
+="网页被卷去的高(ff):"+document.body.scrollTop;
s
+="网页被卷去的高(ie):"+document.documentElement.scrollTop;
s
+="网页被卷去的左:"+document.body.scrollLeft;
s
+="网页正文部分上:"+window.screenTop;
s
+="网页正文部分左:"+window.screenLeft;
s
+="屏幕分辨率的高:"+window.screen.height;
s
+="屏幕分辨率的宽:"+window.screen.width;
s
+="屏幕可用工作区高度:"+window.screen.availHeight;
s
+="屏幕可用工作区宽度:"+window.screen.availWidth;
s
+="你的屏幕设置是:"+window.screen.colorDepth+"位彩色";
s
+="你的屏幕设置:"+window.screen.deviceXDPI+"像素/英寸";
s += "当前窗口的内部宽度:"+ window.innerWidth;
s += "当前窗口的内部高度:"+ window.innerHeight;
//alert(s);
}
getNavWidthandHeightInfo();

另外,网页中的元素常见定位需要用到的概念一并总结一下:


HTML中的元素定位:

scrollLeft:设置或获取位于对象左边界和窗口中目前可见内容的最左端之间的距离
scrollTop:设置或获取位于对象最顶端和窗口中可见内容的最顶端之间的距离

scrollWidth:获取对象的滚动宽度
scrollHeight:获取对象的滚动高度

offsetLeft:获取对象相对于版面或由offsetParent属性指定的父坐标的计算左侧位置
offsetTop:获取对象相对于版面或由offsetParent属性指定的父坐标的计算顶端位置
offsetHeight:获取对象相对于版面或由父坐标offsetParent属性指定的父坐标的高度

event.clientX相对文档的水平座标
event.clientY相对文档的垂直座标

event.offsetX相对容器的水平坐标
event.offsetY相对容器的垂直坐标

document.documentElement.scrollTop垂直方向滚动的值

event.clientX+document.documentElement.scrollTop相对文档的水平座标+垂直方向滚动的量

(注意:CSS中的margin属性,与clientWidth、offsetWidth、clientHeight、offsetHeight均无关)

来看一下笔者写的关于定位和关于宽和高的js函数:


/*关于位置(前面两种常用)*/

//1.取鼠标的位置(Coordinate坐标x和y)兼容IE,FF和其他主流浏览器(onmouseover="getMouseCoordinate(event)")
functiongetMouseCoordinate(ev){
vare=ev?ev:(window.event?window.event:null);
if(e.pageX||e.pageY){
alert(
"x:"+e.pageX+"--y:"+e.pageY);
}
else{
alert(
"x:"+event.x+"--y:"+event.y);
}
}

//2.通过控件获取控件在页面的位置(oControl是控件)
functiongetControlCoordinateByControl(oControl){
varleftPos=oControl.offsetLeft;
vartopPos=oControl.offsetTop;
varheight=oControl.offsetHeight;
while(oControl=oControl.offsetParent){
leftPos
+=oControl.offsetLeft;
topPos
+=oControl.offsetTop;
}
alert(
"x:"+leftPos+"--y:"+topPos);
}

//3.通过控件自身取得控件的宽和高,返回width和height
functiongetControlWidthAndHeightByControl(oControl){
//varwidth=oControl.style.width;
//varheight=oControl.style.height;
varwidth=oControl.offsetWidth;
varheight=oControl.offsetHeight;
alert(
"width:"+width+"--height:"+height);
}


PS:关于dom的一些高和宽,下面这张图有助于我们理解的更清楚:

好了,您猜我一定又会说,“Code is cheap.",没错,看代码先:全文链接:

javascript:浮动div,可拖拽div,遮罩层(div和iframe实现)

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics