Having an issue with Vue2's v-if affecting the functionality of my transition

I'm currently facing an issue with a v-if directive on an element that is supposed to act as a lightbox for displaying a shopping cart.

Upon clicking the "View Cart" button in my navbar, the state of the lightbox should change and activate the v-if statement.

While everything functions correctly, I also attempted to toggle an additional class called "visible" on the lightbox element to implement a fade-in/out effect using transitions on opacity.

However, the problem I'm encountering is that the lightbox cart appears instantly due to the v-if. Is there a way to incorporate my transition alongside the v-if?

Here's the code snippet:

<template>
    <div class="full-page">
        <navbar @openCart="toggleCart"/>
        <product />
        <div class="lightbox"
            v-if="lightboxCart"
            :class="{visible: lightboxCart}">
            <cart
                @closeCart="toggleCart"/>
        </div>
    </div>
</template>

<script>
export default {
    head() {
        return {
            title: 'Totonou',
            meta: [
                {
                    hid: 'description',
                name: 'description',
                content: 'Customizable Jewelry Organizer'
                }
            ]
        }
    },
    data() {
        return {
            lightboxCart: false
        }
    },
    components: {
        'navbar' : require('@/components/AppHeader.vue').default,
        'product' : require('@/components/ProductPage/Product.vue').default,
        'cart' : require('@/components/CartPage/Cart.vue').default,
    },
    methods: {
        toggleCart() {
            this.lightboxCart = !this.lightboxCart
        }
    }
}
</script>

<style scoped>
    .fullpage {
        position: relative;
    }
    .lightbox {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        background-color: rgba(0,0,0,0.5);
        width: 100%;
        height: 100%;
        opacity: 0;
        transition: 0.8s all cubic-bezier(0.39, 0.575, 0.565, 1);
    }
    .lightbox.visible {
        opacity: 1;
    }

</style>

Answer №1

Implementing transitions in Vue.js is made easy with the use of Vue Transitions:

new Vue({
  el: "#demo",
  data() {
    return {
      lightboxCart: false
    }
  },
  methods: {
    toggleCart() {
      this.lightboxCart = !this.lightboxCart
    }
  }
})
.fullpage {
    position: relative;
}
.lightbox {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: rgba(0,0,0,0.5);
    width: 100%;
    height: 100%;
    /* opacity: 0; */
    transition: 1.8s all cubic-bezier(0.39, 0.575, 0.565, 1);
}
.lightbox.visible {
  /* opacity: 1; */
}
.fade-enter-active, .fade-leave-active {
  transition: opacity 1.5s;
}
.fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
  opacity: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
  <div class="full-page">
    <button @click="toggleCart">show</button>
    <transition name="fade">
      <div class="lightbox"
          v-if="lightboxCart"
          :class="{visible: lightboxCart}">
      </div>
    </transition>
  </div>
</div>

Answer №2

Css transitions do not function properly with v-if because it completely removes the element from the dom instead of just changing its visibility. To achieve this effect, you can utilize Vue transitions. Below is an example code snippet:

<transition name="fade">
  <div
    class="lightbox"
    v-if="lightboxCart"
    :class="{visible: lightboxCart}"
  >
     <cart @closeCart="toggleCart"/>
  </div>
</transition>

Additionally, make sure to include the necessary css classes for this transition:

.fade-enter-active, .fade-leave-active {
  transition: opacity .5s;
}
.fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
  opacity: 0;
}

In the provided code snippet, the fade transition is used, but you have the flexibility to choose other transitions or create your own. For more information on Vue transitions, visit the following link:

Vue Transitions

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

The art of connecting a web3 provider to a Vue.js DApp: A step-by-step guide

I am looking to interact with the Rootstock blockchain using a vue.js DApp in order to track wallet balance and send RBTC. ​ My goal is to set up a connection with Metamask and utilize ethers.js web3 provider to engage with the Rootstock network. ​ To ...

Exploring the potential of overflow within a container with a percentage-based

I am attempting to design a webpage that fills the entire screen height without scroll bars, yet contains a scrollable container within it. Despite trying various approaches with percentage heights, I have not been successful as the percentages do not beha ...

I'm having trouble pulling images from Firebase into my Vue application

Currently, I am experimenting with a Vue application that requires users to upload an image to Firebase. After the image is successfully uploaded, it should be displayed within an img tag in the template. The issue I am encountering is retrieving the uplo ...

What is the best way to ensure that all inner divs occupy the full height of their parent div?

I am seeking a solution to make all child divs fill the full height of the parent div without specifying a fixed height. Ideally, I would like them to adjust to the height of the tallest child div. However, if the only way to achieve this is through JS, I ...

Is it possible to access the $data variable from a Mixin.js file in Store.js using Vue.js?

My Mixin file has the following structure: export default { data: function() { return { analysisTime: "nothing", phantomPrefix: "One more", } }, methods: { isGeneric: function( ...

What is the process for modifying the attributes of a currency text box in a Dojo within XPages?

I have come across several discussions aimed at addressing this issue, but none of them have provided a clear and definitive solution that I can easily follow. Is there a straightforward method to modify the css of a dojo field within XPages? Specifically ...

Leveraging .col-* within a .d-flex container in Bootstrap 4

What is the recommended practice for using .col-* as children in a .d-flex container? Should .d-flex be used with .row instead? I have noticed that the Bootstrap 4 Docs do not show any examples of using a .col-* in a .d-flex container. The layouts provide ...

Jquery vertical tabs - right-aligned tab panels

Creating vertical tabs using jQuery is a skill I have mastered. However, what I am struggling with is aligning the tabs vertically and to the right side of the page. My knowledge of jQuery is limited to basic effects and I could use some guidance or soluti ...

Can the default DOM structure of a Jquery Data Table be customized to fit specific needs?

I have incorporated a Jquery Data Table Plugin that can be found at http://datatables.net/api. This plugin comes with its own search box, which is automatically added to the page when enabled. The search box is nested within its own div structure like this ...

Why won't the click event work in Vue when using v-if or v-show?

Attempting to trigger a click event from a div, but if v-if false is present during component rendering, the click event does not work. Here's the code snippet: export default { name: "ProSelect", data() { return { isActive: false ...

Is there a way I can inform iPhone users on my webpage about the existence of an app?

Hey there! I've managed to figure out how to detect the platform being used. It's working well so far: // Extract User-Agent String var UserAgent = navigator.userAgent.toLowerCase(); // Check User-Agent for certain keywords if (UserAgent.search ...

What is the method for calculating table cell widths in HTML?

Take a look at this code snippet here: http://jsfiddle.net/R3AKT/1/. <table class="main"> <colgroup><col class="votes"><col></colgroup> <tr> <td>Votes</td> <td>Comments</td> </tr ...

Having trouble getting Vue 3 integration to work properly in Laravel 9 tutorial

Having previously worked on projects using Laravel, I decided to try integrating Vue.js for the front end of my latest project. In search of tutorials on how to seamlessly blend Vue.js with Laravel, I experimented with multiple guides. However, I will focu ...

When the page is resized, the Font-Awesome icons shift position

/* icon sect*/ .social { margin-top: px; padding: 0; position: absolute; top: 60%; left: 50%; transform: translate(-50%,-50%); } .social li { list-style: none; float: left; margin: 0px; width: 60px; height: 10px; line-height: 60px; ...

What is the best way to showcase a list of divs in three columns rows using a forEach loop?

I need help troubleshooting my code. I'm trying to display an array called posts:[], which consists of a JSON object. Everything seems to be working fine except for the fact that I can't get three columns to display in a row using Bootstrap. I ha ...

Tips for adjusting the font size according to the varying number of characters entered

I am currently facing a challenge with adjusting the font size based on the dynamic number of characters. For example, I have a set of characters with a font-size of 40px, and as more characters are added, the font size should decrease to fit within the av ...

When working with TextareaAutosize component in MUI, an issue surfaces where you need to click on the textarea again after entering each character

One issue that arises when using TextareaAutosize from MUI is the need to click on the textarea again after entering each character. This problem specifically occurs when utilizing StyledTextarea = styled(TextareaAutosize) The initial code snippet accompl ...

The cookie isn't being established in the browser through the backend, rather it is included in the request itself

I'm encountering an issue where the cookie does not attach to the browser after a successful request. Despite showing up successfully in the response, I have tried multiple solutions and configurations without success. The most promising solutions for ...

Image floating gracefully in the top corner of the screen

Check out this Picture I am trying to create a floating image similar to the one in the top left corner of the navigation bar. After searching for some code to achieve this, I came across the following: <html> <head></head> <bod ...

When the anchor tag is clicked, I'm displaying the DIV for Customer Testimonials, however, the expansion of its height is not

One way to display customer testimonials is by using flexslider to create a horizontal scroll. The testimonials are hidden and only shown when the "view all testimonials" link is clicked, which can be seen in this JSFiddle example: Working : Demo JQuery ...