Answer the question
In order to leave comments, you need to log in
How to center popup block on selected text?
I am writing a text editor, and my task is to make a pop-up block in the center of the selected text, something like this:
I have a script:
var sel = document.selection, range;
var top = 0, left = 0;
// основные браузеры
if (window.getSelection) {
sel = window.getSelection();
if (sel.rangeCount) {
range = sel.getRangeAt(0).cloneRange();
if (range.getClientRects()) {
range.collapse(true);
let rect = range.getClientRects()[0];
// var rect = range.getBoundingClientRect()[0];
alert(rect.width);
left = rect.left;
top = rect.top;
// width = rect.width;
}
}
}
// для IE
else if (sel) {
if (sel.type != "Control") {
range = sel.createRange();
range.collapse(true);
left = range.boundingLeft;
top = range.boundingTop;
}
}
// получение координат
var coords = { top:top, left:left, };
Answer the question
In order to leave comments, you need to log in
Divide your text position value by 2, something like this:
left = rect.left / 2;
top = rect.top / 2;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question