获取contenteditable文本的坐标

直接getRect().x 直接获取就可以了

function getRect() { //获取坐标
  let x = 0;
  let y = 0;
  let height = 0;
  let sel = window.getSelection();
  const winW = document.documentElement.clientWidth
  if (sel.rangeCount) {
    let range = sel.getRangeAt(0).cloneRange();
    if (range.getClientRects()) {
      range.collapse(true);
      let rect = range.getClientRects()[0];
      if (rect) {
        width = rect.width
        height = rect.height
        y = rect.top;
        x = rect.left;
      }
    }
  }
  return { x: x, y: y, height: height, width: height };
}
export { getRect }

你可能感兴趣的:(获取contenteditable文本的坐标)