在IHTMLElement接口中 取元素尺寸时,可以找到函数:get_offsetTop,get_offsetLeft,get_offsetWidth,get_offsetHeight.
msdn里的解释是取得元素相对offsetparent的偏移量,通过累加get_offsetTop,和get_offsetLeft,累加直到offestParent为BODY,可以取得元素相对于页面左上角的坐标。但是今天翻msdn的时候发现
SyntaxHTML N/A 
Scripting [ iCoord = ] object.offsetLeft 
也就是说可能在vc中不支持。。然后在IHTMLElement2 里找到几个取元素尺寸的函数clientHeight Retrieves the height of the object including padding, but not including margin, border, or scroll bar.  
clientLeft   Retrieves the distance between the offsetLeft property and the true left side of the client area.  
clientTop    Retrieves the distance between the offsetTop property and the true top of the client area.  
clientWidth  Retrieves the width of the object including padding, but not including margin, border, or scroll bar. 貌似这个是取得一个区域内部的尺寸,不包括边缘或宽度。然后又找到下面的这几个函数:
scrollHeight  Retrieves the scrolling height of the object.  
scrollLeft    Sets or retrieves the distance between the left edge of the object and the leftmost portion of the content currently 
               visible in the window.  
scrollTop     Sets or retrieves the distance between the top of the object and the topmost portion of the content currently visible
               in the window.  
scrollWidth   Retrieves the scrolling width of the object. 
这个应该是取得元素的滚动条的尺寸。但是这里有一个 “currently visible in window” 感觉不直到怎么理解了。是指一个屏幕的大小,还是指整个页面滚动条从上到下的高度和长度以及离顶部和左部的距离呢?通过这个相对滚动条的函数,可以直接取得元素相对于窗口左上角的坐标吗?

解决方案 »

  1.   

    Answer 1. problem seeing the below outline:---------- top
              
    //// Invisible portion ////------------> scrollTop---------- top most portion of the content currently visible in the window//// content////---------- bottom of the content
    Answer 2. problem:
    通过这个相对滚动条的函数,可以直接取得元素相对于窗口左上角的坐标吗?
    ====================================================================
    具体问题应该是:相对于文档左上角的实际坐标, 举例,这个对象的左边实际坐标 = 文档可见的最左部分(left most portion of the content currently visible in the window) + document.body.scrollLeft;实际上, "文档可见的最左部分"就是你说的:
    "通过累加get_offsetTop,和get_offsetLeft,累加直到offestParent为BODY,可以取得元素相对于页面左上角的坐标"
      

  2.   


    <html>
    <head>
    <title>51windows.Net </title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <style type="text/css">
    </style>
    </head>
    <body>
    <SCRIPT LANGUAGE="JavaScript">
    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>
    </body>
    </html>
      

  3.   

    你可以试试 getBoundingClientRect 
    HRESULT getBoundingClientRect(          IHTMLRect **pRect
    );直接获取元素的位置