Can object-fit be preserved while applying a CSS transform?

Currently, I am developing a component that involves transitioning an image from a specific starting position and scale to an end position and scale in order to fill the screen. This transition is achieved through a CSS transform animation on translate and scale properties.

An issue that has arisen is that certain images being transitioned may have been altered by users of the component using the object-fit property. It seems that the CSS translate does not maintain the object-fit property during the transition.

To view a demonstration of this issue, please visit: https://codepen.io/cathyxz/pen/mXgEMB

While I could animate width and height instead, I prefer to stick with properties that browsers can animate efficiently for performance reasons. This means avoiding properties that affect layout, leaving us with only position, rotation, scale, and opacity as viable options for animation.

If you're interested in learning more about high-performance animations, check out: https://www.html5rocks.com/en/tutorials/speed/high-performance-animations/

I am seeking a solution to gradually animate the transition to "uncrop" images without stretching them. Any advice or suggestions would be greatly appreciated!

Answer №1

object-fit is implemented on the element prior to any transformation taking place.

Regardless of the effects of CSS styling on the object, transform steps in and applies the necessary changes. Interestingly, the element remains untouched in the DOM structure. This is why a repaint is not triggered by transform, making it efficient in terms of performance. The only thing that changes is its rendering (the composite layer) which is stretched 4 times along the X axis due to the transform. However, this does not alter the size of the element by fourfold, hence object-fit does not function as anticipated.


Is there a way to gradually uncrop the image? Yes, but not using transform. To achieve this in a cost-effective manner (without causing a repaint within subsequent flow), you will need:

  1. A parent placeholder (with position:relative) set to the height of the image, maintaining space in the document flow.
  2. The element being animated should have position:absolute. By doing so, even if you animate the width, layout won't be affected because the element is outside the document flow.

.transform-placeholder {
  height: 100px;
  position: relative;
}

.transform-placeholder .object-fit {
  width: 100px;
  height: 100px; 
  animation: object-fit 2.1s infinite;
  animation-timing-function: cubic-bezier(.4,0,.2,1);
  
}

@keyframes object-fit {
  0% {
    width: 100px;
  }
  50% {
    width: 400px;
  }
  100% {
    width: 100px;
  }
}
<div class="transform-placeholder">
  <div class="object-fit" style="background-image: url('https://picsum.photos/1600/400?image=857')">
  </div>
</div>

<h2>The animation above ensures no repaint occurs on any DOM elements outside of the animated div.</h2>

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

Update an existing JSON document

Looking for a solution to update the json file while retaining specific strings and values? const fs = require('fs'); var logPath = 'log.json' var logRead = fs.readFileSync(logPath) var logFile = JSON.parse(logRead) LogChannel = &apo ...

Troubleshooting: How to resolve the issue of receiving undefined values when passing API data to a child component with Axios in React

I am encountering difficulties in retrieving data that I pass to a child component. The issue may be related to the Promise not being resolved at the time of access, but I am unsure how to address it. My goal is to ensure that the data is fully retrieved f ...

"I'm encountering an issue with Passport.js where req.isAuthenticated is throwing an error as not

Recently I started working with node and express, along with passport.js for building an authentication feature. However, I encountered an issue while using a middleware function called "checkNotAuthenticated" in my routes. The error message I received was ...

Vuetify's v-text-field not properly aligning text to the center

The alignment of the text in v-text-field is causing issues for me, as I am unable to center it. Despite attempting various solutions, I have not been successful. 1. <v-text-field type="number" class="mr-2 text-center"></v-te ...

What is the best way to align the logo image in the center of the header vertically?

I am trying to vertically center my logo, but I have not been successful with using margin: auto or margin: center. Here is what I have tried so far: ・text-align: center; ・margin: auto; ・margin: center: ・navbar-brand-center The logo currently app ...

Testing a Vue.js/Node.js application using Websockets

I'm working on a Vue project (version 3.0.3) integrated with a Node.js server. Can you provide guidance on how to conduct unit testing for this setup? The application is a game that relies on web-sockets for communication. ...

Accessing properties in JavaScript using square brackets

When I input the following code into my Chrome console: var object = {0:0, 1:1} I am able to retrieve the values by calling object[0] and object[1]. Surprisingly, even when I use object["0"] and object["1"], I still get the same results. Then, if I redef ...

Utilize clipboard functionality in automated tests while using Selenium WebDriver in conjunction with JavaScript

How can I allow clipboard permission popups in automated tests using Selenium web driver, Javascript, and grunt? https://i.stack.imgur.com/rvIag.png The --enable-clipboard and --enable-clipboard-features arguments in the code below do not seem to have an ...

The time.js library is providing inaccurate second values for the given date and time

Utilizing the moment.js library along with the Timezone and BusinessDays extensions in Vue.js, I am developing a datetime format for storing in a MySQL database. Here is the code snippet: import moment from 'moment-timezone' ...

The textboxes are not displaying the floating numbers correctly

My issue involves 3 textareas with numbers displayed next to them. While I previously used a CSS table layout, it became inefficient when adding content between each row. I am puzzled as to why the second number, "2)", appears positioned next to the first ...

Continuously update in JavaScript based on a condition, cease updating when the condition no longer

I have a scenario on my page where specific data needs to be refreshed every minute, but at the same time, the user should be able to view and edit certain modals. I want to ensure that when a modal is open, the data refreshes are paused so that the modal ...

Issue with Laravel: Using `$request->all()` results in an empty array when called using JSON XHR

Having trouble using $.ajax and only the XMLHttpRequest for sending JSON to a Laravel controller. Keep getting 500 errors when attempting to make the request. Here's the method I'm using to send the data: const sendEdit = function(){ ...

A <div> with relative positioning is inhibiting the visibility of absolutely positioned elements without any visible overlap

Recently, I encountered a strange issue on my webpage: Image of page The lines on the left side are supposed to be displayed behind or on top of the text box. However, when I increase their z-index, the lines extend all the way to the top of the page and g ...

What is the best way to pass a variable between different routing functions?

I am currently developing a server-side parser for an API. Each GET request made to my website must first initiate a request to the API, and since this request is always the same, I would like to encapsulate it within its own function. What is the best wa ...

Having trouble removing objects in angular.js?

I have developed an API to be used with Angular.js: angular.module('api', ['ngResource']) .factory('Server', function ($resource) { return $resource('http://localhost\\:3000/api/servers/:name') ...

Position the personalized radio button to be in line with the first choice text

I frequently update this section with a new Jsfidle link to demonstrate the issue when the max-width is set to 300px. For responsive design, adding the "span" tag before radio buttons helps align input content with custom checkbox backgrounds. To see the ...

Access an HTML file in Text Edit on a Mac directly from a web browser

Is there a way to utilize Javascript or another appropriate script to open an HTML file in Text Edit on my Mac? I have created a local web page using Text Edit that has different tabs linking to other Text Edit files within the page. I am looking for a m ...

How to implement a form in PHP that doesn't refresh the page upon submission

I am having an issue with this ajax code. I know it works, but for some reason it's not submitting. <script type="text/javascript"> $(function(){ $('input[type=submit]').click(function(){ $.ajax({ type: "POST", ...

Innovative manipulation of arrays using Javascript

Let's say I have some sample input data: data = [ { color : 'Red', number : 5}, { color : 'Blue', number : 3 }, { color : 'Green', age : 8 }, { color : 'Red', number : 7 } ] and I am looking to combine ...

How can I design a search box similar to the one on the android.com website?

Is it possible to replicate the search box effect on the Android website, where the menu items fade out and the search box expands upon clicking the search icon?view image here See the difference before and after clicking the search icon ...