What is the process to obtain the new top and left coordinates of a child div within a parent div that has been rotated?

I'm attempting to utilize the updated top and left positions of a child div within a rotated parent in order to restrict the draggable area of the parent itself. I am implementing jquery draggable for this purpose.

EDIT: You can find the

<a href="https://jsfiddle.net/2pftt16a/" rel="nofollow noreferrer">jsfiddle here</a>
. My plan is to utilize the red dot on the rotated div as a marker to determine if it collides with the boundaries of the container. I need to extract the new position (top and left) of that red marker so that I can use my existing function to constrain the draggable element.

Answer №1

When determining the offset for the top or left of an element, it is essential to utilize the .getBoundingClientRect() method and take into consideration the window scroll.

This principle applies even to rotated elements, as illustrated in the example below:

function findTopLeft(element) {
  var rec = document.getElementById(element).getBoundingClientRect();
  return {
    top: rec.top + window.scrollY,
    left: rec.left + window.scrollX
  };
}

console.log(findTopLeft('inner'));
#outer {
  position: absolute;
  top: 25%;
  left: 25%;
  width: 100px;
  height: 50px;
  border: 1px solid black;
  transform: rotate(90deg);
}
<div id="outer">
  <div id="inner">Text</div>
</div>

We hope this information proves helpful! :)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Troubleshooting: Why isn't my Axios PUT request functioning properly?

My issue is that axios.put method isn't working, but axios.post works perfectly fine. Here's an example of a successful post request: let response = await axios.post(`${ROOT_URL}/urls/url/`, { post_id, password, content }, { heade ...

changing the data with the d3 streamgraph transition

I am utilizing d3 to create a stream graph that closely resembles the official example found here: http://bl.ocks.org/mbostock/4060954: The only distinction is in how I updated it with fresh data. My aim is not just a vertical (y-value) transition, but al ...

The Material UI Drawer stays closed despite the state being set to true

Currently, I am in the process of developing a WebApp utilizing React and Material UI. Despite following numerous tutorials on implementing the Drawer component and poring over the documentation, I am still struggling to grasp its functionality. Even thou ...

Tips for achieving comma-separated user input via ngModel and appending it to an array

I am working on an email template that includes a cc option. I want users to be able to add email addresses with commas separating them and then push them to an array called $scope.notifyCtrl.cc. How can I accomplish this using AngularJS 1.5 and above? mai ...

Adjust the color of additional SVG paths upon hovering

I have the following code snippet. .icon {stroke-width: 0; stroke: currentColor; fill: currentColor;} a {color: red} a:hover {color: pink} a:hover circle {fill: green !important; color: orange} a:hover path {fill: blue !important} <a href=""> ...

Change the structure of the JavaScript object into a new format

I humbly ask for forgiveness as I am struggling with figuring out how to accomplish this task. It seems like we need to utilize a map function or something similar, but I am having difficulty grasping it. Below is the object 'data' that I am wor ...

What is the optimal location for making API requests in a React/Redux/Reach Router application?

I'm struggling to figure out the optimal location for making API calls in my app. My main component structure looks like this: ReactDOM.render( <React.StrictMode> <Provider store={store}> <Router /> ...

PHP PDO: Object automatically destructs once the function is called

I'm facing a peculiar issue in my PHP file where an object I create at the beginning of the file seems to get deconstructed before the file reaches its end. File1.php: //* Everything is in the same PHP file *// <?php session_start(); inc ...

Utilize ajax to round the floating point number

When working with JSON data, I extract the values and attempt to round them using the script below: function onLoad(){ var output = $('#product'); var id = getUrlVars()["cPath"]; $.ajax({ ...

Format the date using moment() for the last week of the current year and return it with the year. Next year's

I encountered an unusual problem when using Moment.js with Angular.js. When I use the .toISOString() method, I get the correct date, but when I use the .format() method, the date is completely wrong. Here is an example to illustrate the issue: Code snip ...

Issue of displaying buttons based on sibling's height under certain conditions

OBJECTIVE I have undertaken a project to enhance my skills in React and TypeScript by developing a UI chat interface. The design requirement is that when a chat message has enough vertical space, its action buttons should appear stacked vertically to the ...

What is the process for transforming this information into JSON format with Javascript?

I have a server response with data that needs to be converted to JSON using JavaScript. country=Philippines\r\n countryid=840\r\n operator=Globe Telecom Philippines\r\n operatorid=246\r\n connection_status=100\ ...

Incorporating ajax and jquery into html: Step-by-step guide

Previously, I inquired about implementing a show/hide functionality for a div that only renders when a specific link is clicked. (View the original question) Following some advice, I was provided with jQuery and AJAX code to achieve this: function unhide() ...

``There seems to be a problem with navigating to a specific

I'm encountering an issue with this script // App.js ( shrink ) var controller = require('./controller'); app.get('/', controller.index); app.get('/home', controller.home); // /controller/index.js var meta = { ...

Converting the OpenCV GetPerspectiveTransform Matrix to a CSS Matrix: A Step-by-Step Guide

In my Python Open-CV project, I am using a matrix obtained from the library: M = cv2.getPerspectiveTransform(source_points, points) However, this matrix is quite different from the CSS Transform Matrix. Even though they have similar shapes, it seems that ...

Ensure the scrollbar is noticeable and personalized solely within a designated div, such as the left side menu

In the left side menu, I have set the overflow-y: scroll property and a fixed height that is smaller than the entire page's height. I am trying to make the scrollbar always visible only within this left menu. I attempted using -webkit-scrollbar and -w ...

What could be the reason for the absence of search results in my searchbox?

Can anyone assist me in identifying the issue with my search box? I have created a JavaScript search box that validates input against a predefined keyword list. If a word matches, the "HARMI-SOIL 8.0/15 Result Box" should be displayed, otherwise it should ...

Having difficulty displaying the JSON returned from a JQUERY AJAX request in CodeIgniter

I am facing an issue with displaying returned data after making an AJAX call using JQUERY and CodeIgniter. This is my JQUERY code: $.post("<?= site_url('plan/get_conflict') ?>", { user_id : user_id, datetime_from : plan_datetime ...

Utilize Node.js to efficiently send emails to a group of subscribed clients

I have a Node.js website on my local system that sends email notifications to users about trending articles or posts. Here's how my code works: app.get('/:articlename', (req, res)=>{ conn.query(`SELECT views,isAlreadySent FROM article_ta ...

Preventing default events and continuing with jQuery

Currently, I am working on a Django project and have integrated the Django admin along with jQuery to insert a modal dialog between the submission button and the actual form submission process. To accomplish this, I have included the following code snippe ...