Using Vue.js to conditionally render content based on changes in a variable

I am facing a challenge in rendering a new element once the boolean variable waiting changes to true. The issue arises when transitioning from one v-if statement to another, as the boolean does not update until the first statement is completed. How can I smoothly transition to the second v-if condition after the first one is fulfilled without placing the code directly below due to input dependency?

Considering redirecting to a different page as a solution, but I prefer keeping all variables consolidated in one place.

<template>
  <transition name="fade">
    <div class="home" v-if="!secondplayer">
      <div class="inner">
        <img class="responsive" src="/static/mask.png" alt="Responsive image" />
        <h1>How well do you understand the lyrics in Hamilton?</h1>
        <br />
        <p>Invite a second player by sending them this link: <u>{{url}}</u>.</p>
      </div>
    </div>
    <div class="play" v-if="secondplayer">
      <div v-if="!waiting">
        <div class="container hamilton--header--text">
          <h1><i>Test your understanding of Hamilton lyrics</i></h1>

          <div class="columns hamilton--inner">
            <div class="column is-half left">
              <p class="title">Player 1</p>
              <p class="subtitle">Score: {{playerdata.one.score}}</p>
            </div>
            <div v-if="secondplayer" class="column is-half right">
              <p class="title">Player 2</p>
              <p class="subtitle">Score: {{playerdata.two.score}}</p>
            </div>
          </div>

          <div class="hamilton--lyrics--text">
            <p>{{question.lyric}}
            </p>
            <div class="hamilton--answers">
              <a v-bind:class="{ 'wronganswer': hasAnswered && !item.correct, 'correctanswer': hasAnswered && item.correct}" @click="checkAnswer(item)" v-for="(item, index) in options">{{item.name}}</a>
            </div>
          </div>
        </div>
      </div>
      <div v-if="waiting">
        <div class="container">
          <h1>Waiting for the other player to finish...</h1>
          <img src="/static/Spinner.gif">
        </div>
      </div>
    </div> 
    <div class="gameOver" v-if="winMessage">
      <div>
        <div class="container">
          <h1>{{winMessage}}</h1>
          <vue-simple-spinner size="big" message="Loading..."></vue-simple-spinner>
        </div>
      </div>
    </div>
  </transition>
</template>

Answer №1

To achieve the desired effect, you must incorporate the transition css:

For example:

.fade-enter-active { /* <<transition name>>-<<transition class>> */
  transition: all .3s ease;
}
.fade-leave-active {
  transition: all .8s cubic-bezier(1.0, 0.5, 0.8, 1.0);
}

Explore transition classes for more in-depth information.

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

Crafting redirect rules in React that avoid redirecting to the same route

In my project, there is a file named AuthenticatedRoute.tsx, which serves as a template for all the protected/authenticated routes in my Router.tsx file. export default ({ component: C, authUser: A, path: P, exact: E }: { component, authUser, path, ex ...

Steps to automatically logout the user when the cookie expires in Nuxt

I have successfully implemented login functionality using the official NuxtJS Auth Routes example in my project with express-sessions. This is the content of my app.js file: const express = require('express') const session = require('expre ...

How to replace text using jQuery without removing HTML tags

One of the functions in my code allows me to replace text based on an ID. Fortunately, this function is already set up and working smoothly. $('#qs_policy .questionTitle').text("This text has been updated"); However, there is another ...

One way to change the cursor CSS property is by dynamically altering it based on scrolling behavior. This involves modifying the cursor property when scrolling

I'm attempting to adjust the cursor property within an Angular function. The issue I'm facing is that when I begin scrolling the webpage, the cursor changes to a pointer, but when I stop scrolling, it remains as a pointer. I've attempted to ...

Mongoose is struggling to locate the expected result

I currently have three different schemas set up. User.js: const mongoose = require("mongoose"); const bcrypt = require("bcryptjs"); const userSchema = new mongoose.Schema({ name: { type: String, required: true, }, email: { type: String, ...

Integrating external JavaScript libraries into Ionic using the correct process

For the last few months, I have been utilizing jQuery Mobile for a hybrid app. Now, I am interested in exploring Ionic and Angular.js, so I am attempting to reconstruct it. My current JQM application relies on xml2json.js, but I do not have any experience ...

What is the method for shifting content as the window is resized to ensure it remains in its original position?

My page features a grid with three div elements. Each div is the size of the viewport, resulting in only one div being visible at a time, while the other two remain outside the view. This makes the grid three times larger than the viewport. When resizing ...

Modify the bootstrap form dynamically in response to the user's input. Update the form layout instantly as the user types, with no need for clicking any buttons

Imagine a scenario where, as soon as you enter your credit card number, the form automatically undergoes a change without requiring a button click, similar to how a credit card logo pops up. The form detects changes instantly after the input field has be ...

Running into the error message 'TypeError: Cannot invoke a class as a function' while utilizing Ziggy for Vue within Laravel

I have been searching endlessly for a solution to my issue without any luck. Here are the steps I have taken: \\Webpack.mix.js const mix = require('laravel-mix'); const path = require('path'); mix.webpackConfig({ resolve: ...

"Selecting an element through Drag/Drop will result in the element being

INQUIRY I've encountered an issue with a draggable element ($("#draggable")) that interacts with a checkbox ($('#checkable')). When the $('#draggable') is dragged onto a box ($('#droppable')), it checks $(&apos ...

Create a function called "insertEvent" that will insert a new event on a specified date

Below is the data structure for storing affairs according to years, months, and days: let affairs = { 2018: { 11: { 29: ['task111', 'task112', 'task113'], 30: ['task121', 'ta ...

Fluid Grid design showcasing a gap on the right side

After working on the Skeleton Framework and making adjustments to the grid design, I am facing a small gap issue on the right side. Despite things slowly falling into place, this minor gap is causing confusion. Please refer to the image below for clarifica ...

Hold on for the completion of Angular's DOM update

I am facing an issue where I need to connect the bootstrap datepicker to an input field generated by AngularJS. The typical approach of using jQuery to attach the datepicker doesn't work as expected because the element is not yet available: $(functi ...

Is there a way to keep Angular from automatically re-sorting my list when I make edits to it?

I have designed a small application with Angular for managing Todolists. Each list contains multiple todos, where each todo has attributes such as name, value1, and value2. To automatically sort each list using Angular, I utilized ng-repeat="todo in selec ...

How can I make an AJAX request in Rails 3 to retrieve an HTML page?

I am experimenting with using Rails to display an .html.erb template via an ajax request under specific conditions. By default, I am consistently receiving the .js.erb file when making an ajax request. Without delving into the reasons behind this choice, ...

Utilize Javascript to interpret a structure that resembles JSON

My data resembles JSON, with columns and names listed: columns: [ { allowNull: false, autoEnterSubType: 0, autoEnterType: 2, creationOrderIndex: 0, dataType: 4, databaseSequenceName: "seq_admintraties_adminratie_id", flag ...

Changing the hover color of an Angular Material MD Button

<div class="non-active" layout layout-align='space-around center'> <md-button ng-click="$ctrl.widget.type = 'chart'; $ctrl.validateForm()" layout='column' ng-class="{selIcon : $ctrl.widget.type == ' ...

What is the best way to incorporate React hooks into a for loop?

I am looking to dynamically append a container with a specified number of div elements based on the data in a JSON file. I attempted to include the logic within a for loop, but encountered errors in the process. If you're interested in checking out t ...

What is the process for comparing two objects in TypeScript?

There is a unique class named tax. export class tax { private _id: string; private _name: string; private _percentage: number; constructor(id: string = "", taxName: string = "", percentage: number = 0) { thi ...

Looking to learn how to replicate data effortlessly with either javascript or jquery?

I am currently trying to figure out how close I am to successfully cloning this div. Honestly, I am a bit lost at this point and could really use some assistance. Should I consider using jQuery for this task? <div id="question20">20. Details of Chi ...