// 返回元素距离当前设备的 leftTop的X与Y轴 rightBottom的X与Y轴
const returnCoordinateXY = (el: any) => {
let elindex = el.getBoundingClientRect();
console.log(elindex);
let toolsHeight = window.outerHeight - document.body.clientHeight; //浏览器的工具栏高度 当算出是负数时说明浏览器全屏状态
let leftTopY = Math.sign(toolsHeight) == 1 ? Math.round(elindex.top + toolsHeight + window.screenY) : Math.round(elindex.top);
let leftTopX = Math.sign(toolsHeight) == 1 ? Math.round(elindex.left + window.screenX) : Math.round(elindex.left);
let rightBottomY = Math.sign(toolsHeight) == 1 ? Math.round(elindex.bottom + toolsHeight + window.screenY) : Math.round(elindex.bottom);
let rightBottomX = Math.sign(toolsHeight) == 1 ? Math.round(elindex.right + window.screenX) : Math.round(elindex.right);
return {
leftTop: {
leftTopY,
leftTopX,
},
rightBottom: {
rightBottomY,
rightBottomX,
},
};
};
js 元素距离电脑设备的坐标位置
于 2022-12-26 19:32:32 首次发布