What is the process for creating a modal transition?

I am attempting to add animation to a modal using a transition effect. My goal is to open it slowly, either from the center of the screen or from the bottom. However, I am struggling to understand how this can be achieved...

While searching on Google, I came across this code snippet:

https://codepen.io/designcouch/pen/obvKxm

Unfortunately, I find the code too complex and have difficulties in adapting it to my own code...

/*Open stress popup */
// Get the  modal
var hydricstressmodal = document.getElementById('hydricstressmodal');

// Get the button that opens the modal
var stress = document.getElementById("stress");

// Get the <span> element that closes the modal
var hydricstressspan = document.getElementsByClassName("stressclose")[0];

// When the user clicks the button, open the modal 
stress.onclick = function() {
    hydricstressmodal.style.display = "block";
}

// When the user clicks on <span> (x), close the modal
hydricstressspan.onclick = function() {
    hydricstressmodal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
    if (event.target == hydricstressmodal) {
        hydricstressmodal.style.display = "none";
    }
}

/*Open vegetal popup */
// Get the  modal
var vegetalmodal = document.getElementById('vegetalmodal');

// Get the button that opens the modal
var vegetal = document.getElementById("vegetal");

// Get the <span> element that closes the modal
var vegetalspan = document.getElementsByClassName("vegetalclose")[0];

// when clicked, open the modal 
vegetal.onclick = function() {
    vegetalmodal.style.display = "block";
}

// when clicked on <span>, close the modal
vegetalspan.onclick = function() {
    vegetalmodal.style.display = "none";
}

// When click anywhere outside of the modal, close it
window.onclick = function(event) {
    if (event.target == vegetalmodal) {
        vegetalmodal.style.display = "none";
    }
}
@charset "UTF-8";
/* CSS Document */
body {
  margin: 0;
  font-size: 28px;
  background-color: #00011f;
  display: flex;
  flex-direction: column;
  margin : auto;
}


/*styling for hydric stress popup*/
.hydricstressmodal {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1; /* Sit on top */
    padding-top: 100px; /* Location of the box */
    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.7); /* Black w/ opacity */
}

/* styling for hydric stress Modal Content */
.stress-content {
    background-color: #fefefe;
    margin: auto;
    padding: 20px;
    border: 1px solid #888;
    width: 80%;
    height: 70%;
    border-radius: 30px;
    overflow: scroll;

}
.popstress img{
  width: 20%;
}

/* Close Button Style */
.stressclose {
    color: #aaaaaa;
    float: right;
    font-size: 28px;
    font-weight: bold;
}

.stressclose:hover,
.stressclose:focus {
    color: #000;
    text-decoration: none;
    cursor: pointer;
}

/*styling for Vegetal popup*/
.vegetalmodal {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1; /* Sit on top */
    padding-top: 100px; /* Location of the box */
    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 */
    bakground-color: rgba(0,0,0,0.8); /* Black w/ opacity */
  
}

/* Styling for Vegetal Modal Content */
.vegetal-content {
    background-color: #fefefe;
    margin: auto;
    padding: 20px;
    border: 1px solid #888;
    width: 80%;
    height: 70%;
    border-radius: 30px;
    overflow: scroll;

}

.popvegetal img{
  width: 40%;
}
/* The Close Button */
.vegetalclose {
    color: #aaaaaa;
    float: right;
    font-size: 28px;
    font-weight: bold;
}

.vegetalclose:hover,
.vegetalclose:focus {
    color: #000;
    text-decoration: none;
    cursor: pointer;
}
<div id="content" class="container">


  <h3>Electrophotonic Engineering: New approach to macroscopic imaging through corona discharge in healthcare and biotechnology sector.</h3>

  <div id="file" action="" class = "container"> 
  <input id = "stress" type="image" src="IMAGES/PNG/hydricstress.png" />
  <div class = "text">
   Hydric Stress
  </div>  
  </div> 

  <!-- The hydric stress Popup -->
<div id="hydricstressmodal" class="hydricstressmodal">
  <div class="stress-content">
   <span class="stressclose">&times;</span>
    <div class ="popstress" ><img src="images/png/hydricstress.png"></div>
    <p>Some text in the Modal..</p>
 </div>
</div>


  <div id="file" action="" class = "container"> 
  <input id = "vegetal" type="image" src="IMAGES/PNG/vegetal.png" />
  <div class = "text">
   Applied Biophotonics in Plants
  </div>  
</div>
  </div>

  <!-- The vegetal Modal -->
<div id="vegetalmodal" class="vegetalmodal">
  <div class="vegetal-content">
   <span class="vegetalclose">&times;</span>
    <div class ="popvegetal" ><img src="images/png/vegetal.png" ></div>
    <p>Some text in the Modal..</p>
 </div>
</div>  



  <div id="file" action="" class = "container"> 
  <a href="#bridge"><img src="IMAGES/PNG/pont.png" width="100%" /></a>
  <div class = "text">
   Studies on Photonic Bridges
  </div>  
  </div>



  
<script type="text/javascript" src="JS/sticky_navbar.js"></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js'></script>
<script  src="js/index.js"></script>
<script  src="js/button.js"></script>

Answer №1

An easy fix using CSS animations:

$('#show').click(function() {
  $('#overlayModal').show().addClass('modal-active');
});

$('#hide').click(function() {
  var element = $('#overlayModal');
  element.removeClass('modal-active');
  setTimeout(function() {
    element.hide();
  },200);
});
#overlayModal {
  position: fixed;
  top:0;
  left:0;
  right:0;
  bottom:0;
  background-color: rgba(0,0,0,0.8);
  z-index:9999;
}

#modalContent {
  position:fixed;
  width:60%;
  top:55%;
  left:50%;
  padding:15px;
  text-align:center;
  background-color:#fafafa;
  box-sizing:border-box;
  opacity:0;
  transform: translate(-50%,-50%);
  transition:all 150ms ease-in-out;
}

#overlayModal.modal-active #modalContent {
  opacity:1;
  top:50%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="show" type="button">Show Modal</button>
<div id="overlayModal" style="display:none;">
  <div id="modalContent">
    <h1>My Modal</h1>
    <p>This is a basic modal</p>
    <button id="hide" type="button">Hide</button>
  </div>
</div>

Answer №2

For those looking to save time on implementing this feature, consider utilizing a library such as . It is user-friendly and gets the job done efficiently.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Incorporating JavaScript and CSS files into a content page of a master page in ASP.NET: Steps to follow

I am facing an issue with adding javascript files and css to a content page of a master page in asp.net. I attempted to include a datetime picker on my ContentPage, but it only works on the masterpage. When I try to add the same code to my contentpage, i ...

Issues with CSS rendering have been observed following an ajax update when using the <ui:repeat> tag

This scenario is a bit intricate. Essentially, I am utilizing Material Design Lite CSS provided by Google and triggering an AJAX request to dynamically add input fields using PrimeFaces. <h:commandLink value="+ Add something> <f:ajax listener ...

strategies for chaining together multiple observables with varying data types and operations

Hey everyone! I'm facing a situation where I have a form with multiple select types, and the options for these inputs are coming from an API. I then take the data emitted from the HTTP request observable and feed it into my FormGroup, and everything i ...

Design a circular progress bar with a cut-off at the bottom

Does anyone have suggestions on how to create a circular progress bar with cropping at the bottom, similar to this example: PROGRESS BAR? I've been searching for a component like this but haven't had any luck. Any ideas, especially utilizing Mate ...

A windows application developed using either Javascript or Typescript

Can you provide some suggestions for developing Windows applications using Javascript, Typescript, and Node.js? ...

The overflow property is not functioning as anticipated

I've browsed Google and this site, but I couldn't find a solution to my issue. On my webpage, I have a shoutbox and a teamspeak viewer with a specific height for the outer div. The scrollbars don't look good on our new design, so I attempt ...

What is the best way to send a file object to a user for download?

When working within a route: app.get('some-route', async (req, res) => { // ... } I am dealing with a file object called file, which has the following structure: https://i.stack.imgur.com/ByPYR.png My goal is to download this file. Cur ...

Error: The property 'postID' can not be read because it is undefined

I'm new to programming and I am working on creating a news/forum site as a practice project. I have set up a route /post/postID/postTitle to view individual posts. Initially, when I used only :postID, it worked fine. But after adding :postTitle, whene ...

Storing data with jQuery

Looking to store objects in jQuery for events. Each event will have a date, title, and some text that needs to be stored in an array. Wondering if using a multi-dimensional array would be the best way to go so I can easily iterate through them with a count ...

Is it possible to utilize a JavaScript variable in this particular scenario and if so, what is the

let myVariable = <?php echo json_encode($a[i want to insert the JS variable here]); ?>; Your prompt response would be highly valued. Many thanks in advance. ...

WordPress CSS Styling Issue: HTML Element Display Problem

I recently converted my static HTML site to WordPress, but I'm facing an issue where the site is always aligned to the left. Despite trying different solutions like the one below: body{ margin: auto; } The original HTML page was centered perfectly ...

Looking to attach the "addEventListener" method to multiple elements that share the same class?

I'm trying to use an event listener in JS to handle the logic in the "onClick" function, but it's only executing once. I've applied the same class to all four buttons, so I'm not sure why it's only working for the first one. HTML: ...

What is the best way to verify a user's login status in AngularJS using $routeChangeStart event?

I am new to AngularJS and I need help checking if my user is logged in or not using $routeChangeStart. Controller angular.module('crud') .controller('SigninCtrl', function ($scope,$location,User,$http) { $scope.si ...

Can state values be utilized as content for Meta tags?

I am looking for a way to display image previews and titles when sharing a page link. In order to achieve this, I am using the Nextjs Head Component. The necessary details are fetched on page load and used as content for the meta attributes. let campaign = ...

Using jQuery to manipulate the image within a specific div element

I'm facing an issue with locating the img src within a div. I've written a function to find all the divs with specific ids: function identifyDiv(){ divArray = $("div[id^='your']"); divArray = _.shuffle(divArray); } This is the ...

Display the Bootstrap datepicker within an h4 element set to default to today's date, utilizing both Angular and jQuery

Utilizing Boostrap datepicker to obtain the selected date from the calendar and insert it into an <h4> html tag. However, my goal is to display today's date by default in the h4 when it opens for the first time. Using angular.js + jquery f ...

Using Angular to display asynchronous data with ngIf and observables

In cases where the data is not ready, I prefer to display a loader without sending multiple requests. To achieve this, I utilize the as operator for request reuse. <div class="loading-overlay" *ngIf="this.indicatorService.loadingIndicators[this?.indic ...

Incorporating jquery-ui in a Rails application does not lead to an error even with a fictional alias

I'm currently working on integrating the jquery-ui-rails gem into my rails application. The app is based on Spree version 3.0. $ bundle list | grep jq * jquery-rails (4.0.5) * jquery-ui-rails (5.0.5) Despite attempting various solutions in the a ...

Showcasing Portfolio Work in a User-Friendly Mobile Design

Currently revamping my portfolio website and looking for ways to optimize the display of my personal projects. I have a card-like interface in place that works well on desktop but only shows one project at a time on mobile devices. Seeking solutions to imp ...

What are the steps to sorting in JavaScript?

I need help with sorting an array. The array I have looks like this: var temp = [{"rank":3,"name":"Xan"},{"rank":1,"name":"Man"},{"rank":2,"name":"Han"}] I've tried to sort it using the following code: temp.sort(function(a){ a.rank}) But unfortun ...