Gradually make a section disappear and delete it in WordPress

Is there a way to smoothly fade out and remove a section after clicking on it in WordPress? I tried using the code below, which worked but doesn't seem to work for WordPress. I am using the Oceanwp theme, which allows me to easily add CSS and JavaScript code. However, if you have a better solution to achieve this effect, I would greatly appreciate it. You can see the exact effect I am trying to achieve on this website . After clicking on the letter A and then on any button (CORPORATE or INSTITUTION), the current section fades away smoothly.

document.querySelector('.list').addEventListener("click", function(e) {
  if (e.target.localName === "section") {

    //Add CSS hide and animate with fade out
    var currentCSS = this.className;
    this.className = currentCSS + 'hide';

    var removeTarget = e.target.parentNode.parentNode;
    setTimeout(function() {
      removeTarget.parentNode.removeChild(removeTarget);
    }, 1000);
  };
});
.hide {
  opacity: 0;
}

.top {
  transition: 1s linear all;
  background-color: blue;
  height: 100vh;
}

section {
  background-color: green;
}
<div class="top">
  <section class="list">This is a section</section>
</div>

Answer №1

Here is the solution you've been looking for.

document.querySelector('.list').addEventListener("click", function(e) {
  //Applying CSS hide and fade out animation
  this.classList.add("hide");

  const removeTarget = e.target.parentNode;
  setTimeout(function() {
    removeTarget.parentNode.removeChild(removeTarget);
  }, 1000);
});
.hide {
  opacity: 0;
}

.top {
  background-color: blue;
  height: 100vh;
}

section {
  background-color: green;
  opacity: 1;
  transition: opacity 1s ease;
}
<div class="top">
  <section class="list">This is a section</section>
</div>

Answer №2

If you're working with jQuery, consider implementing the 'fadeOut' function:

$('.items').click(
  function(){
    $(this).fadeOut();
  }
);

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

Version 2 of the Microsoft Logo Animation

I made some changes to my Microsoft logo animation project. You can view the updated version on CodePen. Overall, I'm pretty happy with how it turned out except for one issue. I'm struggling to determine the right timing to end the animation so ...

Monitor File Tool for JetBrains IDE - CSS Optimization Setup - Streamline CSS Automatically

Utilizing the File Watcher tool in Jetbrains IDE (Webstorm and Rider) to automatically minify .css files and generate corresponding .min.css files for the entire project has been my goal. However, a challenge arises when it starts minifying files that alr ...

"Resolving the issue with unnecessary line breaks in Optimizepress when input is contained within a paragraph

I am experiencing an issue with the code on my server: <p>This is a test to determine why the <input type="text" style="text-align: center;" value="input"/> element appears on a new line when displayed on a wordpress page.</p> When view ...

What is the best way to position three SVG files in the center of a webpage?

I want to combine three separate SVG files to create one single graphic. The first SVG file is a red triangle, the second is a blue circle inside the triangle, and the third is a purple rectangle beneath the triangle with a little space in between. My obje ...

Using Ruby on Rails and jQuery to dynamically populate a text field when a button is

My edit.html.erb page includes the following form: <%= form_for(@device) do |f| %> <%= render 'shared/error_messages', :object => f.object %> <%= f.label :name %> <%= f.text_field :name %> <%= f.label :de ...

Is there a way to update the dictionary in the context processor without having to reload the page?

I have implemented a custom context processor that returns the value of "unread_messages_count". However, when I try to update this value on the template using the following JavaScript: var update_message_count = setInterval(function(){ ...

Issue with jQuery DataTables - ScrollY feature not responsive

Currently, I have implemented the DataTables plugin with a Responsive Table and fixed yScroll while disabling xScroll. Despite adding the following code snippet... scrollY: 200, scrollX: false, Screenshot Reference: https://i.ssta ...

Placing a div with 100% height inside a table-cell div in Firefox and IE

Seeking guidance on placing a div with 100% height inside a table-cell div. Attempted giving display:inline-block; width:100%;height:100%; to the div, successful in Chrome but facing issues in Mozilla and IE. Fiddle link: http://jsfiddle.net/kQM74/2/ HTM ...

Is there a way to align the height of a standard column with the ng-include section to ensure a cohesive page display?

I have been working on my Angular app and have implemented ng-include to dynamically add content to a template within the page. The issue I'm facing is that this specific area ends up being longer than both the html and body elements where it resides. ...

When accessing WordPress through a URL, I am able to view regular PHP files but encounter difficulty accessing any uploaded files

Currently working on a Wordpress project and facing issues with creating a product via the WooCommerce REST API. I managed to make it work in my local environment, but upon uploading the files to the server, I am unable to reach a specific file via URL. Fo ...

Is there a way to retrieve both the name of the players and the information within the players' profiles?

Just dipping my toes into the world of Javascript and jQuery while attempting to create an in-game scoreboard for Rocket League, I've hit a bit of a roadblock. Here's the console log output of data from the game: I'm particularly intereste ...

In HTML5, I am trying to figure out the best way to connect my webpage sections to the corresponding navigation links with the help of IDs (such as linking navhome to

I am in the process of redesigning a website to optimize it for mobile devices. Although I have completely restructured the site, I am encountering an issue where the main pages are not displaying properly. The header, navigation menu, sidebars, and a plac ...

Integrating Single Sign-On between ASP.NET and WordPress

I'm facing a dilemma where I have to automatically authenticate users between an ASP.NET site and WordPress. The concept is that once you log in to the ASP.NET site, you will also be logged in when browsing WordPress pages, and vice versa. So far, I& ...

What kind of impact on performance does the use of `$('selector')` have?

Currently, my application has the following implementation: var selector = '.selector'; $(selector).methodA(); $(selector).methodB(); //..... $(selector).methodZ(); In order to optimize performance, I am planning to switch selector to a jQuery ...

What is the best way to show only a section of a background in CSS?

Being a novice in HTML and CSS, I am facing the challenge of displaying an image as a banner with text overlay. Despite trying various methods, it seems impossible to achieve this directly with an image element, so I resorted to using CSS's background ...

Leveraging CSS and JQuery to manage an iframe with a programmed autoplay feature embedded within

My presentation was created using Adobe Presenter and inserted via an iframe with autoplay enabled upon publication. This means that as soon as the page loads, the presentation begins automatically along with the audio. Since I am unable to disable autopl ...

Display full lines of nested tree view HTML lists upon hovering using HTML, CSS, Angular, and Bootstrap

I currently have an Angular 4 application where a left-side div displays a tree of items as recursive HTML lists. The issue I am facing is that long texts within this div get cut off by the right border, with a scrollbar appearing instead. What I would lik ...

What is the best way to incorporate the .top offset into a div's height calculation?

Looking to enhance the aesthetic of this blog by adjusting the height of the #content div to match that of the last article. This will allow the background image to repeat seamlessly along the vertical axis. I attempted the following code: $(document).re ...

What are the common reasons for jQuery Image Slider malfunctioning?

After following a tutorial on Youtube (https://www.youtube.com/watch?v=KkzVFB3Ba_o) about creating a JQuery image gallery, I realized that my implementation is not working at all. Despite carefully reviewing my code and fixing any errors I could find, the ...

What could be the reason for the content of my navBar not showing up?

Using Bootstrap and additional CSS for styling has been working well, except for the issue with the Dropdown Menu not displaying its content on hover. Within the head tag, the following scripts are included: <!--Jquery--> <script type="text ...