Skip to content

Instantly share code, notes, and snippets.

@asha23
Last active December 28, 2015 16:08
Show Gist options
  • Save asha23/7526377 to your computer and use it in GitHub Desktop.
Save asha23/7526377 to your computer and use it in GitHub Desktop.
Very simple modal window
<style>
#background-popup{
display:none;
position:fixed;
height:100%;
width:100%;
top:0;
left:0;
background:#000000;
border:1px solid #cecece;
z-index:1;
}
#popup-box{
display:none;
position:fixed;
height:384px;
width:408px;
background:#FFFFFF;
border:2px solid #cecece;
z-index:2;
padding:12px;
font-size:13px;
}
#popup-close{
font-size:14px;
line-height:14px;
right:6px;
top:4px;
position:absolute;
color:#6fa5fd;
font-weight:700;
display:block;
}
#button{
text-align:center;
margin:100px;
}
</style>
<script>
// Stupidly simple Popup
// SETTING UP OUR POPUP
// 0 means disabled; 1 means enabled;
var popupStatus = 0;
//loading popup with jQuery magic!
function loadPopup() {
//loads popup only if it is disabled
if(popupStatus==0) {
$("#background-popup").css({"opacity": "0.7"});
$("#background-popup").fadeIn("slow");
$("#popup-box").fadeIn("slow");
popupStatus = 1;
}
}
// disabling popup with jQuery magic!
function disablePopup() {
// disables popup only if it is enabled
if(popupStatus==1) {
$("#background-popup").fadeOut("slow");
$("#popup-box").fadeOut("slow");
popupStatus = 0;
}
}
// centering popup
function centerPopup(){
// request data for centering
var windowWidth = document.documentElement.clientWidth;
var windowHeight = document.documentElement.clientHeight;
var popupHeight = $("#popup-box").height();
var popupWidth = $("#popup-box").width();
// centering
$("#popup-box").css({
"position": "absolute",
"top": windowHeight/2-popupHeight/2,
"left": windowWidth/2-popupWidth/2
});
}
$(document).ready(function(){
// LOADING POPUP
// Click the button event
$("#button").click(function(){
//centering with css
centerPopup();
loadPopup();
});
// CLOSE POPUP
$("#popup-close").click(function(){
disablePopup();
});
// Click the background event
$("#background-popup").click(function(){
disablePopup();
});
// Press Escape event
$(document).keypress(function(e){
if(e.keyCode==27 && popupStatus==1){
disablePopup();
}
});
});
</script>
<center>
<div id="button"><input type="submit" value="Press me please!" /></div>
</center>
<div id="popup-box">
<a id="popup-close">x</a>
<h1>POPUP TITLE</h1>
<div class="">Work the slider magic here see Cycle2 - it's ace</div>
</div>
<div id="background-popup"></div>
@UVLabs
Copy link

UVLabs commented Jun 19, 2015

Nothing happens when button is clicked...

@sunilyadav489
Copy link

Thanks Ash

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment