Transition within Vuejs moves forwards and backwards, with a unique feature that allows it to skip directly to

I am in the process of developing a slider element that consists of only 2 items. My goal is to ensure that these items smoothly slide back and forth to the left and right when I click on the back or next button. While everything functions correctly when I click next, and the elements scroll and fade seamlessly, an issue arises when I click back. No matter what actions I take, the first element seems to jump ahead in time. To better illustrate this problem visually, please refer to the following gif:

https://i.stack.imgur.com/5hvZT.gif

For further details, you can view the code snippet on JSFiddle - http://jsfiddle.net/eywraw8t/273583/

The CSS code responsible for styling the slider animation is as follows:

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


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

Below is the HTML structure of my slider:

<transition name="first-slide">
            <div v-if="!newShortcut.link">

                <div id="drop-shortcut" class="drag-file clickable" @click="addShortcut">
                    <i class="fas fa-file-upload fa-lg"></i>
                    <p style="margin:20px 0 0;">Drag file here
                        <br> or click to browse</p>
                </div>                      
                <div>
                        <button @click="newShortcut.link = !newShortcut.link">Next</button>
                </div>

            </div>
        </transition>

        <transition name="second-slide">
            <div v-if="newShortcut.link">
                <div id="drop-icon" class="drag-file" @click="">
                    <i class="far fa-file-image fa-lg"></i>
                    <p style="margin:20px 0 0;">Drag file here
                        <br> or click to browse</p>
                </div>
                <div>
                    <button @click="newShortcut.link = !newShortcut.link">back</button>
                </div>
            </div>
        </transition>

I am looking for assistance on resolving this issue and making the slider function as intended. Your help will be greatly appreciated!

Answer №1

It seems @Julian was right about the blocking issue, however, there is an alternative solution that may suit your needs better. Take a look at this fiddle: http://jsfiddle.net/xcmn76Lo/

The key to resolving the problem was adjusting the second slide. By utilizing a hook similar to enter, you can specify where the leave transition should begin from. In this scenario, as the other div immediately takes up its space, it's necessary to start the leave transition with appropriate offsetting.

.second-slide-leave{
  transform:translatex(-100%);
}

Instead of transitioning to 100%, set leave-to to 0 (the position it would hypothetically reach if moved by the incoming div).

.second-slide-leave-to{
    opacity: 0;
    transform: translatex(0);
    transition: all 1.5s ease-out;
}

Answer №2

The issue arose due to the display of your elements. Each slide is set as a block, taking up space in your layout.

When you were on the second slide and clicked the back button, there was no slide 1 present in the layout. As Vue tried to insert slide 1 back in, it took up space, causing the second slide to abruptly shift from left to right.

To remedy this behavior, consider changing each slide's position to absolute. This way, when reinserted, they won't affect neighboring elements' positions like they would with a relative position.

#app>div {
  position: absolute;
  height: 200px;
  width: 200px;
  text-align: center;
  left: 0;
  top: 0;
}

Also, adjust the CSS transition for the second slide slightly:

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

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

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

For reference, check out the full JS Fiddle here: http://jsfiddle.net/eywraw8t/273631/

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

In my Cordova application, I am able to print the locally stored JSON array, but I am encountering an issue where the

Hello, I'm having some difficulties with JSON as a beginner. I've tried searching extensively but haven't found a solution that works for me. My issue arises when attempting to save data using local storage in JSON format - the array prints ...

Duplicate user scrolling input within a specified div container

I am attempting to recreate a horizontal scrolling effect on a div element that mirrors the input scroll. When the user scrolls along the input, I want the div element to scroll in sync. The issue I am encountering is specific to Chrome, where the input b ...

The implementation of Typescript in Express does not rely on Middleware

I've encountered an issue with my Auth Middleware - it seems that the middleware isn't being called at all. Even when I intentionally throw an Error within the middleware function, nothing is printed out. For testing purposes, I only need to inv ...

Ways to eliminate the lower boundary of Input text

Currently, I am working on a project using Angular2 with Materialize. I have customized the style for the text input, but I am facing an issue where I can't remove the bottom line when the user selects the input field. You can view the red line in t ...

Service in Angular2+ that broadcasts notifications to multiple components and aggregates results for evaluation

My objective is to develop a service that, when invoked, triggers an event and waits for subscribers to return data. Once all subscribers have responded to the event, the component that initiated the service call can proceed with their feedback. I explore ...

How to retrieve data from a form object sent via cloud function using App Script

Currently, I am in the process of developing a web application that can extract files from a web form and store them in a designated Google Drive folder. After the user submits the form, my goal is to trigger a cloud function using google.script.run while ...

Unable to activate IndexedDb persistence with Firebase v9 in a Next.js PWA

I'm having trouble enabling IndexedDb persistence in Firebase v9 for a Next.js PWA. These errors keep popping up: index.js // main Firebase file import { initializeApp } from 'firebase/app' import { getAuth } from 'firebase/auth' ...

Using jQuery to target the element before

Is there a way to determine the width of elements located before an element when it is hovered over? I attempted to achieve this using the following code: $('ul li').hover(function() { $(this).prevAll().each(function() { var margin = $(this ...

Enhance your website's performance by optimizing Javascript page loading time when using

I've implemented a simple JavaScript function that calculates the loading time of a URL: var beforeLoad = (new Date()).getTime(); $('#myiframe').one('load', function() { var afterLoad = (new Date()).getTime(); var result = ...

The Alchemy feature on hover is not functioning

I am currently using alchemy.js to display a graph, but I am encountering issues with showing the "onMouseOver" caption of the graph's node. The console is displaying some errors which you can see here. Here is the code snippet: <html> < ...

Implementing a color change for icons in React upon onClick event

export default function Post({post}) { const [like,setLike] = useState(post.like) const [islike,setIslike] = useState(false) const handler=()=>{ setLike(islike? like-1:like+1 ) setIslike(!islike) } return ( <> <div classNam ...

Tips on effectively rendering child components conditionally in React

My components currently consist of an AddBookPanel containing the AddBookForm. I am looking to implement a feature where the form is displayed upon clicking the 'AddBookButton', and hidden when the 'x' button (image within AddBookForm c ...

Issue with Vue and Inertia.js: database value not being updated

Check out the form I have created: <form @change="updatePipeline"> <select v-model="pipeline.input_mode"> <option value="text">Text</option> <option value="html">HTML</opt ...

Appears as though time is slipping away during date conversions

I seem to be experiencing a strange issue where I lose a day when transitioning between MySQL and my JavaScript code, and I can't seem to figure out why. When I insert a date into the database (for example, 10/14/12), it appears as 10/13/12 in the dat ...

function called with an undefined response from ajax request in React

Hello, why am I getting the response "callback is not defined"? Component 1: console.log(getUserGps()); Component 2: import $ from 'jquery' export const getUserGps = () => { $.ajax({ url: "https://geolocation-db.com/jsonp", ...

Utilizing Vuetify's card component to create responsive card layouts similar to Bootstrap

Is there a way to display a list of v-card components from Vuetify similar to the layout in Bootstrap Card Columns? Potential solution: I attempted to implement this, but unfortunately I was unable to create a working Plunker for the code. Here is my ap ...

Developing a Laravel 5.3 application with a focus on API integration

I am interested in creating an API using Laravel and a Single Page Application (SPA) with Vue.js. However, I'm unsure about the best approach to achieve this. Should I create two separate projects: one for the Laravel API and another for the Vue.js S ...

React-scripts testing now displays error messages without a traceback

After recreating the browser version of the TacticToy game using React, I encountered an issue while writing unit tests. The problem is that there is no complete traceback provided for a custom exception, with only the test function being highlighted: htt ...

Unable to generate onsen-ui popover

My expertise lies in utilizing the Monaca platform for developing mobile applications using Onsen UI and AngularJS. I am looking to incorporate a popover feature from Onsen into my app, and have tried the following code snippet: angular.module('app& ...

Arranging Angular Cards alphabetically by First Name and Last Name

I am working with a set of 6 cards that contain basic user information such as first name, last name, and email. On the Users Details Page, I need to implement a dropdown menu with two sorting options: one for sorting by first name and another for sorting ...