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

Navigating a double entry validation in a Java script prompt while utilizing Selenium

Can someone please assist me with the following scenario: I need to complete a double entry check prompt/alert that contains two text boxes. The task is to fill in these two text boxes and then click on the OK button. Potential solutions attempted: I tr ...

The communication hub in a Vue.js application

I'm currently developing a Vue single-page project and I have implemented an empty Vue instance as a central event bus. However, I've encountered an issue when trying to fire an event. eventbus.js import vue from 'Vue' export default ...

Getting a vnode from a DOM element in Vue 3.0: A Step-by-Step Guide

My question pertains to obtaining a vnode through accessing the DOM using document.getElementById(id). How can I accomplish this? ...

Modifying the CSS class of an element does not produce the desired effect after altering its styles using JavaScript

html: <input id="myinput" class="cinput" type="image" src="http://www.foodwater.org.au/images/triple-spiral-3-small-button.jpg"/> <br><br><br><br> <button id="s">set to visible class</button> <button id="h"> ...

Navigate through JSON data to uncover the tree structure path

In my project, I am working on creating a treeview user interface using the JSON provided below. I have included properties such as node-id and parentId to keep track of the current expanded structure. Next, I am considering adding a breadcrumb UI compone ...

The value of this.$refs.<refField> in Vue.js with TypeScript is not defined

During the process of converting my VueJs project to TypeScript, I encountered an error related to TypeScript. This issue arises from a component with a custom v-model implementation. In the HTML, there is an input field with a 'plate' ref that ...

A guide to merging two JSON objects into a single array

Contains two different JSON files - one regarding the English Premier League stats for 2015-16 season and the other for 2016-17. Here is a snippet of the data from each file: { "name": "English Premier League 2015/16", "rounds": [ { "name": ...

Trouble with Vue images not displaying

I'm encountering an issue with displaying images in my Vue CLI project. Here's the situation: this particular Vue file is accessing a JSON file that contains references to individual Eyewear objects, which is all functioning correctly. The JSON ...

Retrieve the earliest and latest dates from a JSON file to utilize with FlatPicker

I have a file in an unknown format, possibly JSON, with dates listed. I am attempting to extract the minimum and maximum dates from this data in MM/DD/YYYY format for use as variables in Flatpicker's minDate and maxDate options. The current AJAX call ...

Can you please explain the process of retrieving the value of an item from a drop-down menu using JavaScript?

I am currently developing a basic tax calculator that requires retrieving the value of an element from a drop-down menu (specifically, the chosen state) and then adding the income tax rate for that state to a variable for future calculations. Below is the ...

Changes to the state will not be reflected until a poll is conducted

Here we have an example of state being initialized and passed down to a child component: const [rowCount, setRowCount] = React.useState<number>(1); <Foo setRowCount={setRowCount} /> Foo: const Foo = (props) => { const { setRowCount } ...

Using Leaflet to beautify categorical json information

As a beginner in coding, I must apologize if a similar question has already been asked. I've spent days searching but haven't found the right combination of terms to find examples for my scenario. I am exploring various small use cases of differ ...

What is the best way to apply a specific style based on the book ID or card ID when a click event occurs on a specific card in vue.js

My latest project involves creating a page that displays a variety of books, with the data being fetched from a backend API and presented as cards. Each book card features two button sections: the first section includes "ADD TO BAG" and "WISHLIST" buttons ...

What is the best way to implement dynamic generation of Form::date() in Laravel 8?

Is there a way to dynamically generate the Form::date() based on the selection of 1? If 1 is selected, then display the Form::date() under the Form::select(). However, if 0 is selected, then hide the Form::date() in this particular view. For example: Sel ...

Why is this regular expression failing to match German words?

I am attempting to separate the words in the following sentence and enclose them in individual span elements. <p class="german_p big">Das ist ein schönes Armband</p> I came across this: How to get a word under cursor using JavaScript? $(&ap ...

Maximizing the worth of itemtype using jQuery: A guide

My goal is to include an 'itemtype' attribute in the body tag, body.page-body.ng-scope.device-lg(itemscope='', itemtype='') I attempted the following method $('body').add(itemtype='t1'); Unfortunately, ...

Error 404: This page seems to have gone missing. Django and react-router

Struggling to integrate reactjs and react-router (1.x) with my Django project. I'm finding it challenging to make everything work together seamlessly. For more details, you can check out the project on GitHub: https://github.com/liondancer/django-che ...

Changing the information entered into a form within a subcomponent will result in all other fields being reset

I encountered an issue with my application that involves a form contained within a child component. The parent passes a prop (wizardData) to the child component. If the form is being used to input new data, the field values in the prop will be null; but if ...

Does JavaScript adhere to a particular pattern?

Is there a more streamlined approach to implementing this particular design pattern? function a() { function x() { /* code */ } function y() { /* code */ } /* additional code */ x(); y(); // can be called from here } a(); a.x(); a.y(); I ...

Significant delay in processing second jQuery Ajax call

Attempting a simple Ajax call with page content refresh using the following code snippet: $("a.ajaxify-watched").bind("click", function(event) { $item = $(this); $.ajax({ url: $(this).attr("href"), global: false, type: "G ...