A pop up window is a great UI tool to use on your site when you need to provide information or a element on your site but it either isn’t necessary to load on the page at first sight or require it as space might be limited on your site.
You can use this tool for a whole spectrum of reasons, it could be to load a product page in a minimised browser so the customer can carry on browsing your collection quickly, it could used as a contact form for product enquires or just a simple size chart pop up… The choice is yours!
If used properly it could be a great addition as CTA button, drawing in the audience to click on it to reveal what’s behind the actual button maybe?
Where to start?
Find an appropriate place to locate the button, for instance an enquiry form on a product page if your product is out of stock? Right underneath the product title:
Copy and paste!
Simply copy and paste this code into where you’d like your button too appear on the page:
<div class="btn" id="myBtn">HELLO</div>
The following code needs to be pasted below the button, this is the html code that will be the pop up window:
<!-- The Pop up--><div id="mypopup" class="popup"><!—Pop up content --><div class="popup-content"><span class="close">×</span>“HELLO”</div></div></div>
Styling
Now lets add some style elements to the pop up, if confident with css then feel free to edit this:
<style>/* The Pop up(background) */.popup {display: none; /* Hidden by default */position: fixed; /* Stay in place */z-index: 1; /* Sit on top */left: 0;top: 0;width: 100%; /* Full width */height: 100%; /* Full height */overflow: auto; /* Enable scroll if needed */background-color: rgb(0,0,0); /* Fallback color */background-color: rgba(0,0,0,0.4); /* Black w/ opacity */}/* Pop up Content/Box */.popup-content {background-color:#2c2c2c;margin: 15% auto; /* 15% from the top and centered */padding: 20px;border: 1px solid #888;width: 80%; /* Could be more or less, depending on screen size */}/* The Close Button */.close {color: #aaa;float: right;font-size: 28px;font-weight: bold;}.close:hover,.close:focus {color: black;text-decoration: none;cursor: pointer;}</style>
Now let’s put it all together
<script>// Get the Pop upvar popup = document.getElementById("mypopup");// Get the button that opens the Pop upvar btn = document.getElementById("myBtn");// Get the <span> element that closes the modalvar span = document.getElementsByClassName("close")[0];// When the user clicks on the button, open the Pop upbtn.onclick = function() {popup.style.display = "block";}// When the user clicks on <span> (x), close the Pop upspan.onclick = function() {popup.style.display = "none";}// When the user clicks anywhere outside of the Pop up, close itwindow.onclick = function(event) {if (event.target == popup) {popup.style.display = "none";}}</script>