How to make an element vanish in Vue.js after a transition

My webpage features a notification system created with vue.js. Everything is functioning properly, but I am looking to remove the element once the transition has completed. Currently, I am achieving this using a setTimeout function, which is not the most efficient method.

If you'd like to see a working example, please visit this jsfiddle: http://jsfiddle.net/yMv7y/1073/

Here is the code I am using:

Vue:

Vue.transition('notification', {
    enter: function(el) {
        app.notifications.pop();
    },
    leave: function(el) {
        setTimeout(function(){
            el.remove();
        },5000);
    },
});

Vue.component('notification', {
    template: '<div class="notification" v-class="red: !success, green: success" v-transition="notification"><p>{{message}}</p></div>',
    data: function() {
        return {
            success: true,
            message: '',
        };
    },
});

var app = new Vue({
    el: 'body',
    data: {
        notifications : [
        ]
    },
    ready: function() {
        self = this;
        var data = {
            'message': 'Thank you',
            'success': true
        };
        self.notifications.push(data);
    },
});

Html:

<div id="notifications-wrapper">
    <notification id="notification"
            v-repeat="notifications"
            >
    </notification>
</div>

CSS #notifications-wrapper { position: fixed; z-index: 99; top: 0; left: 80%;

overflow: visible;
}

.notification
{
position: relative;
z-index: 100;

overflow: hidden;

width: 250px;
margin-top: 20px;
transform: translate(20px, 0);
animation-fill-mode: forwards;
transition: 1s;
-webkit-backdrop-filter: blur(2px);
        backdrop-filter: blur(2px);
background-color: grey;
}

p 
{
margin: 10px 20px;

color: $white;
}



.notification-transition
{
animation-delay: 0s, 4.5s;
animation-duration: 4.5s, 0.5s;
animation-name: slide-in-out, hide-notification;
}


@keyframes slide-in-out
{
0%
{
    transform: translate(20px, 0);
}
10%
{
    transform: translate(-270px, 0);
}
90%
{
    transform: translate(-270px, 0);
    opacity: 1;
}
100%
{
    transform: translate(20px, 0);
    opacity: .5;
}
}

@keyframes hide-notification {
1% {
  height: auto;
  margin-top: 20px;
}
100% {
    height: 0;
    margin: 0;
}
}

Answer №1

Issue: Removing el within the transition (leave function) resulted in an error without using setTimeout.

Resolution: Utilize the afterLeave function instead. Modify leave function to afterLeave function.

afterLeave: function(el) {

        el.remove();
}

Jsfiddle

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

What is the reason behind Intellij Code Inspection indicating that a selector is not being used?

I have a small project consisting of two files located in the same folder. App.css .App-align-left { text-align: left; } App.js import React from 'react'; import 'App.css'; class App extends React.Component { constructor(p ...

Queries with MongoDB RegEx fail to return any matches if the search string contains parentheses

When trying to implement case-insensitivity using regex, it seems to work well for plain strings. However, if special characters like parenthesis are involved in the search query for the name, the database returns no results. For example, a search for "Pu ...

Using a child prop in React's map function

Initializing the parent component: class Parent extends React.Component { constructor(props) { super(props); this.state = { children: [ {name: "Julien Lennon"}, {name: "Sean Lennon"} ] } } render() { retur ...

Make sure that the iframe loads the next page with enough force to break out

My dilemma involves an iframe that loads the new tab page. When a user clicks on the thumbnail, it opens within the iframe. My goal is to have any subsequent load in the iframe redirected to window.top. Is there a way to achieve this without manually setti ...

Using angular.js variables in the script tag: A step-by-step guide

Currently, I am working on a project that involves displaying elements on a view using Angular.js and attempting to connect these DOM elements using jQuery connections. While the elements are being displayed correctly, I am encountering an issue when tryin ...

Utilizing internal PDF links in a Microsoft UWP application

In the process of developing a UWP app using javascript, I have created a list of links that are connected to PDF files stored locally in the app. The ultimate goal is to offer a collection of hands-free documentation for the Hololens (Windows AR) device. ...

Utilizing a preset canvas for the renderer can lead to issues

As I dive into tutorials on three.js, I encounter a challenge when using my predefined canvas elements for the renderer. Everything works smoothly if I let three.js create the renderer DOM element. However, when I attempt to retrieve the canvas using getEl ...

What are the different ways to interact with the object retrieved from the onloadedmetadata event

Having trouble accessing a specific value in an object that is the result of the onloadedmetadata event. When I log the entire object using audioDuration, I am able to see and retrieve the respective value without any issues. However, when I try to access ...

Hide the top rectangle in vue-chartjs that displays the datasets label

view chart rendering chart appearance inquiry After numerous attempts, I still can't seem to get rid of the persistent red rectangle above my chart. Is there a solution to remove it? It seems that the usual methods for adjusting options and legends, ...

Using GeoJson in Leaflet for map display

As I develop an Angular application with Leaflet, my goal is to showcase numerous municipalities within my country: https://i.sstatic.net/9cuSW.png In order to enhance navigation efficiency, I decided to divide my JSON file into multiple files and store ...

Tips for automatically filling out a div class form:

I currently have an Autoresponder email form featured on my webpage. Here is a snippet of the code for the section where customers enter their email: <div id = "af-form-45" class = "af-form" > <div id = "af-body-45" class = "af-body af-standa ...

The URL for the Javascript chunk includes colons at https://example.com/js/chunk-vendors.b3792e11.js:18:16400

I recently completed a Vue application and used npm run build to generate the files. Upon uploading the dist folder to my Apache server, I encountered an issue where Apache was unable to locate the file when trying to access a specific part of the app (:18 ...

Challenge: CSS div challenge - making one div's height the same as another div and aligning it to the bottom of its containing div without using absolute

Constructing the required layout has proven to be much more difficult than anticipated. Even with a diagram illustrating how it should look, aligning div D to the bottom of div A without using absolute positioning is causing some trouble (as it messes up t ...

Having successfully configured and published Google Tag Manager, unfortunately, I am encountering difficulties in displaying the tag on the website

After setting up my GTM account and creating containers, tags, etc., I encountered an issue. Even after publishing my container and creating a version, when I checked my website, all the code was hidden within a div tag with display none and visibility hid ...

Integrating Vue.js into an ASP.NET MVC 5 Application

Looking to integrate Vue.js into an MVC 5 project with webpack transpiling capabilities? Check out these two resources I used to combine them into one project: MVC 5 ES6 Project Core 2 Vue.js Project https://github.com/MarkPieszak/aspnetcore-Vue-starte ...

Achieving unique proportions with HTML5 video resizing

On my page, I plan to showcase a video specifically for desktop browsers. I have devised some code to determine whether the video is in landscape or portrait orientation. The challenge lies in deciding what to do based on this orientation: If the video is ...

Enhance user experience by implementing a feature in AngularJS that highlights anchor

As I am in the process of developing a chat application using Angular, I have encountered an issue with switching between views. One view, named 'chat.html', displays the list of available users while another view, 'chatMessages.html', ...

Obtaining data from a Nested Json file in Angular 5

Encountering difficulties retrieving data from nested JSON. Error: 1. <h3>{{sampledata}}</h3> displaying [object Object] 2. <p>{{sampleDataModel.Title}}</p> ERROR TypeError: Cannot read property 'Title' of undefined ...

Detecting a Checkbox Click Event in JavaScript

I am working with a table that has the following structure: <tbody id="ggwp"> <tr class="r123 disable-it"> <td> <input type="checkbox" name="item" class="row" statusid="1234"> </td> </ ...

Tips on retrieving the image URL from Aws s3 and saving it in mongodb

Can anyone help me with retrieving the URL location of an image from AWS s3 and storing it in MongoDB using mongoose? Currently, when I try to console log the req.file.location value, it shows up as undefined. I have also attempted to console log uploadFil ...