Implemented a scroll event listener on an element which correctly shifts in the desired direction only when the scrollTop value is not equal to 0

Recently, I created a small script that manages two lists of tasks - Finished/Active tasks. Users have the option to switch a task's list by clicking a button or view more details about the task by hitting "more info". A new div element is generated when the user clicks on "more info", which should follow its host element's Y position. However, I've encountered a bug where the tooltip jumps up and covers the task itself when scrolling. Here's an image showcasing the issue: enter image description here

The problem seems to occur whenever the scrollable elements scrollTop property isn't 0. Although, I'm confident that the creation process of the element is correct. Still, as I'm relatively new to coding and not very familiar with CSS/HTML, pinpointing the root cause has been challenging.

createTooltip() {
    // Code snippet for creating tooltips
}

I am including the HTML and CSS snippets from the course instructor for context:

HTML Snippet:

<!DOCTYPE html>
<html lang="en">
  <head>
    <!-- Head section content -->
  </head>
  <body>
    <!-- Body section content -->
  </body>
</html>

CSS Snippet:

* {
  box-sizing: border-box;
}

html {
  font-family: sans-serif;
}

body {
  margin: 0;
}

/* CSS styles go here */

#finished-projects header h2 {
  color: white;
  margin: 0;
}

Answer №1

To resolve this issue, consider utilizing CSS to make the necessary adjustments instead of relying solely on Javascript:

tooltipElement.style.position = 'absolute';
tooltipElement.style.left = x + 'px';
tooltipElement.style.top = y + 'px';

In order to implement these changes, update your CSS with the following modifications:

li.card {
    position: relative;
}
div.card {
    position: absolute;
    top: 0; /* Adjust as needed */
    left: 0; /* Experiment with values */
    z-index: 1;
}

For this solution to be effective, ensure that the div.card element created in your Javascript code is a child element within the appropriate li.card element. It is assumed that your original code resembles something like the example provided in the comment linked here: Sample CodePen, given the difficulty in running your original script.

The reason why your initial position:absolute setting did not produce the desired outcome was due to the default positioning of the parent li.card being set to static.

UPDATE: Don't forget to include the z-index property to prevent overlap issues with subsequent li.card elements.

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

In AngularJS, variables assigned to $scope will not automatically synchronize with HTML elements

I am currently working with 3 files: EventDetails.html, app.js, and EventController.js. Let's take a look at the content of the html file: <html ng-app="eventsApp"> <head>...</head> <body> <div class="container"> & ...

Express Route Handler doesn't work for specific file types

I am currently working on setting up a route handler for requests to '.js' files. I have successfully configured middleware to serve static files from a "/dist" directory, which is functioning correctly. However, I am facing an issue where the r ...

Encountering Datepicker Issue in Your Angularjs App?

I am currently working on a web application using Angular JS and I encountered an error when trying to incorporate a date picker. The error message displayed is "elem.datepicker is not a function" To implement the datepicker, I found reference code in thi ...

Create an HTML table row that includes a value along with its related sibling and parent values

I need to create an HTML table that compares segments in a JSON object. The format should display the segments along with their measures organized by domain group and vertical: ------------------------------------------------------------------------------ ...

Positioning <tr> elements with absolute precision within a intricate layout

I am faced with the challenge of developing a complex UI control. The structure consists of a left-side TreeTable component, implemented using a <table> element, and a right-side SVG component that displays data corresponding to each row of the left ...

Issue with Ionic 2: Variable is altered but does not reflect in the HTML view

Hello everyone, I am new to the world of ionic 2 and I am facing a problem that I hope you can help me with. I have a variable that I want to display on my smartphone screen by placing it between {{ myVar }} in my HTML code. The initial display works fine, ...

Group the JSON data in JavaScript by applying a filter

I have a specific json object structure with keys cgi, tag and name, where the cgi key may be repeated in multiple objects. If any cgi has the tag 'revert', then that particular cgi should not be returned. [ { "cgi": "abc-123 ...

Using Jquery to reset the form I have filled out

I am facing an issue with my forms. I have 2 form input fields and 1 select form. When I select option A from the select form, one of the input fields should hide and automatically reset/clear. I have successfully implemented the hide logic, but unable to ...

Reorganize elements using CSS styling

I have a trio of child divs with the classes span2, span7, and span3. When my browser width drops below 763px, I want them to display in the order span2, span3, and span7. How can I achieve this using CSS? Here is the code snippet I started with: <div ...

Adding objects from an existing JSON file to an empty object in Node.js is a straightforward process

Looking to extract JSON data from a separate file and transfer it into a new object using Node JS. Despite trying various methods without success, I still can't figure out how to achieve this. Can anyone provide guidance or suggestions? Add new eleme ...

Creating a Loopback API that seamlessly integrates with Ember.js

Exploring the use of Loopback to create an API that communicates with Ember. Ember expects JSON data to be enclosed in 'keys', such as for an account: { account: { domain: 'domain.com', subdomain: 'test', title: ...

Two objects intersecting in space

When using AngularJS for insert and update operations, I encounter a problem where changes made to user data are reflected in the list of users. Additionally, when adding a new user, the last record's data populates all input fields. Code: User List ...

If the data attribute exists, toggle between window.ready and window.ajaxComplete

When HTML includes: data-load-file or data-load-content then it should be handled with: document.ajaxComplete If not, then use: document.ready Currently, two separate files are being used to accomplish this $( document ).ready(function() { ...

"Combining multiple DIVs into a single DIV with jQuery - a step-by-step guide

I have multiple DIVs with the same class, and I am looking to group them into a single DIV using either jQuery or pure JS. <div class="a"> <div>1</div> </div> <div class="a"> <div>2</div> ...

Is there a way to print messages to the console of openDevTools in Electron JS?

After finishing a hello world application using electron js, I have successfully printed to my terminal with console.log and opened the openDevTools in the window of my application. However, I am now interested in finding a way for my console.log stateme ...

The concept of nested conditionals occurs when one

Struggling to dynamically assign a variable value based on viewport size using Hooks in Gatsby. While nested conditionals seem to work initially, resizing the browser window manually triggers an error: Rendered fewer hooks than expected. This may be caus ...

What is the method for deactivating an option with multiple values that are separated by commas?

I am working with a unique situation where I have options that contain multiple values $('#addItemModal #servicesBlock select[name=service_id] option[value='+service.val()+']').prop('disabled', true); <script src="https: ...

What is the best way to restrict the number of characters in a textarea when new data is being added

I am currently going through the steps below: 1. Setting up a textarea with a maximum length of 50 characters. 2. Implementing a button that, when clicked, appends text to the textarea. Problem: The maxlength validation does not kick in if the button is ...

Navigate through the overlay div to access the canvas, with a delegated click event triggered by the coordinates

Currently, my canvas application does not have a built-in zoom and pan feature. To work around this limitation, I have created an overlay on top of the canvas with pointer-events: all and opacity: 0, allowing users to zoom and pan seamlessly. However, I ...

Is there a way for me to adjust the typography background based on its current status?

Is there a way to dynamically adjust the background color of text based on the status value? Currently, when the status is pending, the background color defaults to yellow. For example, if the status changes to complete, I want the background color to ch ...