offspring of offspring in jquery animation remains stationary

I'm experiencing an issue with a jquery animation on containers that are set to 100% width and height. Specifically, the children elements that have position absolute move with the container, but when a child of a child has two instances of position absolute, it doesn't move in the expected manner. Does anyone know why this might be happening and how to work around it?

$(document).ready(function() {
  var hash = location.hash;
  console.log(hash);
  $(window).on("hashchange", function() {
    hash = hash ? hash : "#page1";
    $(hash)
      //I have tried adding
      //.css("overflow","hidden");
      .animate({
        height: "hide"
      });
    hash = location.hash
    $(hash)
      //I have tried adding
      //.css("overflow","hidden");
      .animate({
        height: "show"
      });
  });
  hash ? $(hash).toggleClass("page-active") : $("#page1").toggleClass("page-active");
});
body,
html {
  width: 100%;
  height: 100%;
  text-align: center;
}

.page1 {
  width: 100%;
  height: 100%;
  background-color: cyan;
  display: none;
  position: relative;
}

.page2 {
  width: 100%;
  height: 100%;
  background-color: lime;
  display: none;
  position: relative;
}

.page-active {
  display: block;
}

a {
  color: black;
  position: absolute;
  top: 50%;
}

.hello {
  position: absolute;
  top: 3em;
  left: 3em;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<body>

  <div class="page1" id="page1">
    hello
    <div class="hello">
      <div class="i-stay">
        Why am I here
      </div>
    </div>
    <a href="#page2">go to page2</a>
  </div>
  <div class="page2" id="page2">
    hi
    <a href="#page1">go to page1</a>
  </div>
</body>

Click here for the jsfiddle link

Answer №1

The positioning behavior you are seeing is a direct result of the CSS styling that has been applied. When an element's position property is set to absolute, it will be positioned absolutely in relation to the nearest parent element with a position: relative rule. In this case, the link is moving because it has an absolute position along with a top: 50%; declaration.

.hello {
    position: absolute;
    top: 3em;
    left: 3em;
}

On the other hand, in the code snippet above, the element with class "hello" is positioned at exactly 3em from the top and left of its parent element. This distance remains consistent regardless of the size of the parent element.

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

Exploring the dynamic capabilities of JSON with ASP .Net

After successfully retrieving JSON data from a database using PHP through json_encode() and jQuery parsing, I'm curious if there is a similar method for accomplishing this dynamically in ASP.Net Web Forms. Specifically, without the need to pre-determi ...

The battle of line-height versus padding for achieving vertical centering with one-lined text

One-lined text can be vertically centered using either the line-height property or by adding padding-top and padding-bottom. What are the advantages and disadvantages of each method? body { font-size: 12px; font-family: ...

Learn about generating PDF files with React Native Expo using the react-native-html-to-pdf library!

I'm currently attempting to execute a 'Hello World' code utilizing the react-native-html-to-pdf library in order to generate a PDF, but I am encountering difficulties setting it up in Expo. Would you be able to provide assistance? I have tri ...

Using Special Characters in React JS Applications

When handling CSV uploads with accented characters such as émily or ástha, I encountered the need to encode and pass them to the backend. Experimenting with different approaches, I tried adjusting the file type in FormData from 'text/plain' to ...

There seems to be an issue with the React Hooks edit form where it is not selecting the record to edit. Although the currentId is correct

I have a simple CRUD React Hooks app with an ASP.NET Core Web API. The Courses component displays a list, but when I click on a link to edit a particular course, the form shows up with empty fields. Here is the JSX for the Courses component: import React, ...

Unexpectedly, the React custom component is failing to render as intended

I am anticipating the text "SMALL Medium, Big" to be displayed on the screen but unfortunately it's not showing up function Box(prop){ const ele = <div className={prop.cn}> </div> return ele } ...

In JavaFX, when adjusting the transparency of a popup window, only the brightness changes while the solid color remains unchanged, effectively concealing

Is it possible to have a transparent popup window (errorDialog.fxml) on top of the main window (mainWindow.fxml) with 50% opacity while still showing the content of the main window underneath? My attempts at setting the background color of the overlay insi ...

How is it possible to access a variable in a function that hasn't been declared until later?

While working on a Dialog component, I had an unexpected realization. export const alert = (content: string) => { const buttons = [<button onClick={()=>closeModal()}>ok</button>] // seems alright // const buttons = [<button onCli ...

"Encountering a glitch with masterpage.cs in Aspx.net and C#

Currently facing a perplexing issue while developing a website using aspx.net and c#. The following code snippet is on my masterpage: protected void Page_Load(object sender, EventArgs e) { if (HttpContext.Current.Request.Url.AbsolutePath == "/ ...

Tips for Removing Copyright on Charts

Check this out : https://jsfiddle.net/oscar11/4qdan7k7/5/ Is there a way to eliminate the phrase JS chart by amCharts? ...

Textbox value disappears after being updated

When I click on the edit link (name), the value from the database is displayed as a textbox. However, when I update another normal textbox (age), the value in the edit link textbox disappears. Strangely, if I input a new value into the edit link textbox, i ...

How to effectively leverage useMediaQuery in material ui?

Upon completing the web application, I have made the decision to ensure it is mobile-friendly. Utilizing the material UI framework with react, I delved into the documentation but found myself uncertain about how to effectively implement it. Let me provide ...

Transform your standard HTML buttons into a collective of radio buttons

Looking for a way to make some ordinary buttons function as radio buttons without adding another library to the project. Any suggestions on how this can be achieved using just HTML and JavaScript/jQuery? Appreciate any help, thank you. ...

Pass data from Angular UI Bootstrap Modal to parent controller upon background click or closing with the ESC key

Hello everyone. Let's dive right into the issue - I am trying to figure out how to pass a variable back from an AngularJS UI Boostrap Modal when the user clicks on the background or presses the ESC button on their keyboard. There are some solutions ...

Steps to retrieve an incorrect fruit when it is located as the initial item within the array

Currently tackling the precourse material for a coding bootcamp and hitting a roadblock with this particular question. Despite my best efforts, I just can't seem to meet one of the 7 conditions required. Let me outline the question, my attempted solut ...

What is the smoothest way to animate price changes as you scroll?

After searching online, I could only find a version for swift. The only keyword that resulted in a search was ScrollCounter. Is it possible to create this type of animation using CSS and HTML? If anyone knows of a helpful resource or example, please share ...

Identifying the camera model using getMediaStream

Are there methods available to determine if a user's device has a front, rear, or dual cameras installed? For instance, laptops typically only have a front-facing camera while some devices may have both. I am looking for a way to identify the type of ...

ASP.Net - Unexpected JSON Format Error

As I work on my ASP.Net web application, I am encountering an issue with binding data from a database to a Google Combo chart via a Web Service class. While I can successfully bind the data to a grid view, attempting to bind it to the chart results in the ...

Changing <br> to a newline ( ) using JSOUP

Is there a way to efficiently replace all occurrences of <br> with a new line character (\n) in HTML using Jsoup? I'm looking for a clean solution that will result in the Element#text() outputting plain text without any HTML, but with &bsol ...

Applying a class to a specific element within another element using jQuery

This section pertains to a user account. When the user clicks on the edit button, I would like an edit box to appear within the same element as the button. Here is the HTML markup: <div class="col account"> <strong>Account Settings</st ...