Determine the maximum number of children divs that can be accommodated within a parent div

I am facing a situation where I have an API that returns multiple div elements. In my user interface, there is a parent div in which these child divs need to be displayed. The challenge is that if the child divs are overflowing the parent div, they should be shown on the next page.

For example, let's assume the API returns a string like this:

<div class="ab0"></div>
<div class="ab1"></div>
<div class="ab2"></div>
<div class="ab3"></div>
<div class="ab4"></div>

If the parent div can only accommodate ab0, ab1, and ab2, then I want to display these 3 divs first. When the user clicks on the '>' symbol, I intend to show ab3 and ab4. Additionally, if ab2 is partially overflowing and I can display only the overflow part on the next page, it would be ideal.

Is there a way to achieve this?

Thank you in advance.

Answer №1

One effective technique is to leverage the built-in scroll feature of the system.

The system has a preset display capacity, and by determining the height of the parent container, you can navigate through the child elements (even if they exceed the visible area) using Javascript scrollTop.

const parent = document.querySelector('.parent');
const h = parent.offsetHeight;
let page = 0;
const lastPage = Math.floor((parent.scrollHeight + 1) / h);

function next() {
  if (page < lastPage) {
    page++;
    parent.scrollTop = parent.scrollTop + h;
  }
}

function prev() {
  if (page > 0) {
    page--;
    parent.scrollTop = parent.scrollTop - h;
  }
}
.parent {
  height: 64px;
  overflow: auto;
}

.parent div {
  height: 2em;
  border: 1px solid;
  position: relative;
}

button {
  font-size: 2em;
}
<div class="parent">
  <div class="ab0">0</div>
  <div class="ab1">1</div>
  <div class="ab2">2</div>
  <div class="ab3">3</div>
  <div class="ab4">4</div>
</div>
<button onclick="prev();"><</button>
<button onclick="next();">></button>
</button>

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

How do I implement Routing while running Express and React on the same port?

At the moment, I am hosting my react-files in a static manner by serving them through my express-server: app.use(express.static(path.join(__dirname, "..", "..", "build"))); app.get("/", function (req, res) { res.sendFile(path.join(__dirname, "..", "..", ...

The parameter type (key: string, id: string, pagination: number) in the argument does not match the expected type of Boolean for the parameter

I'm facing an issue while trying to implement the following documentation: https://swr.vercel.app/ using my own setup: import React, { useEffect } from 'react' import PatientsTable from 'components/patients/PatientsTable' import ...

Adjust the placement of an object within cannon.js based on its local orientation

I am trying to move a body in cannon.js with a quaternion rotation by 100 units along a vector relative to its local rotation. However, I'm facing a problem with the positioning. For example: let body = new CANNON.Body({ mass: 0 }); body.quaternion. ...

Reloading a page using Javascript

I have designed a login system that collects user credentials and sends them to a controller. If the details are correct, the controller provides data to display on the home page. Now, I'm looking for a way to add a refresh link on the home page that ...

Troubleshooting a NPM issue while trying to launch a React application on Netlify

I'm encountering an issue while attempting to deploy my website on Netlify. The error message I keep getting is: 9:59:45 PM: npm ERR! npm ERR! code EINVALIDTAGNAME 9:59:45 PM: npm ERR! npm ERR! Invalid tag name "^>= 4": Tags may not have ...

What is the method for creating two separate columns that can scroll independently?

I am faced with a challenge involving a modal that contains two columns. My goal is to make these columns scrollable independently of each other while ensuring the scrollbar remains hidden. Thus far, I have managed to make the row scrollable with a hidden ...

Upgrading to React Router v6: Implementing Loader Functions with Context API

Having issues implementing loaders in React-Router V6 while making a request for a page through a function located in the context file. Unfortunately, I can't access the context from main.js where the router is defined. Main.js import ReactDOM from & ...

What would be the best location for me to place my code for adding fresh data to Firestore?

Currently, I am working on a project that involves using React and Firebase. The issue I have encountered is that the logic for writing new data to Firestore is directly in my components, which is making them quite cumbersome. I am also in the process of i ...

Leveraging an HTML form for searching and fetching data from MySQL Workbench using PHP

I'm currently working on fetching data from my database by utilizing an HTML form and establishing a PHP connection to my SQL workbench. The data retrieval is successful, as it appears at the top of my webpage. However, I am facing issues with executi ...

Attempting to create a JavaScript class within an HTML document

Having an issue with instantiating a JavaScript class in a separate HTML file. The JavaScript class looks like this: class Puzzle { constructor(fenStart, pgnEnd) { this.fenStart = fenStart; this.pgnEnd = pgnEnd; } } module.exports = Puzzle; ...

Insert the characteristics of the object into the header of the table, and populate the rows of the table

I have created an HTML table that displays specific object properties when the mouse hovers over a designated object. For example, hovering over the dog object will display the dog's name. I want to expand this functionality to also show the cat' ...

Combine assorted fabrics and materials in selected designs

Is it possible to incorporate multiple texture maps into a material such as phong? I am aware that using shadermaterial and passing them as uniforms makes it simple to mix them in the shader, but I prefer to utilize existing specular maps, environment map ...

"How to include a JavaScript file from the node_modules folder in an Angular project

Currently, I am working on integrating an npm package called svgedit into my angular app. The installation of the package was successful; however, when attempting to reference a JavaScript file, I encountered the following error: net::ERR_ABORTED 404 ( ...

The display/block feature will only function if the div element is contained within a table

I am facing an issue with hiding/showing two div elements alternatively. The code works perfectly when the divs are placed within a table, but fails when they are not in a table due to compatibility issues with Internet Explorer. I prefer not to use a tabl ...

Troubleshooting Bootstrap Grid Alignment Problem During Zooming

I'm facing an issue with the alignment of my bootstrap grid. Currently, I have three main columns in my layout - one for the left sidebar, one for the blog posts, and one for the right sidebar. My challenge lies in trying to add a yellow bar on top ...

Troubles with the functionality of the Ajax loadmore feature

I am struggling with my loadmore function. It works perfectly when I start with 4 items, but not so well when I want to display 8 items from the beginning. Here is my current code: var limit = 8; var offset = 0; $('#loadmoreprojects').click(fu ...

The link background color is malfunctioning

I'm having trouble getting the background color to show for the active link class. The border changes to dashed, but the color remains the same. Can anyone help me figure out what's going on? I'm new to CSS and feeling a bit frustrated. Also ...

Making AJAX requests repeatedly within a loop

My current challenge involves implementing multiple ajax requests within a loop to populate several dropdown lists. Running the requests sequentially has resulted in only the last item in the loop being populated with values. var targetcontrols = []; ...

Node app experiencing port exhaustion within Azure Function

Currently, I am in the process of developing an Azure Function that is responsible for making a high volume of outgoing HTTP requests. However, I have noticed that periodically it reaches a limit where all requests time out for a brief period of a couple m ...

What could be causing the failure of Jsonresult with parameter in my Ajax request?

Here is the code snippet from my view. $(function () { $('#buttonx').on("click", function (e) { e.preventDefault(); $.ajax({ url: 'Ficha/VerificarPatrocinador', ...