Styling an element with CSS through JavaScript manipulation

Welcome to my first post here! :)

So, I'm currently working on a project for my college where I need to create "task notes" that fade in each time I add one to an array.

.note_input {
    height:255px;
    width:200px;
    background-image:url('../img/notebg.png');
    padding-top:25px;
    padding-left:10px;
    display:inline-block;
    animation: fadein 2s; 
    -webkit-animation: fadein 2s; 
}
    @keyframes fadein {
        from {
            opacity:0;
        }
        to {
            opacity:1;
        }
    }
    @-webkit-keyframes fadein { 
        from {
            opacity:0;
        }
        to {
            opacity:1;
        }
    }

That's the CSS for the note design.

Now, here is my JavaScript code:

function Note(){
    notes_p = JSON.parse(notes_j);
    var note_d = "<div>";
    for (var i = 0 ; i < notes.length ; i++) {
        note_d += "<span class='note_input'>" + "<div class='flow' ><p>"+notes[i].input+"</p></div></br>" + "</br>" +"Time:  "+ notes[i].time + "</br>" +"Date:  "+ notes[i].date;
        note_d +=  "</span>";
        };
    note_d += "</div>";
    document.getElementById("note_div").innerHTML = note_d;
    console.log(notes_p);
}

However, I am facing an issue where each new note fades in, causing the entire array to run over. Any suggestions on how I can make only a single note appear in fade each time without interfering with the others?

Answer №1

Visit this link to see the example in action

Forget about IDs, what really matters is applying animations to notes using JavaScript dynamically. It's important not to have any pre-applied animations on the note initially.

To add an animation programmatically, you can use code like this:

ele.style.animation = "animation duration etc...";

If your animation needs a default parameter for its starting point, you can skip that part. For instance, in the CSS provided below, only the 'to{}' is used as 0 opacity is already set as a default by the css.

Here's a snippet of the CSS:

.note {
  opacity: 0;
  transition: all .3s ease-in-out;
}

@keyframes fadein {
  to {
    opacity: 1;
  }
}

And in JavaScript:

 let arr = [].slice.call(document.querySelectorAll('.note'));

for (let i = 0; i < arr.length; i++) {
  setTimeout(function() {
    arr[i].style.animation = "fadein 2s";
  }, 500 * i);
}

The provided example at this link demonstrates how the notes are staggered. Hopefully, this aligns with what you were looking for.

Answer №2

Instead of recreating the entire list with .innerHTML, it's more efficient to use

HTMLElement.appendChild(HTMLElement)
. This way, the browser won't have to restart the animation on all note items each time an update is made.

Here's an example...

var newNote = document.createElement('div');
newNote.innerHTML = note_d;
document.getElementById("note_div").appendChild(newNote);

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

How can I fill the data array of arrays using an Angular Table component?

I am struggling to populate an Angular Table with data in the array of arrays format. Despite my efforts, the code I have written does not seem to work. Any suggestions for a solution would be greatly appreciated. Here is the data that I am trying to popu ...

Tips on handling a JSON string alert

Can someone guide me on how to extract a specific value from a JSON string I received in an alert message? The JSON string looks like this: {"Error":true, "data":["not available","somethinghere"]} The JSON string is obtained from an alert using the follow ...

Add a filter and set a background color on the rows in jqGrid

After analyzing the code and output in firebug, I can filter the td and assign a different background color based on certain conditions. Here is the code snippet: loadComplete: function() { var i, names=this.p.groupingView.sortnames[0], l = n ...

What is the functionality of the CSS switch button?

I'm curious about how the cool turn on/off switch button actually works. Take a look at for reference. I'm interested in implementing a prevention check to confirm whether the user truly wants to switch the button. Here's a snippet of the ...

A <div> with 100% height nested within a full-page div positioned at top:0 and bottom:0

Here's a simple problem. I need a full-height div (#inner) inside another full-height div (#outer, but with padding). This code displays correctly in Firefox and IE8, but not in IE7 and IE6. Edit: In the specific context where I'm using this s ...

Issues with Functioning of Control Buttons in Bootstrap 4 Carousel

Can someone please help me troubleshoot an issue with Bootstrap 4 carousel controls? I've been trying to figure it out on my own for about an hour, but I'm stumped. Any assistance would be greatly appreciated! I've followed the Bootstrap do ...

Unable to initiate script on dynamically appended element

I am facing a similar issue as described in this post: Click event doesn't work on dynamically generated elements , however, the solution provided there did not work for me since the post is a few years old. My problem involves an image carousel that ...

Creating an HTML Menu in PHP by Parsing JSON

Managing a website involves constant updates and improvements. Recently, I started using DataTables, but faced timeout issues with large HTML tables. To resolve this, I decided to switch to server-side processing using JSON. Despite being new to JSON, I&ap ...

Changing Axios requests to send data as a JSON objectDo you need to know how

Looking at the title, there is a scenario where you execute a axios.post('/user', { firstName: 'Fred', lastName: 'Flintstone' }) .then(function (response) { console.log(response); }) .catch(function (erro ...

What is the best way to extend this dropdown menu to span the entire width of the page?

My Bootstrap 5 megamenu needs all the dropdown menus to span the entire page width and be in the same position, similar to this page. How can I make the dropdown menus larger? Which Bootstrap class should I modify? I've attempted to make changes, bu ...

Executing jQuery function after the .load() method has completed

I have a script that should trigger an action once my load() request is completed, but for some reason, the alert is not appearing. The load() request is functioning properly as I can see the content of mypage.html in the .haha div. Here is the simple code ...

Steps for installing an npm package from a downloaded folder

In the past, I had a method of installing an npm project from Github that involved using git clone followed by npm install. git clone http...my_project npm install my_project Instead of manually copying the contents of my_project to my local node_modules ...

Storing multiple arrays with Mongoose rather than just one array

I have encountered an issue while trying to save an array on MongoDB using mongoose. Below is my model setup: let itemFields = { "name": { "type": "String", "required": true, "unique": true } }; let modelFields = { "name": { "type": "String", "requir ...

Executing a function without using the eval() function

I am currently working on a JavaScript code that relies heavily on the eval function. eval(myString) The value of myString is equal to myFunc(arg), and I would like to find a way to call myFunc directly instead of using eval. Unfortunately, I have no co ...

Creating a design that can adapt to different screen sizes by utilizing CSS

On standard computer screens, my div has a height of 100%. However, for mobile screens, I would prefer not to have the height set at 100%. Is there a way to remove the height: 100% that was originally set for regular screens using CSS media queries? ...

The session variable is not accessible in an AJAX function

Currently, I am working on a PHP project where I am inserting values into a database. To achieve this, I am utilizing the Ajax method. The process involves calling an Ajax function on button click, which then triggers another PHP file responsible for the ...

implementing a like button next to every item on a list using ajax

Help needed: How can I add a like button next to each item on my 'destinations' list? I'm struggling to figure out the placement. Any suggestions? Below is the content of my dashboard.html.erb file: <div class="destination-list"> ...

Is it possible to configure Express.js to serve after being Webpacked?

I am currently in the process of setting up a system to transpile my Node server (specifically Express) similar to how I handle my client-side scripts, using webpack. The setup for the Express server is quite straightforward. I bring in some node_modules ...

Opt for utilizing local data table files instead of relying on a content delivery network (CD

I'm in the process of minifying the CSS in my project and am looking to replace CDN files with offline files. <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/r/bs-3.3.5/jq-2.1.4,dt-1.10.8/datatables.min.css"/> <link h ...

Tips for integrating highcharts plugins with highcharts-vue (highcharts vue wrapper)

Currently, I am utilizing the Vue wrapper for Highcharts called highcharts-vue(https://github.com/highcharts/highcharts-vue). However, I am facing an issue where I need to detect the event of a right mouse click (contextmenu) on the chart. To address this, ...