Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript get distance bwetween elements

// get the bounding rectangles
var div1rect = $("#div1")[0].getBoundingClientRect();
var div2rect = $("#div2")[0].getBoundingClientRect();

// get div1's center point
var div1x = div1rect.left + div1rect.width/2;
var div1y = div1rect.top + div1rect.height/2;

// get div2's center point
var div2x = div2rect.left + div2rect.width/2;
var div2y = div2rect.top + div2rect.height/2;

// calculate the distance using the Pythagorean Theorem (a^2 + b^2 = c^2)
var distanceSquared = Math.pow(div1x - div2x, 2) + Math.pow(div1y - div2y, 2);
var distance = Math.sqrt(distanceSquared);
Comment

PREVIOUS NEXT
Code Example
Javascript :: everything about fetch 
Javascript :: send back text from get request in node.js 
Javascript :: start 
Javascript :: supabase realtime connection 
Javascript :: Javascript multiplier function 
Javascript :: redux store template 
Javascript :: filebase64 template 
Javascript :: select elm inside a specific id in js 
Javascript :: hello worled anglular script 
Javascript :: get position/offset of element relative to a parent container 
Javascript :: descomponer un numero js 
Javascript :: see more in jquery 
Javascript :: on device size changes react-multi-carousel items not showing 
Javascript :: Could not parse as expression: "1, "desc" DataTable 
Javascript :: javascript timer code 
Javascript :: Wikibreak enforcer 
Javascript :: set npm push proxy 
Javascript :: Old Syntax of Router Switch 
Javascript :: Make React Tooltip work for dynamic elements 
Javascript :: input should reject non-alphabetical input reacj js 
Javascript :: currentContract.transferFrom is not a function 
Javascript :: RTC measure react native undefined 
Javascript :: js remove item on index 
Javascript :: react extends component with style 
Javascript :: class parent and class child 
Javascript :: react axios POST with super constructor parent class 
Javascript :: codigo para salvar javascript 
Javascript :: telerik grid destroy table 
Javascript :: Just allow Intergers in Input Field 
Javascript :: multi command run in one in terminal npm 
ADD CONTENT
Topic
Content
Source link
Name
4+2 =