Using JQuery toggleclass to add transitioning effects to a :after element

I'm using the toggleClass function to add the class .expend to a div with an ID of #menu, which increases the height of the menu from 100px to 200px with a smooth transition effect. I also have a pseudo-element #menu:after for aesthetic purposes.

My issue is that when the .expend class is toggled and the :after element appears, there is no fading in or out animation present.

I am seeking a solution to smoothly animate the appearance and disappearance of my pseudo-element similarly to how the menu transitions. How can I achieve this?

$('#link').click( function() {
$("#menu").toggleClass("expend");
$('.input-area').fadeToggle(300);
} );
#menu {
  width: 200px;
  height: 100px;
  border: 1px solid blue;
  padding: 10px;
  margin: 10px;
  transition: 0.3s ease-out;
}

#menu.expend {
  height: 200px !important;
}

#menu.expend:after {
  content: 'how to get a 0.3 transition effect here (fade in and fade out) ?';
  position: absolute;
  top: 170px;
  left: 30px;
  width: 170px;
  height: 60px;
  border: 1px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a id="link" href="#">click me</a>
<div id="menu">
  <div class="input-area">
    <input type="text" name="test" id="test" value=""/>
  </div>
</div>

Answer №1

It is recommended to apply a CSS transition on the pseudo element for #menu:after and #menu.expend:after. Since generated content is not part of the DOM, JavaScript cannot access it :)

$('#link').click( function() {
$("#menu").toggleClass("expend");
$('.input-area').fadeToggle(300);
} );
#menu {
  width: 200px;
  height: 100px;
  border: 1px solid blue;
  padding: 10px;
  margin: 10px;
  transition: 0.25s ease-out;
}

#menu.expend {
  height: 200px !important;
}
/* Adjust values as needed, same goes for transition timing :) */
#menu:after {
  content: '';
  position: absolute;
  top: 100px;
  left: 30px;
  width: 170px;
  height: 0px;
  border: 0px solid red;
  transition:height 0.3s, top 0.3s, border 0.25s ;
  overflow:hidden;
}
#menu.expend:after {
  content: 'how to achieve a 0.3 transition effect here (fade in and fade out) ?';
  position: absolute;
  top: 170px;
  left: 30px;
  width: 170px;
  height: 60px;
  border: 1px solid red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a id="link" href="#">click me</a>
<div id="menu">
  <div class="input-area">
    <input type="text" name="test" id="test" value=""/>
  </div>
</div>

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

Can the system automatically generate and display a new user ID right after a form is submitted?

Once users have submitted the basic information in a form, how can I ensure that the newly created userID is displayed in a hidden field on the header page? The initial form collects Name, Phone, and Email details. Upon submission, the 'userID' ...

Developing a dynamic web application using the Django framework along with the Vue.js library and Highcharts for

I am currently working on a data visualization web app using Django, Highcharts, and JQuery. I have recently transitioned from JQuery to Vue JS and I am struggling with fetching JSON data from a specific URL. Below is the code snippet: Template <!doc ...

403 Forbidden - CSRF Token Not Recognized

I am encountering an issue with Node Express and CSurf - 403 (Forbidden) Invalid csrf token. After reviewing other solutions and attempting all available options, I am still unable to resolve this. The API functions perfectly when tested in Postman. Howe ...

Generating React components dynamically can bring flexibility and efficiency to the

Looking for a solution to dynamically render different components based on the arguments passed with data. While using regular or React.createElement(Item) works fine, all other options seem to fail. http://jsfiddle.net/zeen/fmhhtk5o/1/ var React = windo ...

How can I apply a class to the pre and code elements in Redactor?

I've been struggling for days to figure out how to add a class to the formatting option element within Redactor. By default, the "Code" formatting option wraps content in either <pre></pre> or <code></code> HTML elements. Howe ...

The function FileReader() is not functioning properly within a Vue computed property

I'm attempting to display a set of image thumbnails by dragging images onto the screen. Here is an example of my data structure: data() { return { files: [Image1, Image2, Image3] } } ...where each Image is in a blob format. Below is my co ...

Use jQuery to display the first 5 rows of a table

I recently posted a query on show hide jquery table rows for imported xml data regarding how to toggle visibility of specific table rows using jQuery. Now, I am seeking advice on how to make the first 5 elements always visible within the same context. Belo ...

Create a new class in the body tag using Javascript

If the operating system is MAC, I set a variable and then based on a condition, I want to add a new class in the body tag. Check out my code snippet: <script type="text/javascript" language="javascript"> var mac = 0; if(navigator.userAgent.index ...

Having trouble with running 'npm init -y' in Windows? Here's a workaround for the VS

An error occurred when trying to initialize npm with the command "npm init -y". The name "WEB DEV" was deemed invalid. For a detailed log of this issue, please refer to: C:\Users\User\AppData\Roaming\npm-cache_logs\2020-12-05 ...

Implementing user authentication in node.js with passport

Having trouble with a basic login system using passport. I keep getting an error when logging in with the correct credentials: Error: Express 500 TypeError - Cannot read property 'passport' of undefined Oddly enough, everything works fine when ...

Implementing dual pagination components in Vue JS for dynamic updates on click

Having two pagination components on a single page is convenient for users to navigate without scrolling too much. One component is placed at the top and the other at the bottom. The issue arises when I switch to page 2 using the top component, as the bott ...

When the browser is not in the foreground, clicking on the Bootstrap datepicker with Selenium does not register

When you click on the input field <input id="dp1" class="span2" type="text" value="02-16-2012"> If the browser is in the background, the datepicker popup will not display. Even using javascript or jquery to click the input field does not show the ...

Tips for clearing Nightwatch session storage efficiently

To ensure the pop-up functionality was working properly, I had to reset the browser's session storage. By doing this, the pop-up will reappear. Is there a way to clear the page's Session Storage in Nightwatch? ...

Assigning values to template variables in Express 4's routes/index

Recently, I started using node.js and express. To set up express 4, I used the command "express myAppName" in the terminal, which created a default directory with Jade templates as default. The main file, app.js, has the following standard express boilerp ...

Access the child element of a span by targeting a specific attribute value using Protractor

Trying to check if a popover appears using protractor. Below is the snippet of HTML code with the popover in the last child span: <span tariff-popover="views/popovers/c2g/airport.html" class="ng-isolate-scope"> <span ng-transclude=""> ...

Using Jquery to switch controls to labels for a read-only form display

Looking for a solution where a form can be displayed in view-only mode without making controls read-only. The requirement is to show the values of text boxes, drop downs, and list boxes (comma delimited) as labels when the user has read-only access to the ...

Express-minify is having trouble loading the CSS file. If we remove a portion of the file, it should be able

After implementing the express-minify middleware, I encountered an issue with loading the attached CSS file. Even after validating the CSS using an online service, no errors were detected. I attempted to debug by gradually removing elements, and the prob ...

Struggling to use GSAP to control the timeline with my custom functions

I'm attempting to create a greensock animated banner with the ability to switch between different scenes. I've made progress by setting up the timeline for each scene's intro and outro animations using the GreenSock API documentation. Howeve ...

Unlocking the Potential: Employing postMessage in an iFrame to Seamlessly Navigate Users towards an Enriching Destination on

One of my webpages includes an iframe. Here's the challenge: I want a button within the frame to redirect users to another page without reloading the iframe's content. Instead, I want the URL of the main window to change. Unfortunately, I haven& ...

How come this particular design is being passed down from a predecessor (and not a direct parent) div?

Web development can be frustrating at times. Hopefully, someone out there can shed some light on this issue and offer a solution. You can view the HTML/CSS code below or check out this example on JSFiddle The problem arises when I have a header div and a ...