Introducing a content height offset to create a smooth slide-up animation on hover

How can I offset a block with a title and description above a picture by the height of the description content? See the example picture below:

https://i.sstatic.net/DzcMo.png

The goal is to have both titles visible by default, but when hovering over the block, the description should slide up. However, since the length of the content may vary, determining the exact height of the slide is challenging. Even using JavaScript on page load doesn't provide the correct offset due to font loading timing issues.

To address this issue, I attempted to separate the upper and lower content in the HTML:

<div style="background-image: url(https://via.placeholder.com/175x175)">
  <div class="wrapper">
    <div class="team-meta">
      <div class="block-title"><p>TITLE PRESIDENT</p></div>
      <div class="team-name">TITLE PRESIDENT</div>
    </div>
    <div class="team-description">On Hover, darken overlay and add a brief one sentence description of the person if desired.</div>
  </div>
</div>

I then applied these styles:

.team-meta {
  position: absolute;
  bottom: 0; /*always at the very bottom of the block*/
}
.team-description {
  position: absolute;
  top: 100%; /*always right below the block*/
}

While this approach hides the description by default, it complicates displaying it on hover as manual calculations are required for positioning.

For description: top: [height of parent] - [height of the description]

For title: bottom: [height of the description]

This method is inconvenient and requires a lot of steps. As a result, I tried utilizing code that ended up causing more problems:

document.querySelectorAll('.team-container').forEach((e) => {
  anime({
    targets: e.querySelector(".team-description"),
    top: e.querySelector('.wrapper').offsetHeight - e.querySelector('.team-description').offsetHeight + "px"
  }); 
  anime({
    targets: e.querySelector(".team-meta"),
    bottom: e.querySelector('.team-description').offsetHeight + "px"
  });
});

The issue with this code arises from incorrect conversion of values during animation, resulting in unexpected behavior. The block seems to transition from the top instead of the intended bottom alignment.

A solution to this problem may exist, but I haven't found it yet.

Answer №1

It seems like I made a mistake, or maybe something unexpected happened. However, amazingly, the script below now returns the correct value for offsetHeight out of nowhere. By utilizing a container, I was able to manipulate the entire block with just one animation of margin. It all suddenly clicked!

I have a hunch that this could possibly be achieved without involving JS, but unfortunately, I couldn't quite crack it.

(function () {

document.querySelectorAll('.team-container > div').forEach((e) => {

  let offset = e.querySelector('.team-description').offsetHeight;

  e.querySelector(".team-card").style.marginBottom = -offset + "px";
}

}());

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

Optimizing image centering in Next JS as screen size expands

I've been struggling to work with NextJS's Image component. My goal is to set up a banner image that only covers a specific amount of space on the screen, regardless of screen size. I have managed to achieve this by using max-height: 30vh and ov ...

Challenges with container height in Angular Material Tabs

I'm currently working on an Angular application using Angular Material, and I've encountered a significant issue with the height of my tab content. While I have experience with HTML, CSS, and JavaScript, this is my first venture into Angular deve ...

Automatically update the page when there is a change in the status of the

$counter = "SELECT countnumber FROM onairsystem WHERE id='1'"; $counterresult = $connection->query($counter); $rows = $counterresult->fetch_array(); $number = $rows['countnumber']; Is there a way to automatically refresh the we ...

`The Streaming module within React Native`

I am facing an issue in my React Native project where I want to use a node library that depends on stream (require('stream')). The problem arises with the error stream could not be found within the project because stream is a nodejs package not ...

Beginner looking to create a rock, paper, scissors game using arrays in JavaScript

As a recent addition to this community and as someone who just started learning JavaScript 4 weeks ago, I have progressed from html to css and now onto JavaScript. My current project involves creating an array-based rock-paper-scissors game with minimal m ...

Execute Python code alongside JavaScript functions using ExecJS - an efficient JavaScript engine for seamless integration with

Currently, I am exploring the integration of JavaScript engine with Python. I am interested in working with Python classes in JavaScript and vice versa, as well as using JavaScript code within Python. How can I achieve this? In a Java project, I have suc ...

Comparison of single-line and multi-line CSS styles placement

There seems to be a debate among CSS developers regarding the preference for multi-line or single-line formatting. Some argue that multi-line is favored for its ease in finding specific properties within the CSS file. However, others believe that single- ...

CSS: Stretch the text background to match the width of the container

Is there a way to make the background color of a text cell stretch the width of its container div, rather than just the text itself? https://i.sstatic.net/E3zeL.png Here's the HTML code snippet: <div class='col-md-4'> <span class= ...

Having trouble targeting a div with jQuery

Is it possible to target a specific div within an unordered list and list items? I'm having trouble with it. Here is the HTML code: <ul class="grid"> <div id='categoria' cat='web'></div> <li id=' ...

Leveraging AngularJS $filter in conjunction with ng-disabled

I have a variable in my $scope that contains information about an election, including a list of voters with unique IDs: $scope.election = { voters: [ { _id: '123' }, { _id: '456' }, { _id: '789' } ] } Additio ...

How can I trim a value using one-way data binding in Angular?

Is there a way to trim values in Angular using one-way data binding? Can this be done directly in the component.html file rather than in typescript? I tried the following but it didn't work: <P>{{country.trim()}}</P> Thanks ...

"Encountering problem with Angular HTTP services: the requested URL is not

Attempting to send data to API servers is resulting in a 404 error. Testing it on Postman shows that everything works fine. JSONP is being used for posting data due to cross-domain issues. The console displays the following: GET http://myapi.com/registrat ...

Tips for handling various mandatory fields for two different user roles within a unified userModel.ts file on a Next.js and MongoDB user registration API platform

Could you please review my code and provide any suggestions for improvement? I have two types of user roles, candidate and business, each with multiple unique fields. My goal is to consolidate all these fields into one userModel.ts file. import mongoose ...

Is there a way to show the keyboard on Chrome for Android without displaying the address bar?

One of the key features of Chrome on Android is that the address bar disappears when scrolling, allowing for more screen space. However, when you click on a text input field and the keyboard pops up, the address bar reappears. It seems like Google intenti ...

Understanding the JSON output received from the Servlet

So, I have a Java Servlet set up to return JSON data in Application/JSON format using the GSON library. The GET method of the Servlet requires an ID parameter. When I send a request with BookingID as 1, Chrome shows the AJAX response like this: 0: {W ...

Enhancing custom geometry with textures in Three.js

Trying to figure out how to apply a simple material with a texture map to a custom mesh? Look no further! Check out this demo I created to showcase what I'm attempting to do. /// MATERIAL var texture = new THREE.TextureLoader().load( "https://raw.git ...

Adding Conditionally Specified Properties to a Parameterized TypeScript Interface Based on Type

Encountering a challenge with TypeScript where I need to selectively add properties to a generic interface depending on the type parameter. Let me explain further with an example: Consider two interfaces, A and G<T>: interface A { IA: string; } ...

Can you explain the purpose of the #shadow-root found within elements?

Today I witnessed something out of the ordinary. Take a look at the images attached to this post. Specifically, pay attention to the input[type="text"] labeled as "1" in the screen picture. The CSS styling for it appears as follows: tab ...

Extension with a responsive design - Conceal all columns with the exception of the initial one

I am currently utilizing the latest version of jQuery DataTables along with the Responsive extension. While everything is functioning properly on tablets and desktops, I encounter an issue when the screen size drops below approximately 600px. When the res ...

The function fails to return any value, however console.log does output something

I am attempting to implement a JavaScript function that checks for repeated letters within a string. The goal is for the function to return false if any repeating letters are found, and true if no repeats are present. However, I am encountering an issue wh ...