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

来源:互联网 发布:非常网络 易中天 编辑:程序博客网 时间:2024/05/01 09:32

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


//获取浏览器的宽和高(多种浏览器)
function getNavWidthandHeightInfo()
{
var s = "";
+= " 网页可见区域宽:"+ document.body.clientWidth;
+= " 网页可见区域高:"+ document.body.clientHeight;
+= " 网页可见区域宽:"+ document.body.offsetWidth + " (包括边线和滚动条的宽)";
+= " 网页可见区域高:"+ document.body.offsetHeight + " (包括边线的宽)";
+= " 网页正文全文宽:"+ document.body.scrollWidth;
+= " 网页正文全文高:"+ document.body.scrollHeight;
+= " 网页被卷去的高(ff):"+ document.body.scrollTop;
+= " 网页被卷去的高(ie):"+ document.documentElement.scrollTop;
+= " 网页被卷去的左:"+ document.body.scrollLeft;
+= " 网页正文部分上:"+ window.screenTop;
+= " 网页正文部分左:"+ window.screenLeft;
+= " 屏幕分辨率的高:"+ window.screen.height;
+= " 屏幕分辨率的宽:"+ window.screen.width;
+= " 屏幕可用工作区高度:"+ window.screen.availHeight;
+= " 屏幕可用工作区宽度:"+ window.screen.availWidth;
+= " 你的屏幕设置是: "+ window.screen.colorDepth +" 位彩色";
+= " 你的屏幕设置: "+ 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)")
function getMouseCoordinate(ev) {
var e = 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是控件)
function getControlCoordinateByControl(oControl) {
var leftPos = oControl.offsetLeft;
var topPos = oControl.offsetTop;
var height = oControl.offsetHeight;
while (oControl = oControl.offsetParent) {
leftPos 
+= oControl.offsetLeft;
topPos 
+= oControl.offsetTop;
}
alert(
"x:" + leftPos + "--y:" + topPos);
}

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


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

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

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

原创粉丝点击