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?
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.
The jQuery fadeIn() method is used to fade in a hidden element. Syntax: $(selector). fadeIn(speed,callback);
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With