admin管理员组文章数量:1326330
I would like to absolute-position a div on either the initial or terminal corner of a text selection, as the user drags their mouse along. Unfortunately, Range only provides the index position, not the x/y coordinates. I think there might be a way to do this with range.insertNode, but only for one side. (This might also help someone position an auto-plete box under a contenteditable.)
How can I get the x/y coordinates of a text selection, or of the text cursor?
I would like to absolute-position a div on either the initial or terminal corner of a text selection, as the user drags their mouse along. Unfortunately, Range only provides the index position, not the x/y coordinates. I think there might be a way to do this with range.insertNode, but only for one side. (This might also help someone position an auto-plete box under a contenteditable.)
How can I get the x/y coordinates of a text selection, or of the text cursor?
Share Improve this question asked Jan 8, 2010 at 23:29 mk.mk. 11.7k6 gold badges42 silver badges56 bronze badges 1- 4 stackoverflow./questions/1589721/… – Tatu Ulmanen Commented Jan 8, 2010 at 23:34
2 Answers
Reset to default 6Works in chrome/webkit, positioning the div after the last-selected letter. Usually.
<html><head>
<script type="text/javascript" src="http://ajax.googleapis./ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script>
var $div;
var last;
function find() {
var selection = window.getSelection();
var text = selection.toString();
if (last==text) return; else last=text; // prevent needless putations if no changes to selection
if (selection.rangeCount==0) return;
var range = selection.getRangeAt(0);
var $span= $("<span/>");
newRange = document.createRange();
newRange.setStart(selection.focusNode, range.endOffset);
newRange.insertNode($span[0]); // using 'range' here instead of newRange unselects or causes flicker on chrome/webkit
var x = $span.offset().left;
var y = $span.offset().top;
$div.text(x+" "+y);
$div.css({left:x,top:y+($div.height())});
$span.remove();
}
$(window).load(function() {
$("body").keydown(function(e) {
//find();
setTimeout(find,0);
});
$("body").mousemove(function(e) {
//find();
setTimeout(find,0);
});
$div=$("<div style='border:1px solid red;background:white; position:absolute;'>").appendTo($("body"));
});
</script>
</head>
<body><p>the quick brown fox</p><ul><li>jumps <i>over</i></li></ul><p>the lazy dog</p></body>
</html>
You would have to get the mouse position from the mouse move event. Then you can get the position of the text input element and do the math (make the mouse position relative).
EDIT:
Here is a page where you can get JavaScript code that will get the caret position in pixels.
If you want to use JQuery, you can use this
本文标签: javascript selectionrange coordinatesStack Overflow
版权声明:本文标题:javascript selectionrange coordinates - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742200160a2431754.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论