Using CSS Translate can cause discrepancies in the X and Y values retrieved from getBoundingClientRect

In my React project, I am working on animating the transition from multiple images to one when a specific image is selected.

The CSS I am using for the animation is based on the x and y position of the original clicked image. Currently, these values are hardcoded. However, when I input them into the CSS 'translate' property, they seem to be inaccurate and I can't determine why.

I positioned a div absolutely with the same values, and that appears to be correct. But, the initial values for 'translate' in the CSS animation do not match what I input.

@keyframes move-image {
    from {
        transform: translate(383.125px, 7.998px) scale(1);
    }
}

.animation {
   animation: move-image 6.6s;
}

You can view the animation CSS in the CSS tab by following this link: https://codepen.io/nickjsdevelope1/pen/QoWEPV

Answer №1

When translating elements, it's important to consider the initial element position as an offset rather than a fixed destination. This concept also applies when positioning elements using top/left coordinates relative to the containing block if the element is absolute or fixed.

Although both translation and positioning involve an offset from an origin, the origins are not necessarily the same for each method.

To visualize this concept, let's look at two elements being translated with the same values:

.box {
  width:50px;
  height:50px;
  background:red;
  transform:translate(50px,50px);
}
.box1 {
  width:50px;
  height:50px;
  background:blue;
  transform:translate(50px,50px);
}
<div class="box">

</div>
<div class="box1">

</div>

Both elements in this example are offset by 50px on both the X and Y axis from their original positions.

If we use position with top/left coordinates, the elements will overlap because they share the same containing block and are offset by the same values from the same point:

.box {
  width:50px;
  height:50px;
  background:red;
  position:absolute;
  top:50px;
  left:50px;
}
.box1 {
  width:50px;
  height:50px;
  background:blue;
  position:absolute;
  top:50px;
  left:50px;
}
<div class="box">

</div>
<div class="box1">

</div>

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

The navigation bar fails to recognize its own open/closed status

My website's navigation bar is experiencing a logical error that I've identified. Despite no syntax errors being detected by the browser (I'm using Chrome) and partial functionality, there appears to be an issue. The main problem with my na ...

Hide additional tabs on the page when the width of the tabs exceeds the page width

I am currently working on a Google module tab application. I have successfully added the tab control with drag and drop functionality within a specific area. However, I now need to limit the number of tabs displayed on the page. Regardless of the total cou ...

Svelte's feature prevents users from inputting on Mapbox

My goal is to prevent user input when the variable cssDisableUserInput is set to true. Here's what I have within my main tags: <div id=userinput disabled={cssDisableUserInput}> <div id="map"> </div> Within my CSS, I&a ...

Benefits of leveraging UI component libraries such as Bootstrap-Vue and React-Bootstrap

What advantages does Bootstrap-Vue offer over traditional Bootstrap? Does it eliminate the need for jQuery dependency? ...

Chart showing a Google Timeline: Allow for each bar to be colored differently when there are multiple bars on the same line

It appears that the Google Timeline charts have a feature to color individual blocks on the timeline as indicated in the documentation at However, there seems to be an issue when two bars overlap on the same line, which is evident in this fiddle: http://j ...

Unable to align only one link on the right as flex-end is not functioning correctly

I need help rearranging my row of items displayed at the top of the page. I want one item to appear on the far right while the rest are aligned to the left. I attempted using align-self: flex-end but it didn't work. Here's the current code snippe ...

React MUI5 component latency when changing disable prop conditionally

I've been experiencing issues with latency when trying to conditionally enable/disable a Material UI button. This problem arose after updating my materialUi and reactjs to the latest versions (React 18, MUI/Material: 5.10.10). I'm using a sample ...

Error message: "Plugin not specified or declared in the webpack configuration file

I'm currently facing an issue with my webpack configuration file for a multi-tech project involving nodeJS, express, socket.io, and React. The problem lies in the fact that certain plugins are not defined as indicated by the following error: Referenc ...

Resolving CSS glitches that mysteriously vanish in Internet Explorer versions 8 and 9

My website was created using WordPress, with various plugins added to enhance its functionality. However, I have encountered an issue when viewing the site in Internet Explorer 8 & 9. The visual effects of the plugins seem to lose all CSS styling in IE bro ...

A guide on integrating a timeline reference using the @prismicio/client library in your Next.js/React application

I am currently utilizing the @prismicio/client library to retrieve data from Prismic within my Next.js application. However, I am facing difficulty connecting the preview mode and preview reference it offers to the client.query call for fetching data relat ...

HTML TABS: Make the first TAB automatically selected

I've been experimenting with tabbing in HTML using a tutorial I found on W3SCHOOLS. While the source code provided works fine, I'm encountering an issue with automatically selecting the first tab by default. The tutorial doesn't cover this, ...

Is it possible to have the smaller column in a flex column layout display full-width when it is wrapped alongside a bigger column?

I am currently using Bootstrap 4 and have a flex layout set up like this: .larger { background: blue; } .smaller { background: red; } <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__ ...

Leveraging interface property to determine the length of another property within the interface

Currently, I am working on an interface that will be used as props for a section component showcasing various types of equipment. Each piece of equipment will be displayed on a card within a grid column. Below is the outline of the interface: interface E ...

Creating a script to open multiple URLs in HTML and JavaScript

Currently, I am working on creating a multiple URL opener using HTML and JavaScript. However, I have encountered an issue where HTTP links are opening fine but HTTPS links are not. Can someone provide assistance with this problem? Below is the code snippet ...

Dealing with problem of highlighting links in Android WebView

I am currently using an html app that is being loaded in a WebView. My goal is to prevent a specific link from loading when clicked by using shouldOverrideUrlLoading, where I return true to stop the URL from loading. While this method works well, the link ...

Enabling a variable to encompass various types within ReactJS

In my code, I have defined two distinct types as follows: type A = $ReadOnly<{ a: ?$ReadOnlyArray<string>, b: ?string, }>; and type B = $ReadOnly<{ c: ?$ReadOnlyArray<boolean>, d: ?number, }>; Now, I am looking to create a ...

Guide to inserting a glyphicon within a bootstrap badge

I've been working on a unique way to display hierarchical data using bootstrap badges and AngularJS. My goal is to create a user-friendly experience where people can easily navigate through categories. There are 3 main types of categories: parent: T ...

The Redux store has been modified, yet the changes are not reflected in the

In my Posts.js component, I am mapping every object in the posts array. Within this function, I attempt to filter all notes that have the same post_id as the id of the current mapped post object and store them in a variable called filteredNotes. I then pas ...

Adjust the paragraph font size within the bootstrap stylesheet

We currently utilize the bootstrap v2 CSS and are interested in globally increasing the font size by a point or two. I am wondering if this adjustment is a straightforward modification within the CSS file or if it requires a more complex approach. After a ...

Is it possible to automatically resize ImageList columns according to the size of the screen?

I decided to use the ImageList component instead of the Grid for displaying a grid of photos with titles, which seems to be the main purpose of the ImageList. However, I encountered an issue where I am unable to specify different breakpoints for varying ...