Using CSS translateY for a smooth animation of sliding down behind the parent element

Hey there! I'm currently working on animating the sliding down of an element using transformY when a link in the header is clicked. The challenge I'm facing is that the element I want to display is nested quite deeply within other elements, and this seems to be causing some issues - I'd like the element to slide down from behind its parent elements. Unfortunately, setting the z-index doesn't seem to be effective in this scenario. I've provided a JS Fiddle below for reference. In the demo, the green parent container should be positioned on top of the hidden yellow element.

jQuery('.test-link').on('click', function(e) {
  e.preventDefault();
  jQuery('.container').toggleClass('active');
});
.outer-container {
  background: green;
  width: 100%;
  position: relative;
  z-index: 5;
}

.container .hidden-content {
  transition: all 0.2s ease;
  transform: translateY(-100%);
  position: absolute;
  z-index: -1;
  width: 100vw;
  left: 0;
  right: 0;
  background: yellow;
}

.test-link {
  margin-left: 100px;
  display: block;
  position: relative;
  z-index: 3;
  width: 200px;
  background: red;
}

.container.active .hidden-content {
  transform: translateY(0%);
}
 
.other-content {
  position: relative;
  z-index: 4;
  background: blue;
}

.test-content {
  position: relative;
  z-index: 5; 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>

<div class="outer-container">
  <div class="test-content">
    <div class="container">
      <a href="#" class="test-link">TEST LINK</a>
      <div class="hidden-content">
        <h1>My Hidden Content</h1>
        <h1>My Hidden Content</h1>
      </div>
    </div>
  </div>
<div class="other-content">
 <h2>This should be overlaid by the sliding out content</h2>
 </div>
</div>

Answer №1

If you want to achieve this effect, you can utilize a negative z-index.

jQuery('.test-link').on('click', function(e) {
  e.preventDefault();
  jQuery('.container').toggleClass('active');
});
.another-container {
  position: relative;
  z-index: 1;
}
.outer-container {
  background: green;
  width: 100%;
  position: relative;
}

.container .hidden-content {
  transition: all 0.2s ease;
  transform: translateY(-100%);
  position: absolute;
  background: yellow;
  z-index: -1;
}

.test-link {
  display: block;
  position: relative;
  width: 200px;
  background: red;
}

.container.active .hidden-content {
  transform: translateY(0%);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="another-container">
<div class="outer-container">
  <div class="test-content">
    <div class="container">
      <a href="#" class="test-link">TEST LINK</a>
      <div class="hidden-content">
        <h1>My Hidden Content</h1>
        <h1>My Hidden Content</h1>
      </div>
    </div>
  </div>
</div>
</div>
<div>The yellow box should appear on top</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

Executing Selenium test cases based on HTML tag is a key aspect of automated testing

I need to run my selenium test scenarios based on the value of an HTML tag, for example: <meta name="survey name" content="ABC Car"> The content represents different event types, with a total of 4 possible event types. ...

Expand the contents inside a div without affecting the actual div

Is there a way to zoom out the contents of a div similar to how a browser zooms out a page? I'm working on an application that loads a page into a div. However, I want to load the content without scroll bars. This would require zooming out the conten ...

Tips for positioning a Div element in the center of a page with a full-page background

I am trying to center align my div containing login information on the webpage. The static values seem to work fine, but I am looking for a way to position it dynamically. Additionally, the background is only covering a strip with the height of the div... ...

Avoid attempting to mount the component if the element identified by its ID does not exist

Currently, I am engaged in a project where a form builder (Symfony) is utilized to create forms. At times, an internal Vue component takes the place of standard TextArea inputs with more versatile HTML-based text editors depending on the input id provided ...

Arrangement of nested lists in Internet Explorer

I'm currently developing a dynamic side menu using CSS and HTML. The menu functions perfectly in all browsers except for Internet Explorer. In IE, the nested lists that create the submenu appear about 50 pixels too far to the right, causing a gap betw ...

Using the mapState function in vuex allows for easy access to Vue methods

I have an important task to complete while the data is being computed using vuex mapState. I must ensure that the vue method countAlerts is called every time the data changes; however, the computed property needs to invoke this method and unfortunately, ...

Determine the length of the content within an HTML container that has

I am attempting to retrieve the length of the container's content in HTML dynamically. However, I am only receiving the object instead of the desired result. <script> $(document).ready(function (e) { var adsense_box_len = $(&apo ...

Arrange a cluster of CSS table cells onto a fresh line

Currently, I am exploring CSS properties to create the visual appearance of a table using two levels of DOM elements. The highest-level element wraps around its child elements, which are styled to resemble a flat list. For example: <div class="t"> ...

What is the best way to keep making getjson calls until a non-empty response is received?

Hey there, I have a question about handling the response from a getjson call. I'm looking to check if the siteContents response is empty or missing a required string (specifically, looking for seasonEpisode=). If it's not as expected, I want to m ...

Preventing scrolling on the parent/body element while a modal is active in a react application

I have come across many questions similar to this. In my React project, I am trying to prevent the parent/body from scrolling when a modal/popup is in hover or focus. I want the parent scroll bar to remain visible but disabled when the modal is hovered o ...

Developing a new React application with Access Control List (ACL) and encountering an issue with Casl

I've recently started working with casl and it feels like I might be overlooking something crucial. So, I created a file named can.js which closely resembles the example provided in the documentation: import { createContext } from 'react'; i ...

The event.target.name is providing an undefined value

Whenever I attempt to pass the value from an input component to the handlesumbit function and log it in the console, I am receiving 'undefined' for the variable cName. @connect((store) => { return { nameOfCity:st ...

What is causing the undefined result when duplicate elements are encountered?

const cardChild = document.querySelectorAll('.card i'); const cardsArray = ['a','a','b','b','c','c']; const matchArray = []; function cardsToClass() { for (i = 0; i < cardChi ...

Is there a way to display a paragraph when a button is clicked?

I want my paragraph to appear when the button is clicked <button class="con-button" type="button">CLICK HERE</button> <p id="d-c" class="d-c">Thank you for reaching out to <strong&g ...

Managing an unexpected variable when making an AJAX request

Here is a code snippet that I am working with: var User = { get: function (options) { var self = this; $.ajax({ url: options.url, success: function (data, response) { self.nextPageUrl = data.pagination.next_page; opt ...

Looking for assistance on positioning an image of an icon both vertically and horizontally within a div that has a border radius. Can anyone help with

I am attempting to center an image within each of the four divs below. I have experimented with using the image as a background, but due to the border radius, it does not appear quite right. To provide context, I have included a fiddle. HTML: <div ...

Sending a function along with arguments to props in TypeScript

Encountering a peculiar error while executing this code snippet (React+Typescript): // not functioning as expected <TestClass input={InputFunction} /> And similarly with the following code: // still causing issues <TestClass input={(props ...

Output: "Obtain a platform that allows for direct printing to a specified local printer."

I am currently facing a challenge with my website that provides box office services by issuing tickets and generating reports. I am exploring how to send tickets (which are currently in PDF format) directly to a specified printer on a client's local P ...

What are some alternative ways to arrange this main navigation bar?

Check out the website, below the map you'll find an unordered list: HTML code: <ul class="play_navigation"> <li class="active"><a href="#" class="barino_story_bottom">The Story</a></li> <li><a href="#" ...

Using jQuery to Delete a DOM Element Once the Ajax Call is Successful

I'm currently facing an issue with removing an element after a successful ajax request. Below is my ajax code snippet: verifyRequest.denyUser = function(requestId,element){ $.ajax({ url: loaderURL+'idverified/denyRequest', ...