Vue transition unexpectedly ending in the wrong position due to nested transitions

I've encountered an issue while trying to implement a transition on a child div within a 'parent' div that already has its own transition. It seems like the child transition is conflicting with the parent transition, causing it to end up in the wrong position.

What I'm aiming for is to have Child Container A slide out to the left and then have Child Container B slide into view from the right after Child Container A when a button is clicked.

You can see the problem replicated here: https://codepen.io/BONDJAMES/pen/mdegmMe

How can I achieve the desired effect of sliding out Child Container A to the left and having Child Container B slide in from the right after a toggle?

HTML

<div id="app">
  <div class="viewBlocks">
    <transition name="slide">
      <div class="left" v-if="!MaxView">
        <div class="subContainer">
          <div class="container">
            ...
          </div>
        </div>
      </div>
    </transition>

    <transition name="slide">
      <div class="right">
        <div class="subContainer">
          <button @click="toggleBtn">Toggle</button>
          Parent DIV
          <div class="container">
            <transition name="first-slide">
              <div v-if="!showMiniB" class="miniContainerA">

                <button @click="slideDivs">Slide A out - B In</button>
                Child Container A
              </div>
            </transition>

            <transition name="second-slide">
              <div v-if="showMiniB" class="miniContainerB">
                <button @click="slideDivs">Slide B out - A</button>
                Child Container B
              </div>
            </transition>

          </div>
        </div>
      </div>
    </transition>
  </div>
</div>

CSS

body {
     margin: 0;
}
 * {
     box-sizing: border-box;
}
 .viewBlocks {
     display: flex;
}
 .viewBlocks > * {
     flex: 1 1 0%;
     overflow: hidden;
}
 .viewBlocks .subContainer {
     margin: 10px;
     background-color: white;
     min-height: 200px;
     padding: 1rem;
    /* prevent text wrapping during transition? */
     min-width: 300px;
}
 .viewBlocks .left {
     background-color: blue;
}
 .viewBlocks .right {
     background-color: red;
}
 .viewBlocks .container {
     background-color: white;
}
 .slide-leave-active, .slide-enter-active {
     transition: 1s cubic-bezier(0.5, 0, 0.3, 1);
}
 .slide-leave-to, .slide-enter {
     flex-grow: 0;
}

.miniContainerA{
  border: green 3px solid;
  text-align: center;
  height: 60px
}

.miniContainerB{
  border: pink 3px solid;
  text-align: center;
  height: 60px
}

.first-slide-enter {
  opacity: 0;
  transform: translatex(-100%);
  transition: all 0.7s ease-out;
}
.first-slide-enter-to {
  opacity: 1;
  transform: translatex(0);
  transition: all 0.7s ease-out;
}
.first-slide-leave-to {
  opacity: 0;
  transform: translatex(-100%);
  transition: all 0.7s ease-out;
}

.second-slide-enter {
  opacity: 0;
  transform: translatex(0);
  transition: all 0.7s ease-out;
}
.second-slide-enter-to {
  opacity: 1;
  transform: translatex(-100%);
  transition: all 0.7s ease-out;
}
.second-slide-leave-to {
  opacity: 0;
  transform: translatex(0);
  transition: all 0.7s ease-out;
}
.second-slide-leave {
  transform: translatex(-100%);
}

VUE

var app = new Vue({
  el: '#app',
  data: () => ({
    MaxView: false,
    showMiniB: false
  }),
  methods: {
    toggleBtn(){
      this.MaxView = !this.MaxView
    },
    slideDivs(){
      this.showMiniB = !this.showMiniB
    }
  }
})

Answer №1

If you need to make adjustments to the CSS, focus on tweaking the styling of miniContainerA and miniContainerB. In this example, anchor them to the top by setting the parent element as absolute and the child elements (miniContainers) as relative.

.viewBlocks .container {
     background-color: white;
   position: relative;
   top: 0;
}

.viewBlocks .container div{
     background-color: yellow;
   position: absolute;
   top: 0;
   left:0;
   width:100%;
}

After making these adjustments, fine-tune your transition percentages by adding 100% to your translatex values.

.second-slide-enter {
  opacity: 0;
  transform: translatex(100%);
  transition: all 0.7s ease-out;
}
.second-slide-enter-to {
  opacity: 1;
  transform: translatex(0%);
  transition: all 0.7s ease-out;
}
.second-slide-leave-to {
  opacity: 0;
  transform: translatex(100%);
  transition: all 0.7s ease-out;
}
.second-slide-leave {
  transform: translatex(0%);
}

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

Vue.js: The instance does not have a defined property or method

I have a basic vue component snippet: Vue.component('custom-component', { props:["value"], template: '#custom-template', methods: { customMethod: function() { console.log("This is functional") } }, data: functio ...

Top method for individually choosing multiple selections from a foreach loop output

Need help with a loop: foreach ($userItems_get as $item => $value) { if ($value['prefab'] == 'wearable') { echo $value['name'] . "</br>"; echo "<img src=\"{$value['image_invent ...

Modify an HTML table and record any modifications as POST requests using Javascript

As a beginner in JavaScript and HTML, I am open to corrections if I am not following the correct practices. Recently, I delved into editing an HTML form table. {%extends 'base.html'%} {%block content%} <html> <body> <h4> Search ...

Switching between height: 0 and height:auto dynamically with the power of JavaScript and VueJS

Currently, I am changing the height of a container from 0px to auto. Since I do not know the exact final height needed for the container, using max-height could be an option but I prefer this method. The transition from 0 to auto works smoothly, however, ...

Provide JWT token back to JavaScript Single Page Application after completing OAuth login

Currently, I am building a single-page application (SPA) using Vue.js, which will eventually become a Cordova application. Additionally, I am developing a backend API with Lumen. I am working on implementing login functionality with Facebook and Google. T ...

Leveraging class names to dynamically apply CSS styles to components based on their props value

In the process of developing a collection of reusable components, I have utilized material-ui and styled them with CSS. One specific requirement is to dynamically set the width of a component through a prop passed into a custom button. My goal is to lever ...

Unpredictable Behavior of CSS Transition with JS createElement() Function

I'm using JavaScript to create a div element and I'm adding the CSS property transition: .5s linear top; When the user clicks on the element (onmousedown event), it is supposed to smoothly move to the top of the screen and then be deleted using ...

A guide to exporting a class in ReactJS

I am currently working on exporting some classes from my music player file - specifically playlist, setMusicIndex, and currentMusicIndex. const playlist = [ {name: 'September', src: september, duration: '3:47'}, {name: 'hello ...

Creating an HTML Table with Database Data utilizing JSP, Java, and JavaScript

Challenge: I am currently tasked with developing a web application for a university course using JSP, Java, Javascript, and JQuery without relying on any external frameworks or libraries. The main goal is to create a calendar-like table that displays cours ...

Transferring data from an Angular variable to an Express backend using a POST request

req.body seems to be coming up empty for me. I've tried adding content-type headers as json, but it's not making a difference. Can anyone point me in the right direction? Thank you. UPDATE: Just to clarify, my Angular frontend is successfully hi ...

What is the appropriate utilization of the await keyword within concise if-else statements in JavaScript?

Along with transitioning away from jQuery, my main focus is to make my code more efficient. In large scale enterprise applications, the excessive use of jQuery and JavaScript can become problematic. I have decided to switch back to vanilla JavaScript for ...

Guide to filtering out cookies using express-winston

After reviewing the README for express-winston, it appears that removing the headers from the logged line is quite straightforward: we can easily do so by adjusting the requestWhitelist option. However, this will disable logging all headers. Is there a wa ...

Identify the browser dimensions and implement CSS styling for all screen resolutions

I am currently facing an issue with a function that I have created to apply CSS changes to a menu based on browser resizing and different resolutions. The problem lies in the fact that my function does not seem to be correctly interpreted by the browser. W ...

What is the best way to utilize Node.js to serve a single page HTML file on port 80?

This is how my project structure is organized: project assets/images/ css/application.css js/application.js font node_modules index.html server.js package.json I am aiming to run 'node server.js" in the project directory and have it serve on port ...

Caution: It is not possible to make updates on a component while inside the function body of another component, specifically in TouchableOpacity

I encountered this issue: Warning: Trying to update a component from within the function body of another component. Any suggestions on how to resolve this? Interestingly, the error disappears when I remove the touchable opacity element. ...

What is the best way to change CSS style for a button when clicked using JavaScript?

Is there a way to switch the background style of a CSS div when clicking the same button? function changeBg() { var divElem = document.getElementById("change-bg"); if (divElem.style.backgroundColor === "yellow") { divElem.style.backgroundColor ...

Discover the Mongoose Document and Arrange in a Random Order with Various Levels?

I am currently using a .find() method to sort documents in a collection based on their "status" and ordering them by most recent. Is it possible to first sort by status, and then randomly sort within each tier? For example, I have users divided into three ...

Tracking dependencies in Vue 3 after running asynchronous code using watchEffect

I am currently grappling with the concept outlined in this note from the watchEffect documentation TIP watchEffect only keeps track of dependencies during its synchronous execution. When employed with an asynchronous callback, only properties accessed be ...

Arrange the Proxy Array of Objects, the localeCompare function is not available

Encountering an error while attempting to implement ES6 arrayObj.sort(a,b) => a.property.localeCompare(b.property) syntax: Getting TypeError: a.property.localeCompare is not a function. Suspecting that localeCompare might not be in scope, but unsure ...

AngularJS: Issue with ng-show and ng-click not functioning when button is clicked

I have a specific requirement where I need to display and hide the description of each column in a table when a button is clicked. Here is the visual representation of what I have: the table In my HTML code, I have defined a button with ng-click as a func ...