When a key is pressed, apply a background color and fade out effect using JavaScript

Whenever the enter key is pressed, I want to make a red color appear briefly and then fade out quickly to create a blinking effect.

I attempted adding a new class on Keypress that would transition the opacity to 0:

function enterpressalert(e, text) {
  var code = (e.keyCode ? e.keyCode : e.which);
  if (code == 13) {
    document.getElementById('body').className = "show";
  }
}
    #body {
      background-color: rgb(175, 30, 30);
      opacity: .25;
      transition: opacity 0.4s ease-in;
      -ms-transition: opacity 0.4s ease-in;
      -moz-transition: opacity 0.4s ease-in;
      -webkit-transition: opacity 0.4s ease-in;
    }

    #body.show {
      opacity: 0;
      transition: opacity 0.4s ease-out;
      -ms-transition: opacity 0.4s ease-out;
      -moz-transition: opacity 0.4s ease-out;
      -webkit-transition: opacity 0.4s ease-out;
    }
<body id="body" onKeyPress="enterpressalert(event, this)></body>

Answer №1

Welcome here:

function keyPressedFunction(evt, text) {
  var key = evt.keyCode || evt.which;
  if (key == 13) {
    document.getElementById('body').style.animationName = "alert";
    setTimeout(function() {
      document.getElementById('body').id = "newBodyID";
      setTimeout(function() {
         document.getElementById('newBodyID').id = "body";
         document.getElementById('body').style.animationName = "noAnimation";      
      }, 4000);
    }, 100);
  }
}
    #body {
      background-color: rgb(175, 30, 30);
      opacity: .25;
      animation-name: noAnimation;
      animation-duration: 0.1s;
    }

    #newBodyID {
      background-color: yellow;
      opacity: .25;
      animation-name: phaseTwo;
      animation-duration: 4s;      
    }

    @keyframes alert {
       from {background-color: yellow}
       to {background-color: rgb(175, 30, 30);;}
    }

    @keyframes phaseTwo {
       from {background-color: yellow;}
       to {background-color: rgb(175, 30, 30);}
    }
<body id="body" onkeypress="keyPressedFunction(event, 'TEXT?')">
  
  </body>

Answer №2

You have the ability to change colors in a unique way. Instead of relying on opacity, try using background-color: rgba(

body.onkeydown = checkKey;

function checkKey(e) {
e = e || window.event;
    
if (e.keyCode == 13) {
   document.getElementById('body').className = "show";
   var myVar =  setInterval(function(){  document.getElementById('body').className = ""; clearInterval(myVar);}, 500);
    
  }
}
#body {
      background-color: rgba(175, 30, 30,1);
      opacity: 1;
      transition: all 0.4s ease-in;
      -ms-transition: all 0.4s ease-in;
      -moz-transition: all 0.4s ease-in;
      -webkit-transition: all 0.4s ease-in;
      
    }

    #body.show {
     background-color: rgba(175, 30, 30,0);
      transition: all 0.4s ease-out;
      -ms-transition: all 0.4s ease-out;
      -moz-transition: all 0.4s ease-out;
      -webkit-transition: all 0.4s ease-out;
    }
<body id="body" >
</body>

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

React with Three.js - Geometry is present but not displaying on screen

Seeking assistance with troubleshooting a code issue related to React. The goal is to display the sphere under the light, but it's currently not working as expected. To review the code in question, please visit this sandbox link: https://codesandbo ...

What is the best way to show a dropdown menu on the right side of the

I developed a demo that functions properly on both desktop and mobile devices. However, I am facing an issue where the dropdown menu is not displayed when switching to the mobile version. Here is my code snippet: http://codepen.io/anon/pen/OyNLqa ...

Utilize OpenLayer3 to showcase Markers and Popups on your map

I am currently exploring how to show markers/popups on an osm map using openlayers3. While I have come across some examples on the ol3 webpage, I'm interested in finding more examples for coding markers/popups with javascript or jquery, similar to som ...

An issue has occurred while trying to execute the npm start command within PhpStorm

When I try to run npm start on Windows' command prompt, it works fine. However, I encounter errors when running it in the PhpStorm Terminal. You can see the errors here. Is this a result of them being different platforms or could it be related to som ...

Sending AJAX request within a Twitter Bootstrap modal in Symfony2

After exhausting countless Google and StackOverflow search results, I have come to the conclusion that seeking help is my best option. I am currently developing a Symfony2 application. In every view of my app, I have integrated a Twitter Bootstrap modal e ...

Android Gmail Application: Implementing 1px horizontal spacing between inline images placed within <td> tags

Have you ever noticed that in the Android Gmail app, there can be a mysterious 1px gap between inline images? This little pixel problem can sometimes push the rightmost image outside the comfy frame of your email. It seems this glitch prefers images of cer ...

Library of CSS styling information

Has anyone come across a reliable javascript library that can retrieve the original style (not computed) of a specific element in the DOM? Essentially, I am looking for something that can provide results similar to what is displayed in Firebug's style ...

AngularJS directive ngShow not working properly

It seems like I'm encountering some scope issues that I can't seem to resolve. Here's a rundown of the problem: I'm trying to create a custom directive called "my-create-content" that will insert an "img" HTML template into the element ...

JavaScript: abbreviated way to selectively append an element to an array

Currently, I am in the process of creating a Mocha test for a server at my workplace. When dealing with customer information, I receive two potential phone numbers, with at least one being defined. var homePhone = result.homePhone; var altPhone = ...

How can I align an input to the right using Bootstrap?

I found an example of a navbar that I liked and decided to copy it. Here is the code snippet: <div class="container"> <h3>Nawigacja zamknieta w kontenerze</h3> <nav class="navbar navbar-toggleable-md navbar-light bg-faded"> < ...

Rearranging components in React does not automatically prompt a re-render of the page

I'm currently working on a project to display the update status of my Heroku apps. The issue I encountered was that the parent component (Herokus) was determining the order, but I wanted them sorted based on their update dates starting from the most r ...

Twig: A guide to showcasing HTML content within block titles

Within my Symfony2 project, I am utilizing various twig templates. One of these templates contains a block labeled title. {% block title %}{{ announcement.title }}{% endblock %} The issue arises when the variable {{ announcement.title }} contains HTML ta ...

Discover the best correlation among multiple arrays

I am currently developing a chat script that allows users to specify their interests. Upon connecting to the server, the client sends a JSON payload over WebSocket containing information such as ID, hash, auto message, and interests. {"id": int, "hash": m ...

Discovering duplicate values in a JSON object using JavaScript (JQuery)

Below is a JSON object that contains information about various materials: [ { "idMaterial": "Alloy 450 (15Cr6Ni1.5Cu)_S45000", "tipoMaterial": "Alloy 450 (15Cr6Ni1.5Cu)", "uns": "S45000", "temperatura": "NL", "p ...

Tips for transforming code with the use of the then block in javascript, react, and cypress

In my code snippet below, I have several nested 'then' clauses. This code is used to test my JavaScript and React code with Cypress. { export const waitForItems = (retries, nrItems) => { cy.apiGetItems().then(items => { if(items ...

Clicking on a JQuery dropdown menu will always result in it staying open

After creating a dropdown menu click, I noticed some strange behavior. When I click the dropdown button, the menu appears as expected. However, if I move my cursor away without clicking the button again, the dropdown menu disappears and behaves like a hove ...

The error message for ExpressJS states: "Headers cannot be set after they have already been sent."

Currently, I'm facing a challenge with ExpressJS and am having trouble finding the necessary documentation to resolve it. Technology Stack: body-parser: 1.17.0 express 4.15.0 multer: 1.3.0 MongoDB Postman The current view consists of 3 fields: n ...

The sequence of Angular directives being executed

When multiple directives are applied to an element in AngularJS, what determines the order in which they will be executed? For instance: <input ng-change='foo()' data-number-formatter></input> Which directive, the number formatter ...

React - The ._id received by the Modal inside the map function is incorrect

My map is generating edit and delete buttons. The delete button requires confirmation, so I implemented a modal. When I use console.log(additive._id) on the first delete button, I get the correct ._id, but when I click the confirm button inside the modal, ...

Stylesheets may not display correctly in Firefox and IE

Just wanted to say that I recently put together this little website , but I am completely stumped on why the CSS file won't show up in Firefox or IE. I know it's a basic question, but I'm still learning all of this. Thank you so much for a ...