获取相对于窗口(x,y)的插入符号位置坐标

摘要

我想获得插入符号的窗口位置(以像素为单位)(当用户开始键入时)。

完整故事

其中有一个多行contentEditable元素,其中包含复杂的HTML结构。
当用户开始在其内部任何地方输入时,在第一次按键时,我想将一个absolute定位的元素放置在插入符号下方。迫切需要将新添加的元素注入到<body>标记中,并且作为contentEditable元素中的DOM节点。 >

因此,我需要插入符号的确切全局坐标。我会很清楚,没有没有文本选择。
这里的选择无关紧要,甚至不可能。

我的代码

通常,一个问题应该包含OP曾尝试并需要协助的一些代码,但在这种情况下,我全都没主意。 input DOM事件不会公开坐标。我至少会提供我所追求的最终结果:

.info{
  border: 1px solid gold;
  padding: 8px;
  background: rgba(255,255,224,.8);
  position: absolute;
  z-index: 9999;
  max-width: 180px;
}
<h2>contentEditable:</h2>
<div contentEditable>
  <p>
    Lorem ipsum dolor sit amet,consectetur adipiscing elit,sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
    <strong>Ut enim ad minim veniam</strong>,quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. 
  </p>

  <p>
    Imagine the caret is somewhere here and the info element is right under it... <div>reprehenderit <em>in</em> voluptate</div> 
    velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat 
    cupidatat non proident,sunt in culpa qui officia deserunt <small>mollit anim</small> 
    id est laborum.
  </p>
</div>
<div class='info' style='top:150px; left:60px;'>Extra info with a long list of some content goes here</div>


免责声明:

  

我对此事做了很多研究,但不幸的是找不到答案。   有些问题看似相似,但看起来更深入,根本没有,还有一些非常老旧且过时了

谢谢!

aachener 回答:获取相对于窗口(x,y)的插入符号位置坐标

经过进一步的挖掘,我发现一个Gist file确实满足了我的要求,
所以我做了一些调整:

/**
 * Get the caret position,relative to the window 
 * @returns {object} left,top distance in pixels
 */
function getCaretGlobalPosition(){
    const r = document.getSelection().getRangeAt(0)
    const node = r.startContainer
    const offset = r.startOffset
    const pageOffset = {x:window.pageXOffset,y:window.pageYOffset}
    let rect,r2;

    if (offset > 0) {
        r2 = document.createRange()
        r2.setStart(node,(offset - 1))
        r2.setEnd(node,offset)
        rect = r2.getBoundingClientRect()
        return { left:rect.right + pageOffset.x,top:rect.bottom + pageOffset.y }
    }
}

/////////////////[ DEMO ]\\\\\\\\\\\\\\\\\\\\

const contenteditable = document.querySelector('[contenteditable]')
const infoElm = document.querySelector('.info')

contenteditable.addEventListener('input',onInput)

function onInput(){
  const caretGlobalPosition = getCaretGlobalPosition()

  infoElm.style.cssText = `top:${caretGlobalPosition.top}px;
                           left:${caretGlobalPosition.left}px;`
}
.info{
  border: 1px solid gold;
  padding: 8px;
  background: rgba(255,255,224,.8);
  position: absolute;
  z-index: 9999;
  max-width: 180px;
  display:none;
}

.info[style]{ display:block; }
<h2>Place caret somewhere and type:</h2>
<div contenteditable>
  <p>
    Lorem ipsum dolor sit amet,consectetur adipiscing elit,sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
    <strong>Ut enim ad minim veniam</strong>,quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. 
  </p>

  <h2>
    Imagine the caret is somewhere here and the info element is right under it... <div>reprehenderit <em>in</em> voluptate</div> 
    velit esse cillum dolore eu fugiat f nulla pariatur. Excepteur sint occaecat 
    cupidatat non proident,sunt in culpa qui officia deserunt <small>mollit anim</small> 
    id est laborum.
  </h2>
</div>
<div class='info'>Extra info with a long list of some content goes here</div>


我发现的另一种解决方案:https://github.com/component/textarea-caret-position

本文链接:https://www.f2er.com/3054259.html

大家都在问