獲取元素 在瀏覽器窗口中的 座標

嗯哼。很少說,直接上代碼了。瀏覽器

  • // 獲取元素 在 瀏覽器窗口 中的 座標     Test Already.
    function posInClient(obj){
        var theClient = obj.getBoundingClientRect();    // 獲取元素的左,上,右和下分別相對 瀏覽器視窗 的位置
        
        // 在IE中,默認座標從(2,2)開始計算,致使最終距離比其餘瀏覽器多出兩個像素,須要作如下兼容
        var top2px = document.documentElement.clientTop;
        var left2px = document.documentElement.clientLeft;
        
        return {
            top: theClient.top - top2px,
            left: theClient.left - left2px,
            
            bottom: theClient.bottom + top2px,
            right: theClient.right + left2px
        };
    }
相關文章
相關標籤/搜索