简体   繁体   English

通过javascript获取可编辑div上的插入符坐标。

[英]Get caret coordinates on a contenteditable div through javascript.

I've been searching a lot but i could't find a way to get (X,Y) coordinates for the caret on a content editable div,there's a lot of content on the web about this,but the problem none of the pages i found get the coordinates according to the top of the div,so my question in a simpler way is: 我已经进行了很多搜索,但是找不到在可编辑的内容div上获得插入符号的(X,Y)坐标的方法,网络上有很多与此相关的内容,但是没有一个问题我发现根据div的顶部获取坐标,所以我的问题以一种更简单的方式是:

How can i get the (X,Y) coordinates of the caret on a contenteditable div,but the Y coordinate must be calculated according to the top of the div and not from the top of the visible part of the page? 我如何在可编辑内容的div上获得插入符号的(X,Y)坐标,但Y坐标必须根据div的顶部而不是页面可见部分的顶部进行计算?

Note 1: I need specially the Y coordinate. 注意1:我特别需要Y坐标。

Note 2: I can only use pure javascript,no JQUERY. 注意2:我只能使用纯JavaScript,不能使用JQUERY。

My way: 我的方式:

I haven't found a direct way to do this,so as a workaround i thought on getting the Y coordinate according to the visible part of the page and the hidden part of the Div and sum the results(I'm currently working on this but i getting no luck!). 我还没有找到一种直接的方法来做,所以作为一种解决方法,我想根据页面的可见部分和Div的隐藏部分获取Y坐标并求和结果(我目前正在研究但我没有运气!)。

  1. get selection and range 得到选择和范围

      var sel = window.getSelection(); var range = sel.getRangeAt(0); 
  2. insert collapsed div 插入折叠的div

      var div = document.createElement(...) range.insertNode(div) 
  3. get div coordinates 获取div坐标

  4. remove the div 删除div

to find on (X,Y) coordinate and using pure javascript, this function I found may do the trick: in this Source you will find all explanation about their process (the code below is taken from the same source) : 为了找到(X,Y)坐标并使用纯JavaScript,我发现的这个功能可以解决这个问题:在此Source中,您将找到有关其过程的所有解释(以下代码来自同一源)

function getPosition(el) {
  var xPos = 0;
  var yPos = 0;

  while (el) {
    if (el.tagName == "BODY") {
      // deal with browser quirks with body/window/document and page scroll
      var xScroll = el.scrollLeft || document.documentElement.scrollLeft;
      var yScroll = el.scrollTop || document.documentElement.scrollTop;

      xPos += (el.offsetLeft - xScroll + el.clientLeft);
      yPos += (el.offsetTop - yScroll + el.clientTop);
    } else {
      // for all other non-BODY elements
      xPos += (el.offsetLeft - el.scrollLeft + el.clientLeft);
      yPos += (el.offsetTop - el.scrollTop + el.clientTop);
    }

    el = el.offsetParent;
  }
  return {
    x: xPos,
    y: yPos
  };
}

try this: 尝试这个:

var selection = window.getSelection(),
    range = selection.getRangeAt(0),
    rect = range.getClientRects()[0];

in the rect var you should find the position: 在rect var中,您应该找到以下位置:

rect.left -> for the x coordinate rect.top -> for the y coordinate rect.left >为x坐标rect.top >为y坐标

there is also rect.width and rect.height . 还有rect.widthrect.height In case the user has some text selected rect.width will be >0. 如果用户选择了一些文本,则rect.width将> 0。

try this http://plnkr.co/edit/T2S7sKByTIesEVC6R8jQ?p=preview 试试这个http://plnkr.co/edit/T2S7sKByTIesEVC6R8jQ?p=preview

document.addEventListener("DOMContentLoaded", function(event) { 
  var pointer = document.querySelector('#marker')
  function checkCaret() {
    if(document.getSelection()) {
      if(document.getSelection().baseNode) {
        var position = document.getSelection().baseNode.parentNode.getBoundingClientRect()
        pointer.style.top = position.top + 'px'
      }
    }
  }
  setInterval(checkCaret, 500)
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM