24615

Calculating xy-position of text selection

I am trying to create my own text selection with DOM-elements. Yes, I mean the blue background you see behind the text when you select it in this element. The idea is to halt the default behavior (the blue color) and use my own elements to do the job by finding the xy-position of the selection and then placing absolute positioned elements. I want to be able to do this with a regular div.

I'm thinking I need 3 elements. One for the top row (which may be incomplete), one for the middle chunk, one for the last (same as top). Here's an image that helps you understand: <img src="https://i.stack.imgur.com/e2YdE.png" alt="Image showing the needed selection elements">

I've been thinking of catching mouseup/down and then mousemove and then check window.getSelection() but so far I'm having trouble getting anywhere.

Using the CSS ::selection will not work because the element will not have focus.

I appreciate all help I can get! Thanks in advance.

Edit: Stumbled upon https://code.google.com/p/rangy/ which might be of help? Anyone with experience with this plugin?

Edit2: Cross-browser support is required.

Answer1:

You can use getClientRnge:

var element = document.getElementById('element') element.onmouseup = function(){ var selection = document.getSelection(), range = selection.getRangeAt(0), clientRects = range.getClientRects() console.log(clientRects) }

http://jsfiddle.net/XjHtG/

This will return the left, right, top, bottom, width and height of all selections made.

Recommend

  • What's the most efficient way to read, rebuild, and replace a block of content in a file using
  • UITabBarController application and pushViewController fail
  • Position absolute prevents browser to calculate correct height. Is it possible to fix this issue
  • When add include netfilter.h i got error that fields 'in','in6' has incomplete t
  • float:left paragraph followed by float:none paragraph
  • Split an image into 64x64 chunks
  • Centering a specific element among others with flexbox [duplicate]
  • Spacing/Leading PdfPCell's elements
  • Why shared pointer assignment does 'swap'?
  • Is there a way to pivot a customer ID and a their most recent order dates?
  • Rails 3, Custom Actions, and HTML request methods
  • CRASH: *** -[__NSArrayM objectAtIndex:]: index 4294967295 beyond bounds [0 .. 9]
  • XNU incudes in Kext
  • Raphael.js function getBBox give back NAN/NAN/NAN in IE8
  • matching similar elements in between two lists
  • Serve file to user over http via php
  • IE10 strips out hashtag from the URL
  • Create a link to a web page that runs a Javascript function on the page
  • Position: fixed nav does not stay fixed
  • Using a canvas object in a thread to do simple animations - Java
  • uniform generation of points on 3D box
  • SharedPreferences or SQLite Database?
  • How can I enlarge video fullscreen without the affected interface project in as3?
  • Debug.DrawLine not showing in the GameView
  • Python urlparse: small issue
  • Change multiple background-images with jQuery
  • Algorithm for a smudge tool?
  • FileReader+canvas image loading problem
  • Q promise. Difference between .when and .then
  • Insert into database using onclick function
  • Deselecting radio buttons while keeping the View Model in synch
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Can Jackson SerializationFeature be overridden per field or class?
  • Knitr HTML Loop - Some HTML output, some R output
  • Unanticipated behavior
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Android Studio and gradle
  • python draw pie shapes with colour filled
  • Can't mass-assign protected attributes when import data from csv file
  • Unable to use reactive element in my shiny app