Fade out the div element when clicked

For my game project, I needed a way to make a div fade out after an onclick event. However, the issue I encountered was that after fading out, the div would reappear. Ideally, I wanted it to simply disappear without any sort of fade effect. Below is the code snippet I used:

function fade() {
  document.getElementById('circle').style.animation="fadeout 1s"
}
@keyframes fadeout {
  1% {
    opacity: 100;
  }
  99% {
    opacity: 0;
  }
  100% {
    visibility: none;
  }
}

.fadeout {
  animation: fadeout 1s
}

.circle {
  height: 25px;
  width: 25px;
  background-color: black;
  border-radius: 50%;
  display: inline-block;
}
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>Aim</title>
    <div onclick="fade()" id="circle" class="circle"></div>
    <link href="style.css" rel="stylesheet" type="text/css"/>
  </head>
  <body>
    <script src="script.js"></script>
  </body>
</html>

Thank you for your help!

Answer №1

A solution has been discovered and is functioning smoothly. Take a look at the modifications. Best of luck!

function fade() {
  document.getElementById('circle').style.animation="fadeout 1s forwards"   //update this section
}
@keyframes fadeout {
  1% {
    opacity: 100;
  }
  99% {
    opacity: 0;
  }
  100% {
    visibility: hidden;   //modify this part
  }
}

.fadeout {
  animation: fadeout 1s
}

.circle {
  height: 25px;
  width: 25px;
  background-color: black;
  border-radius: 50%;
  display: inline-block;
}
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>Aim</title>
    <div onclick="fade()" id="circle" class="circle"></div>
    <link href="style.css" rel="stylesheet" type="text/css"/>
  </head>
  <body>
    <script src="script.js"></script>
  </body>
</html>

Answer №2

To hide the div element after the animation finishes, simply set the display property to none.

function conceal() {
document.getElementById('round').style.animation = "fade-out 1s";
setTimeout(() => { document.getElementById('round').style.display = "none"; }, 1000);
}

Answer №3

To create a smooth transition effect for the circle, you can use JavaScript to set its opacity to 0. Additionally, by implementing a mouse click event, you can make the circle reappear and adjust its opacity back to 1 wherever the user clicks on the screen.

function fade() {
  document.getElementById('circle').style.opacity="0"
}
.circle {
  height: 25px;
  width: 25px;
  background-color: black;
  border-radius: 50%;
  display: inline-block;
  transition: opacity 2s;
}
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>Aim</title>
    <div onclick="fade()" id="circle" class="circle"></div>
    <link href="style.css" rel="stylesheet" type="text/css"/>
  </head>
  <body>
    <script src="script.js"></script>
  </body>
</html>

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

Apply express middleware to all routes except for those starting with /api/v1

Is it possible to define a catchall route like this? app.get(/^((?!\/api/v1\/).)*$/, (req, res) => { res.sendFile(path.join(__dirname, '../client/build', 'index.html'));}); ...

TypeScript Generic Functions and Type Literals

Everything seems to be running smoothly: type fun = (uid: string) => string const abc: fun = value => value const efg = (callback:fun, value:string) =>callback(value) console.log(efg(abc, "123")) However, when we try to make it generic, we e ...

Execution of Ajax call fails to occur synchronously

I have created a unique weather website that utilizes the flickr API as well as the yahoo API to gather weather data. However, I am facing an issue where the ajax call from the yahoo API does not retrieve the necessary data in time for the content of the p ...

Issue with Navigation menu dropdown not functioning properly on Angular version 12 with Bootstrap 5

I am attempting to integrate bootstrap with Angular through a CDN. Below is the content of my index.html file: <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Laboratorio Sigma< ...

Tips on using JQuery to extract form field information from a drop-down menu, display it in a div, and then compare it with the subsequently

In my HTML file, I am using two dropdown lists and JQuery for validation. First, I need to save the selected items from both dropdown lists in a variable and then compare them with the next selection. If the data from both dropdown lists match, an alert m ...

Instructions on creating a JSON patch in Django REST Framework

I have a PATCH button in my ModelViewSet https://i.sstatic.net/qoQLu.png class CompanyViewSet(viewsets.ModelViewSet): serializer_class = s.CompanySerializer queryset = m.Company.objects.all() def patch(self, request, id, format=None): ...

Editing the object retrieved from JSON is not possible once it has been fetched

Project. Input text in the field and it appears on the shirt. When you click "see back," there is an issue where new text overlaps old text. Clicking on "see front" allows you to enter new text, with the previous text saved underneath. Question: How can ...

Removing the loading spinner from Ag Grid

While utilizing ag-grid with rowModelType=serverSide, I have encountered an issue where the loading overlay includes a spinner as shown in the image below. My goal is to eliminate this spinner from the overlay. I attempted using hideOverlay(), but it seems ...

Individual Ajax data

Starting out with javascript, I'm a bit unsure of how to tackle this task. Essentially, I am looking to implement a for loop within the ajax data call, rather than listing each item manually. jQuery(document).ready(function() { ...

Using an external HTML file to import a template into Vue.js single file components

I've been tackling a Vuejs project that involves using vue-property-decorator in single file components. I'm trying to figure out how to import the template from an external HTML (or different) file, but so far I haven't found a solution. I& ...

Updating the Background Color of a Selected Checkbox in HTML

I have a straightforward question that I've been struggling to find a simple answer for. Can anyone help me with this? Here's the checkbox code I'm working with: <input type="checkbox"> All I want to do is change the backgr ...

Attempting to conceal image previews while incorporating pagination in Jquery

I'm working on implementing pagination at the bottom of a gallery page, allowing users to navigate between groups of thumbnail images. On this page, users can click on thumbnails on the left to view corresponding slideshows on the right. HTML <di ...

Verify a roster within a display by utilizing annotations

My solution involves using a listbox to display a list as shown below, and I am also utilizing Chosen.js to allow for selecting multiple records. @Html.ListBox("AllLanguages", new SelectList(ViewBag.Languages, "Id", "Language1")) <div id="languagesDi ...

Is there a way to divide all the characters within a string, excluding the sequence " "?

My issue (resolved) I've been working on an animation where each letter appears sequentially, but I encountered a problem. In order to transform the letters properly, I had to wrap my span tags in a flex div because using inline-block didn't all ...

Utilizing Async/Await with an Unassigned Object

I'm facing a puzzling issue with an async/await function that refuses to assign to an object. Despite displaying the expected results in the console, when it comes to actually assigning to the object, it fails to do so. Let me elaborate below: Here&a ...

Problem with finding Rails controller file in .coffee extension

I recently started learning Rails and came across a file called welcome.js.coffee in the assets javascripts directory. # Place all the behaviors and hooks related to the matching controller here. # All this logic will automatically be available in applica ...

Substitute the functions within a Node.js module with simulated functions

I am currently working on a Node.js project that serves as an API wrapper. To ensure the reliability of my code, I am writing unit tests using nodeunit and need to incorporate mock functions into the module. For instance, I require a function that mimics s ...

Ensuring correct association of values to avoid redundancies

There are 5 fields available for users to fill out on this form: Leave Code, From Date, Input Time1, To Date, and Input Time2. These variables are declared as a dates object in the .ts file, as shown below. interface Supervisor { name: string; code: s ...

Having trouble getting items to align properly in Bootstrap

As a student, I am struggling with my development skills while attempting to create a personal website. I am encountering difficulty aligning items on my rows in bootstrap. Here is what I am trying to accomplish: 1. However, I am currently stuck at this po ...

Transferring my JavaScript variable to PHP through Ajax

I'm currently facing an issue where my JavaScript variable is not being successfully passed to a PHP variable using AJAX in order to update my SQL database. The function is being called, but for some reason the data is not being sent to PHP.php. UPDA ...