Why is it that only my first div moves to the top when I try to rearrange them, while the others stay in

Picture this scenario with 3 div elements:

<div class="one animated">
    I represent the first div
</div>
<div class="two">
    second div
</div>
<div class="three">
    Last one
</div>

There is some code to move only the first div to the top:

.animated {
    animation: animation 2s;
    animation-fill-mode: forwards;
}

@keyframes animation {
    0%      { top: 0; opacity: 1 }
    100%    { top: -300px; opacity: 0 }
}

The question arises as to why the first div moves to the top while the others do not. Since the first div disappears, the solution is to replace it with the other two divs...

This can be achieved by doing something like the following:

function test() {
   document.getElementById("sample").classList.add("animated");
}
.one {
  border: 1px solid red;
}

.animated {
    animation: animation 2s;
    animation-fill-mode: forwards;
}

@keyframes animation {
    0%      { top: 0; opacity: 1 }
    100%    { top: -300px; opacity: 0 }
}
<div class="one" id="sample">
    I'm the first div
</div>
<div class="two">
    second div
</div>
<div class="three">
    Last one
</div>

<button onclick="test()">Test</button>

Answer №1

To ensure smooth movement, use absolute:position; with Keyframes. This will automatically shift the div to the top when hidden.

You can also include additional attributes in the keyframes for a seamless effect.

function animate() {
  document.getElementById("element").classList.add("animated");
}
.box-one {
  border: 1px solid red;
}

.animated {
  animation: slide-animation 2s;
  animation-fill-mode: forwards;
}

@keyframes slide-animation {
  0% {
    top: 0;
    opacity: 1;
  }
  50% {
    opacity: 0;
  }
  100% {
    opacity: 0;
    position: absolute;
  }
}
<div class="box-one" id="element">
  First box
</div>
<div class="box-two">
  Second box
</div>
<div class="box-three">
  Last box
</div>

<button onclick="animate()">Animate</button>

Answer №2

To enhance the visual effects, make sure to include position: absolute; in the .animated class within your stylesheet.

function test() {
   document.getElementById("sample").classList.add("animated");
}
.one {
  border: 1px solid red;
}

.animated {
    animation: animation 2s;
    animation-fill-mode: forwards;
    position: absolute; // Remember to add this line
    transition: all 2s linear;
}

@keyframes animation {
    0%      { top: 0; opacity: 1; }
    50%     {top: -1; opacity: 0.5;}
    100%    { top: -300px; opacity: 0; }
}
<div class="one" id="sample">
    I'm the first div
</div>
<div class="two">
    second div
</div>
<div class="three">
    Last one
</div>

<button onclick="test()">Test</button>

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

Using jquery to hide an input box once a checkbox is marked as checked

Is there a way to conceal the input box for a name when a checkbox is checked? if ($("#1step").checked){ $("input[username='true']").hide(); } <input username='true'> <input type="checkbox" id="1step"><label>ste ...

Show and hide a div with the click of a button

As I embark on rebuilding a website from the ground up, I find myself pondering how to achieve a specific functionality: My goal is for another view to appear when one of two buttons is clicked. How can this be accomplished? I attempted the following app ...

Why does the "error loading page" message appear on the server when there is a PHP function inside jQuery Mobile?

I am facing an issue with a function I created in my jQuery Mobile code. It works perfectly fine on my local machine, but when I host it on the server, it does not work properly. Can someone please assist me? Here is a glimpse of the code: <!DOCTYPE ht ...

What is the best way to achieve this Bootstrap design without copying the content?

Trying to achieve a specific Bootstrap layout without repeating content is my current challenge. Essentially, I aim to divide some content into 2 columns at the sm/md breakpoints and then switch to 3 columns for the lg breakpoint. The layout for sm/md bre ...

Enhancing password security with an Ajax form field

Looking for an ajax/javascript plugin that adds color indicators next to password fields, with each password having its own unique color combination. ...

jquery select2 not functioning properly following ajax call

I am having an issue with my select2 control on the website. Initially, everything works perfectly fine when the site loads. However, if the content of the div named "simpleDiv" where the Select2 is located, is loaded via Ajax, the Select2 fails to display ...

Can you tell me why the jquery datepicker control is only loading a portion of the CSS?

Working on a new C#/ASP.Net application and I decided to incorporate the datepicker control that I've used before. I transferred all the necessary components from my previous app to this one. The appearance of the datepicker in the old app was like th ...

Issue with Event.target not functioning properly when clicking outside the div to close it

I am facing an issue with my main div and sub div, which is referred to as a form because it is enclosed within the main div. I am trying to close the form when clicking outside of the main div, but unfortunately, this functionality is not working as expec ...

Updating a script to toggle the visibility of div elements depending on the selected options in a hierarchical drop-down menu

Currently, I have set up some dropdown menus for users to select their school year and position. Depending on the selections made, a corresponding div displays information along with another dropdown for selecting a position. When a position is selected, a ...

In bootstrap, two overlapping DIVs are displayed below each other in mobile mode

I have a setup with two divs. The initial div consists of a header bar containing navigation links and a logo. Directly below is the second div, which features a hero image accompanied by some text. These divs are nested within a header tag, with the fir ...

The issue arises when applying Tailwind styling to template variables that have widget attributes in Django

Currently, I am attempting to implement daisyUI styling in my django web application project, however, I am facing some confusion regarding the styling. The specific issue arises when I try to apply daisyUI styling to a dynamically generated input text fie ...

`Weaving mesh into place with three.js`

I'm struggling to grasp how to position my cubes on the canvas. I can't quite figure out how positioning actually works. I'm trying to find a way to determine if my mesh reaches the limits of the canvas. But what exactly is the unit of posit ...

Is there a way to develop a login form that retrieves information from a Google Sheet database?

Please help me with a solution. I have developed a registration form which effectively saves users' information in a Google Sheet. However, now I am yearning to create a login form that verifies the stored data and redirects the user to a different pa ...

Utilizing jQuery to eliminate a script function from an external webpage

Currently, I am utilizing a ColdFusion script to load an external page within the container tag. This external page contains a sorting function defined as: function sorting(sortid). However, this function's sorting criteria constantly leads to errors ...

Ensuring a top margin is applied to a flexbox item of undetermined height

Having a flexbox container with two dynamic content columns, it becomes challenging to adjust the height of the right column without affecting the alignment. Setting a top margin does not yield the desired result as the bottom remains aligned with the left ...

Use PHP to evaluate code within quotation marks on a PHP file

Need help with displaying a dynamic variable inside an HTML element within a PHP if conditional. <?php $url = $thumb['0']; if ( in_category( 'News' )) { //nothing here } else { echo '<div class="image-holder&qu ...

Converting multi-dimensional array into a structured table format

Query about arrays: Here is the array structure I am working with: Array ( [0] => Array ( [0] => Project [1] => Time ) [1] => Array ( [0] => Google [1] => 2 ...

The child and parent tables share identical borders

I am faced with a challenge involving two tables, one main and one child, both with 3 columns each. The child table is located within a row of the main table. My objective is to display these two tables as one cohesive unit with consistent borders. However ...

Basic Jquery CSS style requests

Can you help me troubleshoot this issue? var hover = $('<img />').attr('src', hovers[i]).css('position', 'absolute') I'm having trouble getting th ...

Adjusting the height of a textarea in React mui to match its parent element height

Utilizing the react mui library version 3.9.3 for UI components in my current project, I am facing an issue with a textarea field: <div className={classes.fieldWrappper}> <TextField id="filled-multiline-static" label="Oppgavetekst ...