Bootstrap 5 Alert Auto Close

Bootstrap 5 Alert Auto Close
Code Snippet:Auto Close Bootstrap 5 Alerts
Author: Asif Mughal
Published: January 11, 2024
Last Updated: January 22, 2024
Downloads: 6,769
License: MIT
Edit Code online: View on CodePen
Read More

Alert boxes are useful for providing important messages or notifications to users that are time-sensitive or temporary. Instead of requiring users to manually dismiss the alert, the auto-close feature automatically hides the alert after a specified duration. With the help of Bootstrap, you can display any kind of alert notifications but users need to dismiss them manually.

This lightweight code snippet allows you to automatically hide a Bootstrap alert with a fading effect. You just need to add the class ‘auto-close’ to the Bootstrap alert component, and it will disappear smoothly. You can set the closing time according to your needs.

How to Create Bootstrap 5 Alert with Auto Close Feature

1. First of all, load the Bootstrap 5 CSS by adding the following CDN link into the <head> tag of your website.

<!-- Bootstrap 5 CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">

2. Create a Bootstrap alert component, add your content inside it, and add an additional class “auto-close” to it.

<div class="auto-close alert alert-success" role="alert">
  <h4 class="alert-heading">Well done!</h4>
  <p>Aww yeah, you successfully read this important alert message. This example text is going to run a bit longer so that you can see how spacing within an alert works with this kind of content.</p>
  <hr>
  <p class="mb-0">Whenever you need to, be sure to use margin utilities to keep things nice and tidy.</p>
</div>

3. If you need to apply additional styling, use the following CSS styles:

.auto-close{
   /* Your custom CSS goes here.. */
}

4. Now, load the Bootstrap 5 JS by adding the following CDN link just before closing the <body> element.

<!-- Bootstrap 5 JS -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4" crossorigin="anonymous"></script>

5. Finally, use the following JavaScript function to automatically close the alert message with fading animation. You can set the custom time duration in setTimeOut function after that the alert message should be closed.

// Get all elements with class "auto-close"
const autoCloseElements = document.querySelectorAll(".auto-close");

// Define a function to handle the fading and sliding animation
function fadeAndSlide(element) {
  const fadeDuration = 500;
  const slideDuration = 500;
  
  // Step 1: Fade out the element
  let opacity = 1;
  const fadeInterval = setInterval(function () {
    if (opacity > 0) {
      opacity -= 0.1;
      element.style.opacity = opacity;
    } else {
      clearInterval(fadeInterval);
      // Step 2: Slide up the element
      let height = element.offsetHeight;
      const slideInterval = setInterval(function () {
        if (height > 0) {
          height -= 10;
          element.style.height = height + "px";
        } else {
          clearInterval(slideInterval);
          // Step 3: Remove the element from the DOM
          element.parentNode.removeChild(element);
        }
      }, slideDuration / 10);
    }
  }, fadeDuration / 10);
}

// Set a timeout to execute the animation after 5000 milliseconds (5 seconds)
setTimeout(function () {
  autoCloseElements.forEach(function (element) {
    fadeAndSlide(element);
  });
}, 5000);

That’s it! You’ve now successfully implemented Bootstrap 5 Alert Auto Close. Feel free to comment below if you have any questions or suggestions.

1 thought on “Bootstrap 5 Alert Auto Close”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X