Adjustments in behavior are observed in the transition group animation when a third element is introduced

Background: I have a div positioned at the top of the page that can be toggled to show or hide using a button. This div sits below the button and above the main content area. To achieve a smooth sliding effect when the div is shown or hidden, I am utilizing the transition-group. Additionally, the content has a margin-top property to create space between itself and the toggleable div.

Requirement: I am looking to add a margin on top of the div so that when it is displayed, there remains a gap between the div and the button. https://i.sstatic.net/6uPJm.jpg

Challenges Faced: I have attempted two approaches:

1) Applying a margin-top to the hiding div. Due to the use of position:absolute on the div during hiding (for the content to overlay it), the div dynamically adjusts its size based on the content within, causing the margin to reduce gradually before the div actually hides, resulting in an undesirable effect. GIF:

2) Inserting an hr element above the div, inside the transition-group. When the hr is omitted, the slide animation functions as expected, with the content smoothly sliding over the div. However, upon including the hr and attempting to hide the div, both the div and hr vanish instantly instead of maintaining visibility while being covered by the sliding content. GIF:

Preferred Visual Outcome without the margin/hr on top:

HTML Code Snippet

<transition-group name="slide">
    <hr class="m-0" v-if="isVisible" key='h'>
    <div class="d-flex" v-if="isVisible" id="filters" key='x'>
        <div class="pl-5">
            <p class="filterTitles">Day</p>
            <app-day-filter v-for="day in weekDay" 
                :key="day.index" 
                :day="day">
            </app-day-filter>
        </div>
        <div class="pl-5">
            <p class="filterTitles">Time of day</p>
            <app-tod-filter v-for="todf in tod" 
                :key="tod.index" 
                :todf="todf">
            </app-tod-filter>
        </div>
    </div>
    <app-event v-for='(eveniment, index) in filterEvent' 
        :key='index' 
        :eveniment='eveniment' 
        :index='index'></app-event>
</transition-group>

CSS Styling Details

.slide-enter {
    opacity:0;
}
.slide-enter-active {
    transition: all 1s ease-out;
}
.slide-leave-active{
    transition: all 1s ease-out;
    opacity: 0;
    position: absolute;
}
.slide-move {
    transition: transform 1s;
}
#filters {
/* border-top: 1px solid lightgrey; */
}

Any suggestions or insights would be greatly appreciated.

Thank you.

Answer №1

The main focus here lies within CSS.

When the hr element is inserted into the layout under the transition-group, and the CSS property transition applies to all, alongside setting the position to absolute during the leave-active state (causing the element to vanish from its initial relative position), an array of elements and properties will transit simultaneously, resulting in unintended consequences.

Nevertheless, resolving the issue without placing margin or hr on top of the transition-group, assuming the button possesses an event handler as shown below:

<button class="filter-button" v-on:click="toggleSlider">Filters</button>

The function toggleSlider will switch the isVisible property crucial for the animated transition:

methods: {
  toggleSlider() {
    this.isVisible = !this.isVisible;
  }
}

Utilize CSS to only transition certain properties like opacity and max-height instead of all properties. By eliminating absolute positioning and employing relative positioning with z-indexing in the following CSS code, the desired effect can be achieved.

/* add bottom margin to the button */
.filter-button {
  margin-bottom: 25px;
}

/* enforce z-indexing with relative positioning */
.filter-group {
  position: relative;
  z-index: 1;
}

/* enforce z-indexing with relative positioning */
.filter-content {
  position: relative;
  z-index: 2;
}

/* hidden states */
.slide-enter,
.slide-leave-to {
  opacity: 0;
  max-height: 0px;
}

/* visible states - adjust max-height as needed */
.slide-enter-to,
.slide-leave {
  opacity: 1;
  max-height: 300px;
}

/* animation entry phase */
.slide-enter-active {
  transition: opacity 0.75s ease-in, max-height 0.5s ease-out;
}

/* animation departure phase */
.slide-leave-active {
  transition: opacity 0.75s ease-out, max-height 0.5s ease-out;
}

/* padding at the bottom of filters section */
.pl-5 {
  padding-bottom: 25px;
}

By including bottom margin to the button and filter section, the spacing between sections remains intact.

I have demonstrated this solution in a CodeSandbox.

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

Using JS/AJAX to update a section of a webpage when a button is clicked

Currently, I am working on developing a generator that will display a random line from a .txt file. Everything is going smoothly so far, but I have encountered an issue - I need a specific part of the page to refresh and display a new random line when a ...

Premature updates detected in state cloning process

My program is a simple one designed to showcase an issue I am currently encountering. It involves an array of objects, each containing a sequence number. When a button is clicked, the sequence number of the first object in the array increases by one. To i ...

Display/Conceal Content with Checkbox Utilizing jQuery

I have been attempting to create a section of an HTML form that will display or hide based on the status of a checkbox. Here is the key code snippet I am using: <script src="/js/jquery.js"></script> <script language="JavaScript"> fun ...

Collapse the active panel on a jQuery accordion with a click event

I'm currently utilizing the Simple jQuery Accordion created by CSS-Tricks, and I am seeking guidance on how to enable the active panel to close when clicking on the link within the dt element. The scenario where I am implementing this is as a menu in ...

What is the best way to send the link's ID to an AngularJS controller?

I have a scenario similar to the following: <a href="#" title="Post 1" id="car1"> Audi car </a> <a href="#" title="Post 1" id="car2"> BMW car </a> How can I pass the ID to the controller? I attempted the following: <a href= ...

Guide to converting a specific tag into div using Javascript

I am working with some HTML code that includes a div: <div class="myDiv"> <a href="" title="">My link</a> <p>This is a paragraph</p> <script>//This is a script</script> </div> Additionally, I ha ...

Following the incorporation of bootstrap into the website, the column divisions mysteriously vanished

After completing the coding for my website, I had to integrate Bootstrap. However, upon adding the grid to the respective spans, the columns mysteriously disappeared. Any help in identifying what went wrong would be highly appreciated. <div class=" ...

The following error occurred while running the command "php artisan ui vue get": ErrorException - Unable to open stream: File

Trying to set up VueJS for my Laravel project, but encountered an error when running php artisan ui vue: Can someone please advise on how to resolve this issue? Thank you. ErrorException > copy(D:\Code\Hung's Friend\management&bso ...

Tips on deactivating automatic email client-specific content (such as Gmail or Outlook) in your emails

I have inserted the following code => <span style="font-size:12px;"><b>Name:</b></span></font><font size="1"><span style="font-size:12px;">Pratik</span> However, when viewed in a browser (such as Outlook ...

Running multiple instances of node.js on independent cores

My goal is to establish 4 separate node.js instances, allocating each one to its own core. I am curious if node.js places new instances on the same core or spreads them out across multiple cores. These instances are independent and handle requests individ ...

What is the process of extracting the query and hash parameters from a URL using JavaScript?

Here is a URL: http://local.evibe-dash.in/tickets/123?ref=avl-error#10610 I'd like to extract ref=avl-error and 10610 into two separate variables. For instance, something like this: var1 = 'avl-error'; var2 = 10610; How can I achieve thi ...

Modal failing to update with latest information

My webpage dynamically loads data from a database and presents it in divs, each with a "View" button that triggers the method onclick="getdetails(this)". This method successfully creates a modal with the corresponding data. function getdetails(par) { ...

Easily create an HTML page from a multitude of intricate JavaScript objects with this convenient method

In my project, I am looking to dynamically generate an HTML page based on JavaScript objects and then present it to the user. For example: var mybutton = { id: 'button_1', x: '0', y: '0', width: '100', h ...

Error in Typescript: Function not being triggered on button click

As someone who is just starting out with typescript, I've been tackling a simple code that should display an alert message in the browser when a button is clicked. I've experimented with the button and input tags, as well as using both onclick ev ...

When invoking a function that has been returned, the error message "invalid function" is displayed

I've been working on a wordle-style game, but I'm running into an issue with fetching the API and getting the validation functions to work properly. Every time I press submit, I keep getting an error saying "getData(...).isCorrect is not a functi ...

Center title with a subtitle beside it

https://i.sstatic.net/giiPr.jpg My goal is to replicate the typography shown above. The main title is centered, while the subtitle is positioned next to the title. I managed to achieve something similar below using Bootstrap's small class. https://i ...

How can I effectively utilize executeScript in Selenium and webdriver.io?

I'm currently working on testing a basic form using Selenium, WebDriver.io, and Node.js (with Mocha). Here is a snippet of the code I have: var webdriverio = require('webdriverio'); var expect = require('expect'); describe(' ...

information not visible on the webpage

Recently, I made an alarming discovery on my website. I have implemented two different types of menus - one for the front page (designated as .navbar-inverse) and another for all other pages (.navbar-default). The front page menu is static while the other ...

GATSBY: Error: Unable to find the specified property 'includes' within an undefined value

I'm struggling to figure out how to properly filter images in my portfolio website as discussed in this post… Every time I attempt it, I encounter the following error: "TypeError: Cannot read property 'includes' of undefined" D ...

Clicking on multiple external links will open them in separate tabs simultaneously

My attempts to find a solution on Google have been fruitless. I am in the process of creating a content aggregator, which is essentially a list of links to external websites. I want each link clicked to open in a new tab. Currently, I am using target="bl ...