js獲取距離螢幕的座標

2021-09-07 19:39:13 字數 896 閱讀 4049

首先計算js距離瀏覽器視窗的座標:

let x = $('#test').offset().left + window.screenleft;

let y = $('#test').offset().top;

再計算瀏覽器距離螢幕的寬和高,通過window.screenleft和window.screentop獲取瀏覽器距離螢幕的偏移量;

兩者相加就可以得到距離螢幕的座標:

let x = $('#test').offset().left + window.screenleft;

let y = $('#test').offset().top + window.screentop;

y軸部分還缺少瀏覽器工具欄的高度,

window.outerheight:瀏覽器高度

window.innerheight:瀏覽器可用高度

工具欄高度=window.outerheight-window.innerheight。

最終座標為:

let x = $('#test').offset().left + window.screenleft;

let y = $('#test').offset().top + window.screentop + window.outerheight - window.innerheight;

下面這張圖是比較直觀的了解。

wpf獲取螢幕座標

wpf程式設計時,我們經常使用mouse.getposition iinputelement relativeto 或mouseeventargs.getposition iinputelement relativeto 來獲取滑鼠相對於某一介面元素的相對位置,這自然也聯想到另外的乙個問題 如何獲取...

js獲取元素座標

function getelementpos elementid var parent null var pos var box if el.getboundingclientrect ie else if document.getboxobjectfor gecko else safari ope...

js 獲取當前元素距離視窗的距離

獲取dom到body左側和頂部的距離 getboundingclientrect 平時在寫js的時候,偶爾會需要用js來獲取當前div到 body 左側 頂部的距離。網上查一查,有很多都是通過offsettop offsetleft來計算出來的。我按照網上的查到的資料用了一次,算出來了一堆錯誤答案。...