Instructions for creating a fading effect on W3schools Modal example

After utilizing the CSS class from https://www.w3schools.com/w3css/tryit.asp?filename=tryw3css_modal_fade to achieve a fade in effect, I am now looking for a way to fade out when the modal closes. How can this be accomplished?

.w3-animate-opacity{animation: opac 0.8s}@keyframes opac{from{opacity:0} to{opacity:1}}

Below is the code snippet:

<!DOCTYPE html>
<html>
<title>W3.CSS</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<body>

<div class="w3-container">
  <h2>W3.CSS Animated Modal</h2>

  <button onclick="document.getElementById('id01').style.display='block'" class="w3-button w3-black">Fade In Modal</button>

  <div id="id01" class="w3-modal w3-animate-opacity">
    <div class="w3-modal-content w3-card-4">
      <header class="w3-container w3-teal"> 
        <span onclick="document.getElementById('id01').style.display='none'" 
        class="w3-button w3-large w3-display-topright">&times;</span>
        <h2>Modal Header</h2>
      </header>
      <div class="w3-container">
        <p>Some text..</p>
        <p>Some text..</p>
      </div>
      <footer class="w3-container w3-teal">
        <p>Modal Footer</p>
      </footer>
    </div>
  </div>
</div>

</body>
</html>

Answer №1

If jQuery is not your preferred option, you can consider the following alternatives:

Implementing an animation effect:

.custom-animation{
    animation: fadeOut 0.8s;
    animation-fill-mode:forwards; // ensures the animation stops at its final state
}
@keyframes fadeOut{
    0%{opacity:1}
    100%{opacity:0}
}

To apply the animation in your HTML code, modify the span element as follows:

<span onclick="document.getElementById('id01').classList.add('custom-animation');" class="button close-button">&times;</span>

It's important to note that this animation does not actually close the modal, it simply makes it disappear visually.

Answer №2

Visit the following link to view the answer that was chosen: How to achieve a Fade-in-out effect for modals?

Here is the code snippet for achieving this effect:

var modal = document.getElementById('myModal');

document.querySelector('.close').addEventListener('click', function() {
  modal.classList.add('fade-in');
})

modal.addEventListener('animationend', function() {
  if (this.classList.contains('fade-in')) {
    this.style.display = 'none';
    this.classList.remove('fade-in');
  }
});

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

Out-of-sync movement in animated characters

Looking for some assistance with my page coding. I have a scenario where two stars are moving around a button, simulating an animation by incrementing the CSS properties top and left. Initially, everything appears fine and they move synchronously. However, ...

I can only use innerHTML once in my React application

I am attempting to clear my container div when the user clicks the search button in order to remove all existing search results. However, I am encountering an issue where using innerHTML to clear the container div only works the first time. If a second sea ...

Utilizing absolute positioning with a rotated div element

The situation I am facing involves a parent div and child div. The child div is positioned absolutely with respect to the parent div and has been rotated by an angle of 90 degrees. Due to the fact that the origin is located at the center of the child div ( ...

Automatically load file and play audio when the page is loaded

I have created code that allows me to input an mp3 file, output the audio, and display a visual representation of the music. Currently, loading the file is done manually through an input type="file". Now, I want to change it so that the music automaticall ...

Javascript does not execute properly in Google Apps Script

Encountering issues while attempting to execute the code provided in Google app script. // 1.foldername = Folder name | 3.templateId = ID of the template to use // 2.SheetId = ID of spreadsheet to use. | 4.rootFolder = ID of the root fold ...

Ways to align two columns at the center using CSS

I'm currently facing some challenges centering two columns on my website. Despite my efforts to make adjustments, the result always ends up with the columns positioned to the left (you can view the image). Could you please help me identify what I migh ...

Hide button for the last input form to dynamically remove in jQuery

At first, I hide the remove button when there is only one form. However, if there are multiple forms and the user deletes all of them, I want the last form to remain visible and not be deleted. Can someone assist with this issue? Here is my code: <! ...

Ways to enlarge the font size of text on a mobile website?

this is the site i have added viewport, but with image, the text saw on phone is small, how to make the text bigger when see on the phone, what should I add to the code? this is the site i have added viewport, but with image, the text saw on phone is sm ...

Using CSS to overlay a fixed element with a specific z-index

HTML: <div class="wrap"> <div class="fixed"></div> </div> <div class="cover"></div> CSS: .cover{z-index: 10;} .wrap{position: relative; z-index: 1;} .fixed{position: fixed; display: block; z-index: 1;} Example: ...

Certain line borders are more robust compared to the rest

I have been working on a calendar application where I have designed boxes to display dates. However, I have encountered an issue where some of the line borders appear darker or thicker than others. Below is a snippet of my CSS and HTML code: <style> ...

Using PHP with the CodeIgniter framework, learn how to use Javascript to delete specific values in an HTML table row

Within my CodeIgniter project, I have a table that is being dynamically generated by Ajax. Each row in the table has a button to delete the corresponding entry from both the HTML table and the MySQL table. I have already found a way to remove the HTML tab ...

Utilizing Javascript to Extract Data from Twitter Json Files

Can someone provide assistance with parsing JSON feed text retrieved from Twitter? I am looking to access and apply style tags to elements like the link, created date, and other information. Any tips on how I can achieve this task successfully would be g ...

Transforming a JSON string containing multiple arrays into a JavaScript object

My JSON file is structured as follows: { "items":[ { "username":"abc", "orderID":"1234", "commentHistory":[ { "comment":"Comment Date: 2016/12/09 13:44:23" }, { ...

Center align Bootstrap 4 dropdown menu

I am attempting to center align my dropdown menu using Bootstrap 4. Following the given example: [ This is the result I am achieving: [ This is the code I have implemented: <div class="container"> <div class="row"> <div class=" ...

Align an image with text using CSS and HTML

I'm having trouble with my CSS and HTML code. I want to align the text to the right of an image in a grid format, but it keeps appearing below the image instead. Here is the code snippet: <html> <head> <style type="text/css"> #conta ...

Adjusting image size according to browser dimensions

I've been struggling to resize a background image on my Square space website dynamically based on the user's browser window size. If the window width drops below a certain point, I want the image to adjust accordingly while maintaining its aspect ...

Disabling the visibility of elements through a transparent sticky-top menu

I'm having an issue with my website design. I have a gradient background and a sticky-top menu on the site. The problem is that when I scroll down, the content appears through the menu, which is not ideal. I don't want to apply the same gradient ...

Adding an image in HTML from a different drive - step-by-step guide!

Currently, I am immersing myself in HTML, CSS, and JavaScript as I gear up for my upcoming school project centered around frontend development. Here's the issue I encountered. While attempting to insert an image into one of my HTML files, I ran into ...

What is the most efficient way to display a dynamic alphabetical order list?

sample code: <table width="100%" cellspacing="0" cellpadding="0" border="0" style="line-height: 1.7;"> <tbody> <?php $this->db->select('*'); $this->db->from('cm_options'); ...

Use jQuery to permit only numeric characters and the symbol "-" to be entered into a textbox

I have been working on a JQuery script that only allows users to input numbers in a text field. However, I am now trying to modify the code to also allow users to enter "-" (hyphen), but so far it's not working as expected. If anyone can provide assis ...