Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 618
PariSh KhAn Avatar asked Oct 29 '15 20:10

PariSh KhAn


People also ask

How do I fade a div in Javascript?

jQuery Effect fadeOut() Method The fadeOut() method gradually changes the opacity, for selected elements, from visible to hidden (fading effect). Note: Hidden elements will not be displayed at all (no longer affects the layout of the page). Tip: This method is often used together with the fadeIn() method.

How do you fadeIn Javascript?

The jQuery fadeIn() method is used to fade in a hidden element. Syntax: $(selector). fadeIn(speed,callback);

How do I fade an image in Javascript?

To make the image transition with a fading effect we use the asynchronous function. Inside the asynchronous function, use another setInterval() method to slowly decrease the opacity of the topmost image till opacity becomes 0. By doing this, the topmost image will appear to fade away slowly.


1 Answers

I've made this function a while ago for a personal project:

function removeFadeOut( el, speed ) {     var seconds = speed/1000;     el.style.transition = "opacity "+seconds+"s ease";      el.style.opacity = 0;     setTimeout(function() {         el.parentNode.removeChild(el);     }, speed); }  removeFadeOut(document.getElementById('test'), 2000); 
like image 75
alvarodms Avatar answered Oct 27 '22 14:10

alvarodms