最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Calculate width & height of the selected text (javascript) - Stack Overflow

programmeradmin3浏览0评论

I need to calculate the width and the height of the selected/highlighted text using JavaScript.

I am using the following code written by Tim Down, as the starting point,

function getSelectionCoords() {
    var sel = document.selection, range;
    var x = 0, y = 0;
    if (sel) {
        if (sel.type != "Control") {
            range = sel.createRange();
            range.collapse(true);
            x = range.boundingLeft;
            y = range.boundingTop;
        }
    } else if (window.getSelection) {
        sel = window.getSelection();
        if (sel.rangeCount) {
            range = sel.getRangeAt(0).cloneRange();
            if (range.getClientRects) {
                range.collapse(true);
                var rect = range.getClientRects()[0];
                x = rect.left;
                y = rect.top;
            }
        }
    }
    return { x: x, y: y };
}

The left & the top co-ordinates are being displayed correctly. To calculate the width & the height, I need the right & the bottom positions as well.

So I added few lines of code to find the bottom & the right positions (Code available here - /). But to my surprise, the code displays the left & the right co-ordinates always the same, even though there is visible difference between them (tested only in firefox).

There is no problem with the top & the bottom positions, as they are working perfectly, which will help me calculate the height. But to calculate the width, I would still need the correct right co-ordinate.

Can anybody point any flaws with the code? or any alternate approach, using which I can calculate the width & the height of the selected text?

I need to calculate the width and the height of the selected/highlighted text using JavaScript.

I am using the following code written by Tim Down, as the starting point,

function getSelectionCoords() {
    var sel = document.selection, range;
    var x = 0, y = 0;
    if (sel) {
        if (sel.type != "Control") {
            range = sel.createRange();
            range.collapse(true);
            x = range.boundingLeft;
            y = range.boundingTop;
        }
    } else if (window.getSelection) {
        sel = window.getSelection();
        if (sel.rangeCount) {
            range = sel.getRangeAt(0).cloneRange();
            if (range.getClientRects) {
                range.collapse(true);
                var rect = range.getClientRects()[0];
                x = rect.left;
                y = rect.top;
            }
        }
    }
    return { x: x, y: y };
}

The left & the top co-ordinates are being displayed correctly. To calculate the width & the height, I need the right & the bottom positions as well.

So I added few lines of code to find the bottom & the right positions (Code available here - http://jsfiddle.net/pankajparashar/kv2Bp/). But to my surprise, the code displays the left & the right co-ordinates always the same, even though there is visible difference between them (tested only in firefox).

There is no problem with the top & the bottom positions, as they are working perfectly, which will help me calculate the height. But to calculate the width, I would still need the correct right co-ordinate.

Can anybody point any flaws with the code? or any alternate approach, using which I can calculate the width & the height of the selected text?

Share Improve this question asked Sep 26, 2012 at 13:56 Pankaj ParasharPankaj Parashar 10.2k6 gold badges36 silver badges50 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 18

Here's some code to get the dimensions of the selection's bounding rectangle. It's pretty similar to the original code.

Demo: http://jsfiddle.net/UFkjy/

function getSelectionDimensions() {
    var sel = document.selection, range;
    var width = 0, height = 0;
    if (sel) {
        if (sel.type != "Control") {
            range = sel.createRange();
            width = range.boundingWidth;
            height = range.boundingHeight;
        }
    } else if (window.getSelection) {
        sel = window.getSelection();
        if (sel.rangeCount) {
            range = sel.getRangeAt(0).cloneRange();
            if (range.getBoundingClientRect) {
                var rect = range.getBoundingClientRect();
                width = rect.right - rect.left;
                height = rect.bottom - rect.top;
            }
        }
    }
    return { width: width , height: height };
}

Try using range.getBoundingClientRect instead of range.getClientRects.

JSFiddle

发布评论

评论列表(0)

  1. 暂无评论