Is it possible to create a collapse and expand animation that does not involve transitioning the `height

After extensively researching, I have come across numerous articles advocating for the use of transform and opacity for achieving smooth CSS transitions.

An example can be found here:

The prevailing notion always revolves around:

...the optimization of transition experience should involve preventing any repaint caused by animated CSS.

INQUIRY

I am looking to achieve something similar to the code snippet below where filters boxes expand/collapse upon user interaction. Currently, I am only able to utilize the transition of the height property. Is there an alternative method to accomplish this without transitioning the height property as demonstrated?


Note: This pertains to a mobile view where I aim for the "Filters" section to smoothly push down the "Results" when expanded. The "Results" will include a gallery featuring 30 products with thumbnails, titles, and links.

const styled = window.styled;

const Header_DIV = styled.div`
  position: fixed;
  top: 0; left: 0;
  width: 100%;
  height: 50px;
  background-color: lightblue;
`;

const Main_DIV = styled.div`
  padding-top: 42px;
`;

const Filters1_DIV = styled.div`
  width: 100%;
  background-color: lightgreen;
  cursor: pointer;
  height: ${props => props.open ? '100px' : '16px' };
  transition: height ease-out .5s;
`;

const Filters2_DIV = styled.div`
  width: 100%;
  background-color: lightcoral;
  cursor: pointer;
  height: ${props => props.open ? '100px' : '16px' };
  transition: height ease-out .5s;
`;

const Results_DIV = styled.div`
  background-color: lightgray;
`;

function App() {
  
  const [openFilter1,setOpenFilter1] = React.useState(false);
  const [openFilter2,setOpenFilter2] = React.useState(false);
  
  function toggleFilter1() {
    setOpenFilter1(prevState => !prevState);  
  }
  
  function toggleFilter2() {
    setOpenFilter2(prevState => !prevState);  
  }
  
  return(
    <React.Fragment>
      <Header_DIV>
        Header
      </Header_DIV>
      <Main_DIV>
        <Filters1_DIV
          onClick={toggleFilter1}
          open={openFilter1}
        >
          Filter1 ----------- Click to open/close!
        </Filters1_DIV>
        <Filters2_DIV
          onClick={toggleFilter2}
          open={openFilter2}
        >
          Filter2 ----------- Click to open/close!
        </Filters2_DIV>
        <Results_DIV>
          Results
        </Results_DIV>
      </Main_DIV>
    </React.Fragment>
  );
}

ReactDOM.render(<App/>, document.getElementById('root'));
* {
  box-sizing: border-box;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.3/umd/react-dom.production.min.js"></script>
<script src="//unpkg.com/styled-components/dist/styled-components.min.js"></script>
<div id="root"/>

Answer №1

To achieve a disappearing effect, experiment with the CSS properties transform: scale(0) and opacity: 0 during transitions. However, be prepared to make adjustments to your layout and styles as needed.

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

Send form based on the outcome of the ajax request

I have developed a form that triggers an ajax request upon submission to check the validity of the data. My goal is to automatically submit the form if the ajax response confirms the data is valid, and prevent the form submission if the data is invalid. $ ...

Is it possible to capture a screenshot of a YouTube video at a specific moment?

Imagine a scenario where we have a function called getScreenShot. We invoke it in the following manner: scrShot=getScreenShot(videoID, time, quality) This function is designed to capture a screenshot of the video at the specified time (e.g. 1:23) and in t ...

Class remains unaffected by media query modification

With Bootstrap5 in play and an additional stylesheet added as the last one in the lineup, my goal is to customize the margins of the .content class. The snippet of code below resides at the end of my stylesheet... .content { width: 100vw; m ...

Is there a way to retrieve the value from a moment-formatted date picker in the (YYYY-MM-DD) format?

I have a Vue application with a datepicker from Ant Design Vue that is returning the following moment object: Moment {…} _d: Thu Oct 24 1996 00:00:00 GMT+0800 (Malaysia Time) _f: "YYYY-MM-DD" _i: "1996-10-15" _isAMomentObject: (...) _isUTC: (...) _isVal ...

How can the 'selected' attribute be assigned to 'select/option' tags depending on the option value?

In my code, I have a select input with various options and values. I want to set the 'selected' attribute for the option that corresponds to a specific value X. Here is an example: // If x=3, I want the option with value=3 to be marked as 's ...

Retrieve a particular cookie from the request headers in Express framework

Today, I encountered a problem with express. Let's say we set multiple cookies, but when I check request.headers, only one cookie is returned: cookie: 'userSession=123' For instance, not only is it unreliable to use request.headers.cookie ...

Trouble with Ajax loading asynchronously - webpage refreshing upon form submission

I am currently learning about ajax, and it seems like my code is correct. However, I am facing an issue where the page always refreshes when displaying the returned JSON string. Any assistance on this matter would be greatly appreciated! <script> ...

Combining default and named exports in Rollup configuration

Currently, I am in the process of developing a Bluetooth library for Node.js which will be utilizing TypeScript and Rollup. My goal is to allow users to import components from my library in various ways. import Sblendid from "@sblendid/sblendid"; import S ...

Disabled radio buttons are appearing as if they have been chosen

While working on customizing the styles of radio buttons in a form, I encountered an issue where disabled radio buttons appeared selected even though they were supposed to show as disabled. Here is the code snippet that I used: input ::-ms-clear { ...

Position elements in the center of the page at 33.3333% width

I'm facing a challenge with centering elements that are floated left and have a width of 33.33333% on the page. I want the text inside these elements to remain aligned to the left, but I'm unsure how to go about centering the items: ul.home-pr ...

Is there a way to customize the slicing of *ngFor in a component according to the components it is being injected into?

This code snippet represents a component that needs to be included in other components: <div class="row"> <div class="col-12 [...]" *ngFor="let course of courses"> <div class="card"> ...

Is there a better alternative to using nested async callbacks?

Imagine I need to execute asynchronous actions like sending an email and updating the database. Usually, I would write code like this: send_email(function(err, id){ if(err){ console.log("error"); }else{ update_database(id,function( ...

Easy-to-use form input ensuring a constant total of 100

Looking for a way to collect numerical values from users that total 100% on a dynamically generated form? I'm exploring the idea of using a script or algorithm to adjust text fields as values are changed, ensuring they always add up to 100%. However, ...

Updating the state in React while controlling the change causes OrbitControls from three to malfunction

When using react three with drei orbitControls, I am trying to determine whether the camera is above or below zero while moving it around. Currently, I am using an onEnd listener to trigger my code in the ParentComponent. If I change the state of my parent ...

Unable to display data retrieved from JSON file

I am encountering an unusual issue while trying to retrieve elements from JSON in JavaScript. I fetch a JSON string from a URL using the following code: // Create Request HttpWebRequest req = (HttpWebRequest)WebRequest.Create(@"www.someurl ...

What are the possible arguments for the event type onInput?

While diving into the world of html / javascript / vue, I stumbled upon the following code snippet. <input type="text" onInput="doAction(event);"> <script> var mesdata = { message: 'type your m ...

Utilize flexbox to create a list that is displayed in a column-reverse layout

I am facing a challenge in displaying the latest chat person in the 1st position (active) using Firebase. Unfortunately, Firebase does not have a date field which makes it difficult to achieve this. I have attempted converting the date into milliseconds an ...

I am wondering about the proper method for deleting multiple records simultaneously in Ember Data

My current challenge involves managing a list of record IDs, such as [1, 20, 20]. The process of individually querying and deleting each item is proving to be quite inefficient. store.findRecord('post', 1).then(function(post) { post.deleteRec ...

Is it possible to update the data in a table row and then transfer it to the backend API?

Can someone assist me with updating a record in the database through the front-end table display? Here is a screenshot for reference: https://i.stack.imgur.com/CPLN6.png I need the entire record to be updated in the backend once I click the save button on ...

Text field auto-saving within an iFrame using localStorage is not functioning as expected

My goal is to create a rich text editor with an autosave feature using an iframe. Although each code part works individually, I am struggling to combine them effectively. View LIVEDEMO This graphic illustrates what I aim to accomplish: The editable iFram ...