Live DemoDownload Source Code

Building the Page

To get started, we need to make two files named “index.html” and “style.css”. I will be including a reference to the latest jQuery library hosted by Google’s code CDN. The HTML is fairly straightforward since we just need to create some dummy text within an alert box. And all of the JavaScript has been added into the bottom of the page to save on HTTP requests.

This is my header code for setting the external documents and the HTML5 doctype. Not very convoluted since we are just building a sample demo. For the notification windows I have setup two different styles – success and error. There are other design styles like warnings and info boxes, however I did not create alternate styles because I wanted to focus more on the effects.

Each icon image was created from this freebie PSD of flat elements and UI pieces. The icons were vector shapes which I scaled and resized to fit as a checkmark and X error button. If you need a warning/info icon it shouldn’t be hard to update the colors and create your own. The generic class .notify is added onto each of the message boxes. This will generate the box shadow and internal font styles.

Then additional classes such as .successbox and .errorbox allow us to change colors and details within the alert window. You can see this in my demo where the page loads with two existing alert messages. Each of the buttons at the bottom may be clicked to append a new alert box at the top of the page.

CSS Box Styles

I won’t go into too much detail on the CSS resets which are typical in all my other tutorials. I have created a default batch of typography and also centered the wrapper element with an inner #content div. This creates the box area which allows jQuery to append new warning elements at the very top of the page.

The website layout is quite simple to keep the effects noticeable. Anybody who is familiar with frontend web development should be able to port over these classes into their own stylesheet. I am using a unique style for the flat buttons to generate new alert box windows. Similarly I have updated the internal styles for each of the .notify class elements.

I setup a few default assumptions which work best in my sample layout. Each of the message notifications are limited to a 400px width and centered on the page using margin: 0 auto. Also I have updated the cursor icon to a pointer hand so that users know the entire element is clickable. We need to create a jQuery event listener which performs a function whenever the user clicks to dismiss a notification.

jQuery Animation

My JS codes actually perform two distinct operations. First we are detecting any existing .notify elements which are contained inside the #content div. Once a user clicks on this .notify box we need to fade the box down to 0% opacity with display: none, and then remove() the element out of the DOM altogether.

If you are familiar with jQuery then this selector may appear strange at first. We are not selecting the #content div but actually looking for any .notify boxes within the content container. If you check out the documentation for jQuery’s .on() method you will notice we have the ability to pass another selector as the 2nd parameter which will update after the page has been rendered. Here is an excellent thread on Stack Overflow which explains this concept in greater detail.

The other piece of my script will check whenever a user clicks on either of the two buttons at the bottom of the page. These buttons are using the IDs #newSuccessBox and #newAlertBox. Whenever the user clicks we stop the default action of loading the HREF value and instead create a new HTML block to prepend onto the page.

Each function has its own variable containing a copy/paste mirror of the HTML I used for the box items. This HTML content is stored inside a string using the jQuery selector as an object. I can use the method prependTo() and select the content div so the new alert boxes will appear at the very top of the page. All of the new boxes may also be dismissed in the same fashion since their HTML codes are identical to the boxes which are hard-coded in static HTML.

css3 notification alert windows messages jquery tutorial

Live DemoDownload Source Code

For more related resources: