Unable to make jQuery animate top function work

I have three rectangles set up like this. I've managed to make them disappear when clicking the close button on the right side of each rectangle, but I'm struggling with getting the remaining rectangles to move upward to fill the empty space. Here's the code I've tried so far:

$(".note-content-right").click(function() {
  $(this).parent().removeClass("note-float-view");
  var thisDataIndex = $(this).parent().attr("data-index");
  $(".note-float").each(function() {
    if(($(this).hasClass("note-float-view")) && ($(this).attr("data-index") > thisDataIndex)) {
      $(this).animate({
        "top" : "-=54px"
      });
    };
  });
});

Unfortunately, it's not working as expected.

If anyone can provide some assistance, that would be greatly appreciated! :)

$(document).ready(function() {
  $(".note-float").removeClass("note-under");
  loadNote();
});

function loadNote() {
  $(".note-float").each(function(index) {
    var el = $(this); 
    setTimeout(function () {
      el.addClass("note-float-view");
    }, index * 200);
  });
}

function unloadNote() {
  $(".note-float-view").each(function(index) {
    var el = $(this); 
    setTimeout(function () {
      el.removeClass("note-float-view");
    }, index * 200);
  });
}

$(".note-content-right").click(function() {
  $(this).parent().removeClass("note-float-view");
  var thisDataIndex = $(this).parent().attr("data-index");
  $(".note-float").each(function() {
    if(($(this).hasClass("note-float-view")) && ($(this).attr("data-index") > thisDataIndex)) {
      $(this).animate({
        "top" : "-=54px"
      });
    };
  });
});

$(".load-note").click(function() {
  $(".note-float").removeClass("note-under");
  $(".note-float").removeClass("note-float-view");
  setTimeout(function() {
    loadNote();
  }, 500);
});
.note-float-view {
  top: 24px !important;
  opacity: 1 !important;
  transition: top 1s, margin-bottom 1s, opacity 1s;
}

(note styles and classes here)

</script>

Answer №1

The styling of .note-float-view with top: 24px !important; is causing a conflict with the animation. Removing the !important declaration resolves this issue.

In addition, I have made modifications to the 'reset' function to ensure that all items return to a top position of 24px instead of wherever the animation may have left them. It might be more effective to handle this adjustment on individual items at the conclusion of the animation process.

$(document).ready(function() {
  $(".note-float").removeClass("note-under");
  loadNote();
});

function loadNote() {
  $(".note-float").each(function(index) {
    var el = $(this);
    setTimeout(function() {
      el.addClass("note-float-view");
    }, index * 200);
  });
}

function unloadNote() {
  $(".note-float-view").each(function(index) {
    var el = $(this);
    setTimeout(function() {
      el.removeClass("note-float-view");
    }, index * 200);
  });
}

$(".note-content-right").click(function() {
  $(this).parent().removeClass("note-float-view");
  var thisDataIndex = $(this).parent().attr("data-index");
  $(".note-float").each(function() {
  if (($(this).hasClass("note-float-view")) && ($(this).attr("data-index") > thisDataIndex)) {
    $(this).animate({
     "top": "-=54px"
   });
 };
});

$(".load-note").click(function() {
  $(".note-float")
    .removeClass("note-under")
    .removeClass("note-float-view")
    .css("top","24px");

  setTimeout(function() {
    loadNote();
  }, 500);
});
.note-float-view {
  top: 24px ;
  opacity: 1 !important;
  transition: top 1s, margin-bottom 1s, opacity 1s;
}

/* Additional CSS styles included here for reference... */

@media (max-width: 575px) {
 /* Responsive CSS styles added here */
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.11/css/all.css" integrity="sha384-p2jx59pefphTFIpeqCcISO9MdVfIm4pNnsL08A6v5vaQc4owkQqxMV8kg4Yvhaw/" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/css?family=Open+Sans:400,400i,700,700i" rel="stylesheet">

<div class="note-wrapper">


  <div class="note note-float success" data-index="1">
    <div class="note-content-left">
      <i class="fas fa-check-circle"></i>
    </div>
    <div class="note-content-center">
      You have successfully added <b>1 item(s)</b>
    </div>
    <div class="note-content-right">
      <i class="fas fa-times close-success"></i>
    </div>
  </div>

  /* More HTML content for different types of notes... */

  <div class="load-btn-wrapper">
    <a class="button white-btn footer-btn load-note">Reload </a>
  </div>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

A point to consider is that using a fixed pixel value like 54px for animating position may cause layout issues when text wraps across lines. It's advisable to calculate the height dynamically and animate based on that to avoid such problems. Avoid hardcoded pixel values and reliance on absolute positioning as much as possible, as they can lead to manual adjustments like in this case. For smoother transitions, leveraging document flow and avoiding absolute positioning can simplify your workflow. One alternative approach could be animating max-height to zero with hidden overflow-y to remove elements without needing manual repositioning.

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

During the initial cycle, the CSS keyframes animation may experience glitches, but it will run smoothly in subsequent cycles

Seeking advice on troubleshooting a CSS keyframes animation issue. I have created an animation with 5 frames where the background image fades and transitions to the next image smoothly in all cycles, except for the first cycle where it glitches before each ...

Creating a mechanism that fetches web links and automatically substitutes the URL with the corresponding title of the webpage

Just the other day, I noticed that putting a link in my profile resulted in it being replaced by the exact title of the question. This got me thinking, how exactly do they accomplish this? My hunch is that they achieve this by using ajax to send the link ...

Looking to expand the width of the sub menu to reach the full width of the

Is there a way to use CSS to make a sub-menu start from the left side of the screen instead of starting it below the parent item? nav { margin: 0 auto; text-align: center; } nav ul ul { display: none; } nav ul li:hover > ul { di ...

Removing the gridlines in a Linechart using Apexcharts

I am experiencing issues with the grid and Nodata options on my Apexchart line chart. noData: { text: none , align: 'center', verticalAlign: 'middle', offsetX: 0, offsetY: 0, style: { color: undefined, fontSize: &apo ...

Managing numerous range sliders in a Django form

My Request: I am looking to have multiple Range sliders (the number will change based on user selections) on a single page. When the sliders are moved, I want the value to be updated and displayed in a span element, as well as updating the model. The Issu ...

"Efficiently fetch data with an Express GET request without the

My goal is to send the client the HTML page (create.html) in response to a GET request triggered by a button click using fetch. I am intentionally avoiding the use of a form due to formatting and potential scalability issues. The code acknowledges that the ...

Basic use of AJAX for sending the value from jQuery's datepicker

As a novice in JavaScript and AJAX, I'm hoping for your patience as I navigate my way through. In the scope of this project, I am utilizing the CodeIgniter framework. My objective is to implement a datepicker and transmit its value via AJAX. Below is ...

Utilizing media queries in AMP for optimizing mobile website performance

Recently, I delved into the realm of utilizing AMP for my blog. After perusing the documentation and observing examples of its application, I must say that anything enhancing performance is a welcome addition to me. Therefore, I am keen on incorporating i ...

Issue with the functionality of Jquery scrolling through thumbnail images animation

I have encountered an issue related to the previous question that I posted here. After successfully implementing the provided solution, it initially seemed to be working. However, when I clicked the next button and reached the 3rd click, the jQuery scrolli ...

Shifting the form to the bottom right corner

Just starting out with HTML5 and CSS, I've been experimenting with different elements. One project I've been working on is a form: <div id="form1"> <form action="demo_form.asp" autocomplete="on" > Departure City & ...

Is it possible to add a bottom border without having slanted corners?

I am looking to design a box with different colors - red on the left, right, and top, and grey at the bottom. However, I want the bottom border of the box to be flat. HTML <div class="ts"></div> CSS .ts { height:100px; width:100 ...

How can you confirm the validity of a dropdown menu using JavaScript?

<FORM NAME="form1" METHOD="POST" ACTION="survey.php"> <P>q1: How would you rate the performance of John Doe? <P> <INPUT TYPE='Radio' Name='q1' value='1' id='q1'>1 ...

What is the most effective method for incorporating parameterized aesthetics using CSS in a JSP program?

I am currently working on a JSP web application without the support of any framework, unfortunately. This application is intended to serve multiple customers, each with their unique set of colors and company logos. The overall layout of the CSS will remai ...

jQuery AJAX Live Search Function Only Executes Once

Here is a code snippet for a live search functionality: <script> $(".search").keyup(function() { var Team_Name = $('#TeamName').val(); var Teacher = $('#Teacher').val(); var Searc ...

Divide HTML content while keeping inner tags intact

Currently, I am developing an online store. In my code, there is a requirement to display attributes and descriptions for multiple products on a single page. The attributes are displayed in a table format, while the description can include simple text as w ...

The issue with CSS filter invert not functioning properly in Mozilla Firefox is causing complications

I am currently developing a small Firefox add-on designed to make reading pages at night more comfortable. The goal is to invert page colors without affecting images. Here is the code snippet I am using: document.body.style.filter = "invert(100%)"; var i ...

Extracting text from an HTML file and passing it to an Express.js server: A beginner

Currently, I'm attempting to retrieve the values from an HTML text field and store them in variables. I require HTML to capture these values and return the response within the .html file. HTML: <body> <form> ...

Customizing Bootstrap css variables for a unique design

Currently, I'm working on tweaking the data-bs-theme in Bootstrap 5.3. My goal is to adjust the pre-defined CSS variables of the Bootstrap dark theme and refresh the color palette. Specifically, I want to change the shade of blue for my blue button (b ...

"The issue of a Cordova app experiencing timeouts while attempting to connect to a local server via

I've been searching tirelessly to find a solution to this issue, but so far I've had no luck. My problem involves trying to establish a connection between a Phonegap app and a server hosted on my local machine, but every time it times out. I foll ...

New design in TinyMCE V5: Vertical toolbar labels arranged within a CSS grid layout

When TinyMCE version 5 is placed inside a CSS grid layout, the toolbar labels are displaying vertically. It appears that the "width:auto" property is not being applied correctly, and I am struggling to find a solution to fix it. I would greatly appreciat ...