Simple jQuery popup on page load
var shadow = $( '<div id="shadowElem"></div>' ); $(document).ready( function () { $( 'body' ).prepend(shadow); }); |
#shadowElem { position : absolute ; top : 0 ; left : 0 ; width : 100% ; height : 100% ; background-color : black ; opacity: 0.3 ; } |
< div id = "dropElem" > <!-- wrapper --> < div id = "dropContent" > <!-- Inside this tag put your popup contents --> < div id = "dropClose" >X</ div > <!-- this will serve as close button --> < img src = "http://beautiful-island.50webs.com/beautiful-island/beautiful-island.jpg" alt = "blue" /> <!-- using this image as demo --> </ div > </ div > |
#dropElem { display : none ; position : absolute ; top : 0 ; border-radius: 10px 10px 10px 10px ; box-shadow: 0 0 25px 5px #999 ; padding : 20px ; background : #fff ; } #dropContent { position : relative ; } #dropClose { position : absolute ; z-index : 99999 ; cursor : pointer ; top : -32px ; right : -30px ; padding : 5px ; background-color : black ; border-radius: 6px 6px 6px 6px ; color : #fff ; } |
var speed = 1000; //animation speed $(window).load( function () { screenHeight = $(window).height(); screenWidth = $(window).width(); elemWidth = $( '#dropElem' ).outerWidth( true ); elemHeight = $( '#dropElem' ).outerHeight( true ) leftPosition = (screenWidth / 2) - (elemWidth / 2); topPosition = (screenHeight / 2) - (elemHeight / 2); //place drop element on top middle of screen $( '#dropElem' ).css({ 'left' : leftPosition + 'px' , 'top' : -elemHeight + 'px' }); //some animation .. coming down from top $( '#dropElem' ).show().animate({ 'top' : topPosition }, speed); //change opcity of shadow element shadow.animate({ 'opacity' : 0.7 }, speed); //close button $( '#dropClose' ).click( function () { shadow.animate({ 'opacity' : 0 }, speed); $( '#dropElem' ).animate({ 'top' : -elemHeight + 'px' }, speed, function () { shadow.remove(); $( this ).remove(); }); }); }); |
<!doctype html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">
var shadow = $('<div id="shadowElem"></div>');
var speed = 1000;
$(document).ready(function() {
$('body').prepend(shadow);
});
$(window).load( function() {
screenHeight = $(window).height();
screenWidth = $(window).width();
elemWidth = $('#dropElem').outerWidth(true);
elemHeight = $('#dropElem').outerHeight(true)
leftPosition = (screenWidth / 2) - (elemWidth / 2);
topPosition = (screenHeight / 2) - (elemHeight / 2);
$('#dropElem').css({
'left' : leftPosition + 'px',
'top' : -elemHeight + 'px'
});
$('#dropElem').show().animate({
'top' : topPosition
}, speed);
shadow.animate({
'opacity' : 0.7
}, speed);
$('#dropClose').click( function() {
shadow.animate({
'opacity' : 0
}, speed);
$('#dropElem').animate({
'top' : -elemHeight + 'px'
}, speed, function() {
shadow.remove();
$(this).remove();
});
});
});
</script>
<style type="text/css">
#dropElem {
display: none;
position: absolute;
top: 0;
border-radius: 10px 10px 10px 10px;
box-shadow: 0 0 25px 5px #999;
padding: 20px;
background: #fff;
}
#shadowElem {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: black;
opacity: 0.3;
}
#dropContent {
position: relative;
}
#dropClose {
position: absolute;
z-index: 99999;
cursor: pointer;
top: -32px;
right: -30px;
padding: 5px;
background-color: black;
border-radius: 6px 6px 6px 6px;
color: #fff;
}
</style>
</head>
<body>
<div id="dropElem">
<div id="dropContent">
<div id="dropClose">X</div>
<img src="http://beautiful-island.50webs.com/beautiful-island/beautiful-island.jpg" alt="blue" />
</div>
</div>
<h3>Hello world</h3>
</body>
</html>
No comments:
Post a Comment