Display a special message to your visitors with an easy-to-code jQuery modal window

July 27th, 2010 § 9 comments

We love marking life’s events. It’s what life is all about after all, a series of joyful, sad, dull, or exquisite moments. Business life isn’t any different. From the first customer to the 5-year anniversary of the company, the lives of our beloved businesses are also marked by step stones.

 

This is why sometimes, as website owners, it is nice to get intimate with our visitors by bringing up an interesting yet crucial fact about our site or activities. A new partnership, an important change, or just a ‘thank you note’ on a special event like an anniversary are indeed worth flagging.
jQuery modal window

 

Of course, there are many ways to deliver your followers such messages. And when it comes to communication, it is more than likely that a site owner will be using more than one technique to do so: Emails can be sent, new pages can be designed, and tweets (or other social media links) can be triggered but I find the usage of informative modal windows upon site entry quite convenient, and sooooo much more sexier than the brutal pop up windows or alert boxes.

 

It is likely you may already be using modal windows for your contact forms, or to display in detail your portfolio or other various pictures, but instead of using an obscure ready-to-use script or plugin, I’m going to show you how to quickly code a sexy modal window in jQuery (in less than 20 lines), which you may use on your site’s index page (or elsewhere) to display a message for a special date/event, and which will require the interaction of your visitors before they can browse the rest of your site.

 

If you’ve read my post about pushing the minimalist approach (more details here), you will recognize the now-famous but yet fake Mobilytics website I’m gonna be using for this demonstration.

 

Right, folks. Let’s put our thinking hats on.

 

We are going to need for this:

 

  • a layout for a semi-dark mask to partially-hide the index page (CSS)
  • a way to send the index page behind this mask while we show the modal window (jQuery)
  • a layout for the modal window (CSS)
  • a position for the modal window (jQuery)
  • a way to make the modal window appear by default upon entry (jQuery)
  • a way to end the modal window process when the visitor clicks on the close button OR anywhere on the mask (jQuery)

 

Right, Let’s get cracking then.

 

I’m gonna pass on the CSS lines regarding the layout of the site, and just concentrate on the lines regarding the mask and the modal window.

 

#dark                {
                         display: none;
                         background-color:#000;
                         position:absolute;
                         left:0;
                         top:0;
                         z-index:100;
                        }
#message           {
                          display:none;
                          position:absolute;
                          left:0;
                          top:0;
                          z-index:101;
                          width:350px;
                          height:250px;
                          background-color:#FFFFFF;
                          border:1px solid #333;
                          -moz-border-radius: 20px;
                          -webkit-border-radius : 20px;
                          padding: 20px;
                          color: #666666;
                         }

 

Up to you to change those settings and adapt them to your own requirements/preferences.
As you can see, I’ve given the dark mask a z-index of 100 and the modal window a z-index of 101, for obvious reasons. Note the border radius of the modal window, which will give it a sexier appeal.

jQuery code

 


 

Ok so let’s take this in the coming order:

I’m using variables WindowHeight and WindowWidth to:

  • determine the size of the screen, and report those two onto the css of the mask (so it covers the full screen).
  • center the modal window according to both the screen and the CSS-set sizes of the informative modal window.

Let’s give the dark mask a fading speed and an opacity level…

$('#dark').fadeTo(2000,0.75);

 

… apply the same principles (with no opacity this time) to the modal window:

$('#message').fadeIn(2000);

 

And finish it off with a couple of commands to end the modal window process (both mask and modal windows then fade out), when:

 

a) Visitor clicks on the picture which we setup with a .close class

 

$('.close').click(function() {
$('#dark').fadeOut();
$('#message').fadeOut();

 

Or b) Visitor clicks on the dark mask

 

$('#dark').click(function () {
$('#dark').fadeOut();
$('#message').fadeOut();

 

Easy peasy, isn’t it?

 

Check the demo and good usage of this technique if you decide to implement it on your site(s). Download files here.

§ 9 Responses to Display a special message to your visitors with an easy-to-code jQuery modal window"

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes:

What's this?

You are currently reading Display a special message to your visitors with an easy-to-code jQuery modal window at Shoogle Designs.

meta