Jump to content
Sign in to follow this  
Raktim

Animated Modal Box

Recommended Posts

I create a modal box that when the user clicks on the button the modal will appear with animation.

When the user clicks on the close button ( located on the right side of the header area ) or anywhere of the screen except modal box it will disappear without animation.

I want that the modal will disappear/hide with animation. 

I create two CSS animation. One is animatetop which I already used when modal will open and another is animatedown which I want to use when modal will disappear. But, I can't understand how to do that? 

See the code on JSFiddle

<html>
<head>
    <style>
        .background{
            display:none;
            position: fixed;
            z-index: 1;
            height: 100%;
            left: 0;
            top: 0;
            width: 100%;
            padding: 100px;
            background-color: rgba(0,0,0,0.4);
        }
        .modal{
            background-color: white;
            position: relative;
            border: 1px solid #888;
            box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
            animation-name: animatetop;
            animation-duration: 0.4s;
            width: 80%;
        }
        .header{
            width: 100%;
            padding: 8px 20px;
            background-color: crimson;
            color: white;    
        }
        .footer{
            width: 100%;
            padding: 8px 20px;
            background-color: lime;
            color: white;
        }
        .middle{
            width: 100%;
            padding: 2px 20px;
            background-color: white;
            color: black;
        }
        h2{
            margin: 0px;
        }

        @keyframes animatetop {
        from {top:-300px; opacity:0}
        to {top:0; opacity:1}
        }
        
        @keyframes animatedown {
        from {top:0; opacity:1}
        to {top:-300px; opacity:0}
        }
        
        span{
            float: right;
            color: white;
            font-size: 28px;
            font-weight: bold;
            cursor: pointer;
            padding: 2px 5px;
        }
    </style>
</head>

<body>
    <button onclick="modal()">Open Modal</button>
    <div class="background" id="MyModal">
        <div class="modal" id="modal">
            <div class="header">
                <span onClick="a()">&times;</span>
                <h2>Header</h2>
            </div>
            <div class="middle">       
                <h1>Modal</h1><br>
                <h1>Popup Box</h1>
            </div>
            <div class="footer">
                <h2>Footer</h2>
            </div>
        </div>
    </div>
</body>
    
<script>
        background = document.getElementById("MyModal");
        function modal()
        {
            document.getElementById("MyModal").style.display = "block";
        }
       function a()
        {
            document.getElementById("MyModal").style.display = "none";
        
        }
       window.onclick = function (event)
       {
           if(event.target == background)
               {
                 document.getElementById("MyModal").style.display = "none";
               }
       }
</script>
</html>

 

Share this post


Link to post
Share on other sites
<html>
<head>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>

	<title>Title</title>
	<meta charset="utf-8">
    <style>
        .background{
            display:none;
            position: fixed;
            z-index: 1;
            height: 100%;
            left: 0;
            top: 0;
            width: 100%;
            padding: 100px;
            background-color: rgba(0,0,0,0.4);
        }
        .modal{
            background-color: white;
            position: relative;
            border: 1px solid #888;
            box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
            width: 80%;
        }
        .header{
            width: 100%;
            padding: 8px 20px;
            background-color: crimson;
            color: white;    
        }
        .footer{
            width: 100%;
            padding: 8px 20px;
            background-color: lime;
            color: white;
        }
        .middle{
            width: 100%;
            padding: 2px 20px;
            background-color: white;
            color: black;
        }
        .open {
        	display: block;
        }
        h2{
            margin: 0px;
        }
        
        span{
            float: right;
            color: white;
            font-size: 28px;
            font-weight: bold;
            cursor: pointer;
            padding: 2px 5px;
        }
        .animated {
  			animation-duration: 1s;
  			animation-fill-mode: both;
		}
		 @keyframes fadeintop {
  0% {
    transform: translateY(-50px);
    opacity: 0;
  }
  100% {
    transform: translateY(0);
    opacity: 1;
  }
}
.fadeintop { 

  animation-name: fadeintop;
}
 @keyframes fadeouttop {
  0% {
    transform: translateY(0);
    opacity: 1;
  }
  100% {
    transform: translateY(-50px);
    opacity: 0;
  }
}
.fadeouttop { 

  animation-name: fadeouttop;
}
    </style>
</head>

<body>
    <button id="button">Open Modal</button>
    <div class="background" id="MyModal">
        <div class="modal" id="modal">
            <div class="header">
                <span onClick="a()">&times;</span>
                <h2>Header</h2>
            </div>
            <div class="middle">       
                <h1>Modal</h1><br>
                <h1>Popup Box</h1>
            </div>
            <div class="footer">
                <h2>Footer</h2>
            </div>
        </div>
    </div>
</body>
    
<script>
        var background = $('#MyModal')[0];

        $("#button").on("click", e => {
        	if(!$("#MyModal").hasClass("animated")) {
        		$("#MyModal").addClass("animated");
        	}
        	$("#MyModal").removeClass("fadeouttop");
        	$("#MyModal").addClass("fadeintop open");
        });
       function a() {
        	$("#MyModal").removeClass("fadeintop");
            $("#MyModal").addClass("animated fadeouttop");
          setTimeout(function(){
          	$("#MyModal").removeClass("open");
          },800);
        
        }
       $("body").on("click", event =>{
           if(event.target == background)
               {
                 a();
               }
       });
</script>
</html>

 

You can use the jquery.

Please try this codes. If you want all animation types :

https://drive.google.com/file/d/1lU58HfEdFEizqpqk7kpfoWWT5_bpB5gq/view?usp=sharing

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...