Create a JavaScript function to implement a fade-in effect without relying on

Here is the code snippet that I am working with:

document.getElementById('showRtb').addEventListener('click', function () {
    document.getElementById('rtb').style.display="inline-table";    
});

document.getElementById('hideRtb').addEventListener('click', function () {
    document.getElementById('rtb').style.display="none";    
});

I am now looking to achieve a FadeIn animation effect using just JavaScript, without relying on jQuery or CSS3. Is such a task achievable?

Answer №1

To achieve this effect, you can utilize the functions setInterval() and getComputedStyle(). For more detailed information, refer to TheAnimationinterface

var box = document.getElementById("box"),
  timer = null;

document.getElementById("showBox").addEventListener("click", function() {
  if (box.style.opacity != 1) {
    clearTimeout(timer);
    box.style.display = "inline-table";
    timer = setInterval(function() {
      box.style.opacity = +box.style.opacity + .10;
      if (+getComputedStyle(box).getPropertyValue("opacity") >= 1) {
        clearInterval(timer);
      }
    }, 100)
  }
});

document.getElementById("hideBox").addEventListener("click", function() {
  if (box.style.opacity != 0) {
    clearTimeout(timer);
    timer = setInterval(function() {
      box.style.opacity = +box.style.opacity - .10;
      if (+getComputedStyle(box).getPropertyValue("opacity") <= 0) {
        box.style.display = "none";
        clearInterval(timer);
      }
    }, 100)
  }
});
#box {
  width: 100px;
  height: 100px;
  background: olive;
  opacity: 0;
  display: none;
}
<button id="showBox">show</button>
<button id="hideBox">hide</button>
<br>
<div id="box"></div>

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

The list is not responding correctly to the navbar-right functionality

Although I'm new to bootstrap, I am trying to create a navbar at the top of my webpage. The navbar should feature the brand name on the left and options on the right side. However, when using navbar-right, it does not align properly resulting in a lay ...

issue with animation occurs when the value changes while the setTimeout timer is still running

function wallpaperMediaPropertiesListener(event) { // reset animation test.stop(true); test.css('margin-left', 0); // set song title. test.text(event.title); var testWidth = test.outerWidth(); // clone text ...

What is the most optimal method for converting an untyped array of 32-bit integers into a UInt8Array?

I have a standard JavaScript array filled with valid 32-bit signed integers that I need to convert into a UInt8Array. Take for example this JavaScript array: [255, 3498766, -99] The resulting UInt8Array should display the signed 32-bit representation of ...

Encountering issues with incorporating jquery functions into Ember.js

This piece of code was added to the index.ejs file: <script type="text/javascript> $(window).load(function() { $('#username').glDatePicker(); }); </script> However, the jQuery code only functions on the IDs of the ...

Hiding an element in JavaScript when the onfocus event occurs is not possible

I'm having trouble hiding a div element when clicking on a text box using JavaScript. I've tried the following code, but it's not working. Can anyone spot the error in my program and suggest a better solution? <html> <head> ...

When running the command `npm run prod`, production mode is not activated in Vue.js

Currently, I am working on a project that involves Laravel and Vuejs. As part of the process, I tried to publish my website, but I keep encountering this particular message in the browser console. The message states that Vue is currently running in deve ...

How to represent a JavaScript string with a new line character using JSON.stringify without the escape sequence displaying

I am currently working on converting a string to JSON in JavaScript and then sending the converted string to an HTML textarea element. The original string from the backend looks like this: "customBar_query": "select\\n\\neq.Name,\ ...

Vue JS console displays an error stating "the <li> tag is missing a closing tag."

Below is the code snippet I am currently using to change the color based on the character's name: I encountered an error indicating that the li tag was not properly closed, although it appears to be closed. However, there seems to be an issue during ...

Is there a way to use findByIdAndUpdate() without inadvertently removing any existing data?

Imagine a scenario where there is a mongoose Person model structured as below: const PersonSchema = new mongoose.Schema({ name: String, address: { street: String, number: number } }); Now, consider a method designed for updati ...

Problem with the menu button overflowing

I'm exploring the process of designing burger menus for the mobile version of my site. I've successfully created a basic burger menu with some JavaScript assistance, but I encountered an overflow issue. The burger menu functions properly, but the ...

How to load several Collada objects in THREE.js

When loading multiple car models using a loop in THREE.js, I've encountered an issue where sometimes all objects load correctly, but other times not all of them load. For instance, if the loop iterates 3 times, it may only load 2 objects at times, whi ...

Is it possible to determine the height of a div after it has been rendered using the .resize method?

In my current structure, <div id="A"> <div id="A1"> <div id="B1"></div> <div id="B2"></div> </div> <div id="A2"></div> </div> A2 and B2 contain tables, while B1 has 4 checkboxes. I&a ...

Looking for a substitute for iframes when using jQuery UI tabs?

I currently have a Spring-based web application that integrates with jQuery Tabs. What I'm doing is building a data string with specific parameters and appending it to a URL: var hrefData = "?" + item1 + "&" + item2 + "&" + item3; var href = ...

The language is being detected, but the translation feature is not functioning properly with i18n

I've configured the i18n middleware in my Express Node js server as follows: // server.js import i18nMiddleware from 'i18next-express-middleware'; import i18n from 'i18next'; import Backend from 'i18next-node-fs-backend'; ...

Tips on verifying the presence of an image with chai or mocha?

Is it possible to verify the existence of an image using Chai or Mocha? For instance, when checking the availability of https://upload.wikimedia.org/wikipedia/commons/5/51/Google.png ...

Creating static pages with dynamic nested routes is a powerful feature that can enhance the structure

I've been working on a website and I've encountered a problem. Essentially, I need to display movies by genre from a database. When a user clicks on a genre, it should take them to another list of movies in that genre. Then, when they click on a ...

Uploading files using C# and data streaming

I am exploring the various methods of file uploading in .NET, such as using HttpPostedFile and a HttpHandler. I want to delve into the technical details of how this process actually works. Specifically, I am curious about how the data is written to a file ...

Any ideas on how to present data in a card layout with two columns?

.card { box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2); transition: 0.3s; } .card:hover { box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2); } .container { padding: 2px 16px; } .card { width: 50%; heig ...

Creating a mongoose query to utilize the $all operator within an array

Consider this dataset: [{ "users": [{ "name": "one" }, { "name": "two" }] }, { "users": [{ "name": "one" }, { "name": "three" }] }, { " ...

Using one payload.js file for all Nuxt static generated routes with identical data

I am facing a challenge with my NuxtJS site where I have only one page /pages/matrix/index.vue but numerous dynamic routes pointing to this page, all utilizing the same data set. During static build generation for deployment on Netlify, the size of the dis ...