Notifications

In this example below you will see how to do a Notifications with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by Cory2911, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright Cory2911 ©

Technologies

  • HTML
  • CSS
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>Notifications</title>
  
  
  
      <link rel="stylesheet" href="css/style.css">

  
</head>

<body>

  <div class="container"></div>
  <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js'></script>

  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/Cory2911/notifications-jzNxdQ */
.container {
  position: fixed;
  top: 25;
  right: 0;
  height: 80%;
  width: 15%;
  border: 2px solid #777;
}

.gts-notification {
  position: relative;
  margin-top: 10px;
  transition: all 0.6s;
}

.gts-success-notification{
  width: 100%;
  color: #000;
  padding: 10px;
  background: linear-gradient(to bottom, #70FF70, #44DD44);
}

.gts-error-notification{
  width: 100%;
  color: #000;
  padding: 10px;
  background: linear-gradient(to bottom, #FFBBBB, #DD7777);
  transition: all 0.35s;
}

.close-notification-btn {
  position: absolute;
  top: 0;
  right: 20px;
  font-weight: 900;
  color: #000;
  width: 25px;
  height: 25px;
}

/*Downloaded from https://www.codeseek.co/Cory2911/notifications-jzNxdQ */
function NotificationContainer($target)
{
  this.parent = $target;
  this.notifications = [];
  this.MaxNotifications = 5;
}

NotificationContainer.prototype.SetMax = function(maxNumber){
  this.MaxNotifications = maxNumber;
}

NotificationContainer.prototype.DisplayNotifications = function()
{  
  for (var i = 0; i < this.notifications.length; i++)
  {
      this.parent.append(this.notifications[i]);
  }
}

NotificationContainer.prototype.CreateErrorNotification = function(message)
{
  console.trace("Creating error notification.");
  var notification = new Notification(message, "error");
  this.AddToNotificationQueue(notification.$notification);
  this.DisplayNotifications();
  console.trace("Created error notification successfully.");
}

NotificationContainer.prototype.CreateSuccessNotification = function(message)
{
  console.trace("Creating error notification.");
  var notification = new Notification(message, "success");
  this.AddToNotificationQueue(notification.$notification);
  this.DisplayNotifications();
  console.trace("Created error notification successfully.");
}

NotificationContainer.prototype.AddToNotificationQueue = function ($notification)
{
  this.notifications.push($notification);     
  if (this.notifications.length > this.MaxNotifications)
    {
      while (this.notifications.length > this.MaxNotifications)
      {
        var $removedNotification = this.notifications.shift();
        NotificationContainer.prototype.RemoveGTSNotification($removedNotification);     
      }
    }
}


NotificationContainer.prototype.RemoveGTSNotification = function($notification)
{
        $notification.css("transform", "translate(100%)");
        setTimeout(function(){
            $notification.remove();
          //Move the notifications that are left up by the height of the removed notification
          $notification.nextAll().css("transform", "translate(0px, -" + $notification.height() + ")");
        },250);
}


function Notification(message, type)
{
  this.message = message;
  this.type = type
  
    var $notification = $("<div class='gts-notification gts-" + this.type + "-notification'>" + this.message + "</div>")
    this.$notification = $notification;
  
    var $notificationCloseButton = $("<div class='close-notification-btn'>X</div>");
    $notificationCloseButton.on("click", NotificationContainer.prototype.RemoveGTSNotification.bind(null,$notification));
    $notification.append($notificationCloseButton);  
  
    return this;  
}


var container = new NotificationContainer($(".container"));
container.CreateErrorNotification("Test Error.</br>Test Multiline");
container.CreateSuccessNotification("Test Success.");
container.CreateErrorNotification("Test Error.");
container.CreateSuccessNotification("Test Success.");
container.CreateErrorNotification("Test Error.");
container.CreateSuccessNotification("Test Success.");

Comments