Vuetify Alert fade after a duration

Vuetify Alert fade after a duration

Vuetify is a popular Material Design component framework for Vue.js. In this post, we will explore how to create alerts in Vuetify that automatically fade away after a specified duration. Alerts are a common UI element used to convey important information or messages to the user. Adding a fade-out effect after a certain time can enhance user experience and provide a non-intrusive way to deliver messages.

Profile Photo

Abhishek Yadav (SD) SDE of FAI

Feedback

written
Profile Photo

Anil Kumar (Expert) Coding Expert of FAI

Feeback

reviewed
Profile Photo
Oct 30, 2023 03:10 AM Last Updated
updated

Copied to Clipboard
Run

Output of Vuetify Alert Fade After A Duration 

Pasted Image

Creating the Alert Component

In our example, we'll create a Vuetify alert component that can be triggered to show a message and automatically fade out after a specified duration.

Copied to Clipboard

In this code, we have a button that triggers the alert and a v-alert component that displays the message. The v-if directive is used to conditionally display the alert based on the visible data property.

Adding the JavaScript Logic

Next, we'll add the JavaScript logic to control the alert's visibility and fade-out behavior. Here's the script section of our code:

Copied to Clipboard

In this code, we define a data object to hold the visible and message properties. The showAlert method is called when the "Show Alert" button is clicked. It sets the message and makes the alert visible. The setTimeout function is used to hide the alert after 5 seconds (5000 milliseconds)

 

Conclusion

With the provided code, you can create Vuetify alerts that automatically fade away after a specified duration. This is a useful feature for notifying users of events or messages without requiring them to manually dismiss the alert. You can customize the alert's appearance and behavior further based on your project's requirements

Ad