I am new to javascript. I am trying to make a simple div switch. In user select mode, the div pointer does not fit well. After some searches, I found one working fiddle. But not as expected. See screenshot to see the difference.
When I select some text at the beginning of a paragraph, it works fine.

But when I selected some text from the bottom paragraph, it does not work as expected 
Jsfiddle
I actually work in a version of React. Fiddle is in jQuery
This is my code.
import React from 'react' import {render} from 'react-dom'; export default class App extends React.Component{ constructor(props){ super(props); this.state = { display:'none' , top:'', bottom:'', left:'', right:'', diplayForDown:'none' }; this.handleOnMouseDown = this.handleOnMouseDown.bind(this) this.onMounseUp = this.onMounseUp.bind(this) this.onMouseDwn = this.onMouseDwn.bind(this) this.triggerAlltime = this.triggerAlltime.bind(this) } handleOnMouseDown(){ let sel = window.getSelection && window.getSelection(); let r = sel.getRangeAt(0).getBoundingClientRect(); let relative=document.body.parentNode.getBoundingClientRect(); console.log('Relative ',relative); if(!sel.isCollapsed){ console.log(sel,r); let display = 'block'; let top = (r.bottom - relative.top - 80)+'px'; let bottom = r.bottom+'px'; let left =( r.left)+'px'; let right = (r.right)+'px'; console.log('This is Height',r.bottom-r.top); let selectionHeight = r.bottom - r.top; if(selectionHeight => 22.22){ this.setState({ display, top:top, bottom, left, right }) }else{ this.setState({ display, top, bottom, left, right }) } }else{ this.setState({ display:'none' }) } console.log('Slected') } onMounseUp(e){ e.preventDefault() let sel = window.getSelection && window.getSelection(); if(!sel.isCollapsed){ console.log('Moved Up') } } onMouseDwn(e){ let sel = window.getSelection && window.getSelection(); if(!sel.isCollapsed){ console.log('Moved Down') } } getSelectionHtml() { let html = ""; if (typeof window.getSelection != "undefined") { let sel = window.getSelection(); if (sel.rangeCount) { let container = document.createElement("div"); for (let i = 0, len = sel.rangeCount; i < len; ++i) { container.appendChild(sel.getRangeAt(i).cloneContents()); } html = container.innerHTML; } } else if (typeof document.selection != "undefined") { if (document.selection.type == "Text") { html = document.selection.createRange().htmlText; } } console.log('html',html) return html; } lastCharRTL(txt) { return /[\u0591-\u07FF\uFB1D-\uFDFD\uFE70-\uFEFC]$/.test(txt); } triggerAlltime(e){
source share