Create a Fading Popup Modal with jQuery


This tutorial is aimed at getting beginners familiar more advanced concepts like data attributes and, at times, there will be more “efficient” ways of doing things, like creating advanced functions and optimising the output. However, as this tutorial is aimed at helping beginners, I’ll be taking a more procedural approach than some people might take. There is no right or wrong way here, there are simply more advanced and less advanced methods of getting the desired outcome.

Table of Contents

  1. Skip to the HTML
  2. Skip to the CSS
  3. Skip to the jQuery

If you get stuck at all please leave me a comment below with a description of the problem and a link to a JSFiddle showing your code in action. I’m here to help, no matter what the problem.

You might also like: How to Create a 12 Column Grid System with Sass

1.) Step 1: HTML

Pro-Tip: Place it in the Footer

If you’re running a PHP powered site, like a WordPress blog, then it’s a good idea to put the code for the popup itself in the footer include file. This way you can always be sure the popup will be included on every page.

Final HTML

The HTML Explained

The structure of the popup is quite straightforward, so nothing to worry about there. We start off with a wrapper div and give it a class of popup. This is the black background around the popup but also what we’ll animate with jQuery.

You may notice those data attributes I’m using, for example data-popup="popup-1". The reason for using these is to make the code easier to use and maintain in the long run. Some people are advocates of using IDs in the same approach, however, this can become confusing as IDs are also commonly used for styling.

The open button also uses a data attribute of data-popup-open="popup-1", which you may notice is the same value as the popup itself. This is handy because it will allow us to dynamically open the popup with jQuery.

To close the popup there’s a normal anchor link and a specially positioned anchor link with data-popup-close="popup-1" applied to them. The good thing about using a data attribute on both links is that it allows us to have a special close button in the upper right of the popup, but also use a normal link inside of the popup to close it as well.

2.) Step 2: CSS

Final CSS

The CSS Explained

The popup wrapper is using RGBA colour values to enable transparency, something a basic #HEX code can’t do. It also has fixed positioning, which makes it stay centred if the page is scrolled.

The inner content area of the popup is absolutely positioned but also repositioned with transform. The absolute positioning moves the inner popup content left 50% and down 50%. The transform then moves it left -50% of its own width and down -50% of its own height. This then enables the inner popup to be exactly centred no matter what the size of the screen.

The close button actually has a lot more styling rules than you’d expect. The reason it has a fon-family and font-size added is to make the popup work better across different projects. I highly advise using an icon font for the close button. For a good example, click the newsletter subscribe button at the top of the sidebar on this site. There’s also a transition on the button, this comes into play when you hover.

On hover, the button is spun around 180 degrees. If you put 360 degrees it will actually not look any different so 180 is what’s needed for the desired spinning effect.

That’s it for the CSS. If you’ve got any question on what we covered so far, please leave me a comment below.

3.) Step 3: jQuery

Final jQuery

The jQuery Explained

Do you remember those data attributes from the HTML? This is where they come into play. We first set up a click event on any of the open buttons and stop the default behaviour, which in this case, would be to follow the link. If we don’t use e.preventDefault(); to stop the default behaviour, a # will be added to the URL in the address bar.

The next step is to grab the value from the attribute and save that information to a variable. The value saved will be whatever has been added to that buttons attribute data-popup-open. In our case, the output will be popup-1.

Now comes the fun part, fading in the popup. The code below might look pretty scary but really, it’s quite simple. All it says is “find the popup with a data-popup value of popup-1 and fade it in for 350 milliseconds“. Also, because we’re using a variable, it saves us from having to repeat the open functionality for each popup we create, as it’s dynamically found and used.

Closing the Popup

Closing the popup is actually just a reverse of opening it. Instead of looking for the value of the open button we’re looking for the value of the close button. We’re also fading out instead of fading in.

In Conclusion

Custom popup modals can be incredibly powerful, but also far more annoying if used incorrectly. When implementing your popup modal please don’t have it to autoload. This is a big problem as it will cause people to never return, even if you remove the popup because there only thought of your site and brand is an annoying popup the second they open up some of your content.

Help and support: If you have questions and/or need help with any code, please leave a comment below with a link to a JSFiddle showing your code in action.

You might also like: Tooltips in HTML and CSS.

Show More

Related Articles

Leave a Reply

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