How to use CSS animations to remove an element from the DOM

My website has a structured HTML layout with product containers and individual products. I also have a JavaScript function that can remove any product from the page.

<div class="products-container">
    {foreach from=$cart.products item=product}
  <div class="product"     data-id-product="{$product.id_product}" data-id-product-attribute="{$product.id_product_attribute}">
   ...
  </div>
</div>

I am looking for a way to enhance the user experience by fading out the deleted product div as it moves to the right, while animating the other products to fill in the empty space.

Answer №1

An easy illustration

const button = document.querySelector('button');
const productsContainer = document.querySelector('.products');
const productsList  = document.querySelectorAll('.product');

productsList[0].addEventListener('transitionend', function() {
  [...productsList].forEach((product) => product.parentNode.removeChild(product))
});

button.addEventListener('click', function() {
  this.setAttribute('disabled', 'disabled');
  productsContainer.classList.add('products--delete');
})
div {
  border: 1px #9bc solid;
  height: 60px;
  width: 200px;
  margin: 10px;
  color: #69A;
  font: 1em system-ui;
}

button {
  margin-bottom: 3em;
  cursor: pointer; }

.products {
  overflow-x: hidden; }

.product {
  font-weight: bold;
  transition: transform 1.5s 0s, opacity 1.25s 0s;
  transform: translateX(0);
  opacity: 1;
}

.products--delete .product {
  transform: translateX(100vw);
  opacity: 0;
}
<button type="button">Remove item/s</button>

<section class="products">
  <div>Not a product</div>
  <div class="product">Product</div>
  <div class="product">Product</div>
  <div>Not a product</div>
  <div class="product">Product</div>
  <div>Not a product</div>
</section>


Description: upon clicking the button, the class .products--delete gets applied to the .products_container element triggering a CSS transition on the .product items.

Subsequently, when the transitionend event is detected on an individual product item, all products are removed from the DOM.

Answer №2

Utilizing CSS transitions can enhance your design. The process of removing elements might vary based on your specific needs. Simply select the product you want to remove.

let productsRy = Array.from(document.querySelectorAll(".product"));


productsRy.forEach((p,i)=>{
  p.style.top = i * (3 + 1) * 16 +"px";
  p.addEventListener("click",()=>{
  container.removeChild(p);
  productsRy = Array.from(document.querySelectorAll(".product"));
  productsRy.forEach((p1,i1)=>{p1.style.top = i1 * (3 + 1) * 16 +"px";})
})
  
  
})
.products-container{position:relative;}

.product{
  position:absolute;
  padding:1em; 
  margin:.5em;
  height:3em;
  outline:1px solid; width:200px;
  height:auto;
  transition: all 1s;
}
}
<div class="products-container" id="container">
  <div class="product"  data-id-product="a" data-id-product-attribute="a">
  product a
  </div>
    <div class="product"  data-id-product="b" data-id-product-attribute="b">
  product b
  </div>
    <div class="product"  data-id-product="c" data-id-product-attribute="c">
  product c
  </div>
</div>

Answer №3

Although CSS lacks the capability to manipulate objects like JavaScript, you can still achieve this task effortlessly.

$(".item").fadeOut("slow", 0.00, function(){
    $(this).slideUp("slow", function() {
         $(this).remove();
    });
});

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

What is the best way to ensure my hover caption spans the entire size of the image?

I'm currently working on creating a hover caption that adjusts itself to fit the full width and height of an image even when it is resized. I attempted to implement a jQuery solution that I came across, but unfortunately, I am struggling to get it to ...

Styling with CSS: Changing the Background Color of Text

Looking for a solution to apply line-height to text without affecting the background color? Check out the code snippet below and share your ideas if you have any. Thanks! .nb-semnox-impact-grid { display: block; font-size: 6 ...

Netlify deployment is failing to display the CSS styling on a website built with Hugo

Currently in the process of creating my own website using the "Call Me Sam" hugo theme. Everything looks great locally but once deployed on Netlify, the CSS styling seems to be completely disregarded. If you want to check out my repository: https://github ...

Only the initial external CSS ID is functional, while the second one is inactive

I am facing an issue with my table where I have applied an external CSS that uses alternating shaded rows. My aim is to have specific table cells with different background and font colors - one cell with a green background and red font, and another with a ...

Is there a way to place my searchbox in the top right corner of the page?

I am currently working on creating a search function for my list of products. However, I have encountered an issue where the searchBox is not appearing in the top right corner as intended. Despite trying various solutions, I have been unsuccessful in movin ...

modify the appearance of HTML controls in real time with C#

Is there a way to dynamically add an additional navigation item to my website's menu list when the admin logs in through admin.aspx? The menu list is created using HTML controls such as <ul>...<li>, and I would like the new navigation item ...

Utilizing SASS, JavaScript, and HTML for seamless development with Browser Sync for live syncing and

I've been on a quest to find a solution that covers the following requirements: Convert SASS to CSS Post-process CSS Minify CSS Move it to a different location Bundle all Javascript into one file Create compatibility for older browsers Tre ...

Showing information to several classes using JavaScript

I am currently developing a "Gamebook Engine" that enables users to set a custom username. The user name is extracted from an input element with the id="setUserNameInput" and stored using the function setUserName(). It is then displayed or loaded into an e ...

Mouse click failing to trigger CSS property update

I am attempting to create an accordion feature. I want the accordion to expand when hovering over the "accordion head," and also show/hide the accordion body when clicking on the "accordion head." I have successfully implemented the show/hide functionalit ...

The menu field remains open even after clicking on the menu

I have encountered an issue with my code. Here is a DEMO that I created on jsfiddle.net Currently, when you click on the red div, the menu opens. However, if you click on the menu items, the menu area does not close. What do I need to do in order to clo ...

Prevent elements from collapsing within the Bootstrap 5 grid system

Hey there, happy Friday everyone! I've been working on rebuilding my company's website to learn HTML and CSS. I'm currently creating a replica of the original site and it's going really well so far. However, I've run into some is ...

Incorporate Zurb Foundation seamlessly into your current codebase without causing any

While Foundation is great for creating whole page designs, it may not be the best choice when you just need a small snippet to add into existing HTML and CSS code. In Foundation's _global.scss file, there are global settings such as: html, body { h ...

What is the best way for me to locate and access the initial element that has been assigned the

How do I reference the first element with the "myabilities" class in the code snippet below? <div class="span6"> <h3 class="srvc-title">Responsive Design</h3> <p class="srvc-detail">Crafting great experie ...

What could be causing this addEventListener to fail when I am assigning elements to a class?

I've encountered an issue with my code where I have two text inputs and two date inputs. I tried to select all of them using QuerySelectorAll with a class, added a click listener that should change the textContent of a div element to "", but it's ...

What are some effective strategies to ensure my header is aligned properly on both mobile and desktop platforms?

While I'm more focused on back end web development, I've been struggling to make my header look good on both mobile and desktop without resorting to creating separate sites for each. This approach has proven to be cumbersome as maintaining two si ...

The car glides smoothly across the screen from left to right before disappearing from view

I'm looking to create an animation where a car moves from left to right and then switches to another image moving from right to left. Can anyone provide assistance with this task? Here's the code I have so far: .car-movement { position: a ...

In HTML, a Bootstrap row appears on top of another row when viewing on mobile devices

I have implemented a Bootstrap HTML section that consists of a container and two main rows. Here is the code snippet: <link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"> <style& ...

Is it necessary to use a specific button to submit on Return (Enter)?

In a wizard-type form, there are two buttons present. The Back button is positioned on the left side, while the Next button is located on the right side. For an example of this setup, visit http://jsfiddle.net/WSwb7/1/. Please note that submitting the form ...

The vertical-align property in CSS seems to be malfunctioning

Hi there, I'm struggling with the CSS code below: .parent { position : 'absolute'; top : '50px'; left : '50px'; width : '400px'; height : '160px'; padding : '10px'; ...

Styling your printed documents in Next.js 10

Within my Next 10 application, I am able to bring in global styles by importing them in _app.js in the following manner: import 'assets/css/app.css' I want to incorporate a print stylesheet as well. Instead of using a @media print { } query with ...