How to remove a div with fade out effect in JavaScript?


Tags: javascript,html,css,animation,fade

Problem :

I want to remove a div element on click event but i want to remove it with a fade out effect. I have got some JQuery solution but i need pure JavaScript or css solution.

document.querySelector('.list').addEventListener("click", function(e){
    if (e.target.localName === "span") {
        var removeTarget = e.target.parentNode.parentNode;
        removeTarget.parentNode.removeChild(removeTarget);
    };
});

This code is removing the div element with no effect. How can i add a fade out effect?



Solution :

There are two ways you can achieve this: CSS3 animation or jQuery animation.

CSS3 Animation

  1. In your CSS document, add:

    .list {
        opacity: 1;
        -webkit-transition: opacity 1000ms linear;
        transition: opacity 1000ms linear;
    }
    
    • This will make any change of opacity to your item fade by 1000ms.
  2. Change line 4 of your JavaScript to:

    removeTarget.style.opacity = '0';
    setTimeout(function(){removeTarget.parentNode.removeChild(removeTarget);}, 1000);
    
    • This will make your item change opacity to 0, thus making the transition from step 1 have an effect. Then it will remove the item with your code after 1000ms.

Note: Make sure the time of the CSS3 transition and the setTimeout are the same.

jQuery Animation

  1. Get jQuery

    • Go to the jQuery Website and download it, or
    • Add ` in your HTML document before any jQuery code.
  2. Change line 4 of your Javascript to:

    removeTarget.fadeOut(1000)
    
    • This will Fade Out your item by 1000ms, you can change this time to whatever you want.

    CSS Howto..

    How do I add multiple linear-gradients to a css background? If

    show one div but hide all other divs is not working

    CSS- how to prevent the original title info to be displayed

    How to make a div fill the entire page height using css? [closed]

    How to center a child element in CSS, even if it is larger than the parent?

    @media query device - how to portrait one css, landscape other?

    How to horizontally align to “block” divs inside a div?

    Javascript: How do I get the id of a clicked link and then style it in css

    CSS: how to create responsive irregular shapes

    How to best make a hover navbar?

    Show the menu at the center of the data toggle

    how to create 3 columns in HTML+CSS and group 2 div elements in the third column

    CSS: How to clear line-through for an element in a table row?

    How to use CSS 3D to display child elements perpendicular to parent element?

    How do I load a CSS file with HTMLTextWriter

    How to position an element relative to the background image width

    how to write a desktop application which uses HTML and CSS for user interface and python/perl/c++/java for the processing?

    How can we retrive style from css by selector without creating element?

    How to find all elements with a particular CSS style, without a JS library?

    How does Mashable.com displays/hides an element through Css?

    How can i apply css rules dynamically in a div after the user enters the rules [closed]

    How to create the corner that show in picture with css in webpage?

    How to make a left and right panel, and be able to slide a divider between them?

    How to make Asp.net Gridview header and first column frezee?

    How to do isometric 3D in CSS

    How to get the current browser resolution in css?

    How to center a image with transparent background in css?

    How to do CSS wild card search? [duplicate]

    How to make UL Tabs with only HTML CSS

    How can I format my css text so that the result is in 2 columns?