DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Subtitles: The Good, the Bad, and the Resource-Heavy
  • Loader Animations Using Anime.js
  • Next.js Theming: CSS Variables for Adaptive Data Visualization
  • Creating Scrolling Text With HTML, CSS, and JavaScript

Trending

  • Creating a Web Project: Caching for Performance Optimization
  • Cosmos DB Disaster Recovery: Multi-Region Write Pitfalls and How to Evade Them
  • How to Merge HTML Documents in Java
  • Building an AI/ML Data Lake With Apache Iceberg
  1. DZone
  2. Coding
  3. Languages
  4. How To Do Deletion Animations With CSS

How To Do Deletion Animations With CSS

In this tutorial, we're going to be looking at a bunch of different 'delete' styles to use on your next project. Let's look at how to create a set of delete animations.

By 
Johnny Simpson user avatar
Johnny Simpson
DZone Core CORE ·
Dec. 15, 21 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
6.3K Views

Join the DZone community and get the full member experience.

Join For Free

In this quick tutorial, we're going to be looking at a bunch of different 'delete' styles to use on your next project. So let's take a look at some cool ways to delete things in your next project. Below are a set of delete animations that all work on similar principles. Let's take a look at them and then we'll jump into how they work. 

To start the animation, simply click the cross button on each card in the Codepen below.

How To Make UI Animated Deletion Effects

So although all the animations above are based on CSS principles, we need a little bit of Javascript to kick start them. The key Javascript function we need is an event for whenever the user clicks the cross button. First off, let's make our card in HTML. Notice that we give the delete button an attribute called data-delete. This is how we track the type of animation to run.

<div class="item">
    <div class="image"></div>
    <div class="delete" data-delete="zoom"><i class="far fa-times"></i></div>
    <div class="text">
        <h2>Zoom Delete</h2>
        <p>Click the cross above to delete this item..</p>
    </div>
    <div class="animation-assets"></div>
</div> 


Next, let's make our Javascript function to give animation to our cards. Although this looks a little bit more complicated, all this function does is add the text from data-delete in our HTML to the class of the .item div. If the animation is the shredder, we have to do a little bit more too:

  • First, we add 10 or so new divs to the .animation-assets div. We clip each of these so they look like shredded pieces of paper.
  • We then delay the animation a little for each, to give variety to each slice - and we alternate between two animations in our CSS - so we give different slices different animation names.
document.querySelectorAll('.delete').forEach(function(item) {
    item.addEventListener('click', function(e) {
        // First we get the 
        let newClass = this.getAttribute('data-delete');
        let getParent = parent(this, '.item', 1);
        if(newClass === 'shredder') {
            getParent.classList.add('shredding');
            // Shredder animation
            // Slices
            let shredAmount = 10;
            let width = document.querySelector('.item.shred').getBoundingClientRect().width / shredAmount;
            let animationName = 'spinALittle';
            let animationDelay = 0;
            for(let x = 0; x <= shredAmount; ++x) {
                animationDelay += 1;
                if(x % 2 === 0) {
                    animationName = 'spinALittleAlt';
                } else {
                    animationName = 'spinALittle';
                }
                if(x % 3 === 0) {
                    animationDelay = 0;
                }
                let newEl = document.createElement('div');
                newEl.classList.add('item-wrap');
                newEl.innerHTML = `<div class="item">${getParent.innerHTML}</div>`;
                newEl.querySelector('.animation-assets').innerHTML = '';
                let clip = `clip-path: inset(0px ${(shredAmount - x - 1) * width}px 0 ${(x * width)}px); animation-delay: 0.${animationDelay}s; transform-origin: ${x*width}px 125px; animation-name: ${animationName};`
                newEl.children[0].setAttribute('style', clip);
                getParent.querySelector('.animation-assets').append(newEl);
            }
        } else {
        getParent.classList.add(newClass);
        }
    });
}); 


CSS for Delete Animations

Next up, let's look at our CSS. To create the animations, we use keyframes. First up, we add animations to each of our .item divs, based on the type of animation we want to show.

.item.zoom {
  animation: zoom forwards 0.7s ease-out 1;
}

.item.shredding {
   animation: reduceWidth forwards 1.7s ease-out 1; 
}

.item.fall {
    animation: fallAway forwards 1s ease-out 1;
}
.item.analogue {
    animation: analogue forwards 1s ease-out 1;
}
.animation-assets > .item-wrap > .item {
    animation: spinALittle 1.4s ease-out 1 forwards;
    perspective: 1000px;
    position: absolute;
    top: 0;
    left: 0;
}
.item-wrap {
    animation: dropShadow 0.1s ease-out forwards 1;
}


Then we make our animations. These do just a few things:

  • First, they do the main animation, which is usually some kind of transform
  • Then they reduce the padding, width, and margin to zero. This means items are removed smoothly from the card order.
  • Finally, we usually reduce the opacity to 0 so the items are no longer visible.

An example of the animation we use for the shredder is shown below:

@keyframes fallAway {
  0% {
    transform: rotateZ(0deg);
    top: 0;
    opacity: 1;
  }
  25% {
    transform: rotateZ(-15deg);
  }
  100% {
    top: 300px;
    transform: rotateZ(-5deg);
    opacity: 0;
  }
}


If you want to view the full code, you can do so via the codepen linked here. These delete animation styles were pretty fun to do - and there's lots more you could do with them. If you come up with any cool ones, let me know in the comments below or on Twitter!

CSS

Published at DZone with permission of Johnny Simpson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Subtitles: The Good, the Bad, and the Resource-Heavy
  • Loader Animations Using Anime.js
  • Next.js Theming: CSS Variables for Adaptive Data Visualization
  • Creating Scrolling Text With HTML, CSS, and JavaScript

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!