Split the list items based on their heights if they exceed the height of the unordered list

I'm exploring ways to transform dynamic content into slides. Essentially, I have a <ul> element that is sized 300x100 pixels. As the list items within it exceed the height of the <ul>, I aim to group these overflowing items into divs, creating multiple sets based on the specified height.

The approach I am experimenting with involves floating the list items, but I'm unsure if this is the correct path or how to proceed further.

CSS:

<style>
    ul {
        height: 100px;
        width: 300px;
    }

    ul li {
        float: left;
    }
</style

HTML:

    <ul>
        <li>first</li>
        <li>ffgggfs</li>
        <li>sffsfsf</li>
        <li>jgjghjgfj</li>
        <li>trtretert</li>
        <li>ghhfhfhgf</li>
        <li>sdfsdfsdf</li>
        <li>fghjjh</li>
        <li>iuyuiy</li>
        <li>cvcvc</li>
        <li>hgjhjg</li>
        <li>tryryre</li>
        <li>kkhjkhjk</li>
        <li>sdfsdfsdf</li>
        <li>khjkhjk</li>
        <li>adfsfafsaf</li>
        <li>syuuyuyu</li>
        <li>sweeerre</li>
        <li>last</li>
    </ul>

jQuery:

<script>
var sumHeight = 0;

$('ul li').filter(function() {

    var $this = $(this),
        pHeight = $this.parent().height();      // parent inner height

    sumHeight += $this.outerHeight(true);       // + block outer height

    return sumHeight < pHeight;
}).insertAfter('ul').wrapAll('<div></div>');

Answer №1

Alright, I didn't provide the exact solution you requested, but it can definitely be customized as needed.

Check out this FIDDLE where I opted for using div elements instead of ul/li. They are quite versatile in terms of styling possibilities.

There is a main container div that holds individual divs for each entry. By adjusting the maxheight value, you can determine how many lines should appear in each "slide".

var maxheight = 100;
var divlistnum = 0;

$('.holder').append("<div class='" + "slidelist" + divlistnum + "'></div>");

$('#clickme').click(function(){
    $(".slidelist" + divlistnum ).append("<div class='littlelist'>Stuff to Add</div>");

    if( $(".slidelist" + divlistnum).height() > maxheight )
      {
       divlistnum = divlistnum + 1;
       $('.holder').append("<div class='" + "slidelist" + divlistnum + "'></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

Having difficulty retrieving the current time of an audio element using jQuery

Currently, I am facing an issue while attempting to manage an audio element for my custom player. Despite numerous attempts, I have been unsuccessful in acquiring the currentTime and duration properties. Below is a snippet of what I've tried: var pla ...

Beautiful prompt interface for HTML

I'm attempting to create a sweet alert using the HTML option: swal({ title: "HTML <small>Title</small>!", text: "A custom <span style="color:#F8BB86">html<span> message.", html: true }); Instead of just text, ...

Adjust the dimensions of the dropdown menus

I've set up 2 dropdown menus, but I'm having trouble adjusting their size. I need them to fill the screen width. Here's my code: <select id="repeatSelect" ng-model="selectedArea"> <option ng-repeat="(key,prop) in result" value=" ...

How to retrieve a JSON item without knowing the name of the parent key

When requesting JSON from Wikipedia's API, the URL is: http://en.wikipedia.org/w/api.php?action=query&prop=description&titles=WTO&prop=extracts&exsentences&explaintext&format=json This is how the response is structured: { ...

JSON.Parse function does not return a valid value

I'm currently developing a polling system. Once a user submits their answer choice, I expect to receive JSON data containing all the answers for display purposes. Upon submitting the AJAX form, the JSON response is as follows: [{"answer_1":0,"answer ...

Is it possible for PHP to dynamically load a file based on a condition set in JavaScript?

I am attempting to dynamically insert a PHP include onto the page once the user scrolls to a specific part of the page. Is this feasible? For example: var hasReachedPoint = false; $(window).scroll(function() { var $this = $(this); if ($this.scrollTo ...

styling a flex container with aligned text and buttons

I'm attempting to align text on the left and button controls on the right within a display: flex div. <CustomFieldContainer> <StyledWellContainer> <FieldDetails key={id}> <H4 bold>{label}</H4> <Styled ...

Ways to allow scroll events on top of an overlay without passing click events

My goal is to develop a unique map annotation tool with custom controls, not relying on the built-in features of map providers. Imagine something like this: https://i.sstatic.net/70Yj7.gif I had the idea of placing a canvas over the map for this purpose ...

Enhancing Navigation in React Native by Passing Multiple Values to the Next Screen

I have a challenge where I can successfully pass the first value of (postONE) from the current screen to Screen Two, and it displays perfectly there. However, I am unable to show the second value of (postTwo). This means that the Next screen only shows the ...

The mat card doesn't quite align with the rest of the page

I am currently utilizing Angular Material and Bootstrap in my project. My aim is to display a mat-card that fits perfectly on all screen sizes. However, I am encountering empty spaces on the right side of the card. While it fits properly on some screen ...

Continuously executing a series of JQuery functions or iterating through them repeatedly

Struggling with a jQuery animation loop issue that has me stuck. I need the animation to repeat continuously, but it's not working despite trying setInterval. Can anyone assist? Here's the fiddle link: https://jsfiddle.net/8v5feL9u/ $(document). ...

Issue with Bootstrap carousel not centered on the page

Currently working on a carousel using bootstrap. The goal is to display 5 images at a time and have it advance by 1 slide each time. The main issue I'm facing right now is that the elements are not properly positioned within the container, and the tr ...

The issue with jQuery Autocomplete not retaining text after AutoPostBack

I recently encountered a puzzling issue with my jQuery Autocomplete field on an ASP.Net Webform. Up until now, everything was running smoothly. However, I also have a need for a DropDownList that triggers onSelectedIndexChanged with AutoPostBack. After ad ...

What is the method for inserting a gap between Bootstrap card components?

Can someone help me figure out how to add space between the two card decks using Bootstrap 4 alpha 6? I've tried using mt-20 on the second card deck but it's not working. I also attempted to wrap them in rows, but that didn't work either: h ...

Tips for storing HTML form data in a local JSON file without the need for PHP or Node.js dependencies

I'm interested in saving any information panel in an HTML file to a local JSON file. For example, if there are blocks like this: <div class="panel"> <div> <input type="text" name="producttype" id=&q ...

Content in the div stretches when viewed on a mobile device

I have created a responsive page that is centered, but when viewed on a mobile window, I want the text to stretch and remove any white space on the sides. How can I achieve this using Bootstrap 4? hr { border: 0; height: 1px; ba ...

Adjust Title Padding Specifically for Mobile Devices

When viewing my blog page on mobile, I noticed that the titles of my blog posts are overlapping with the teasers. To fix this issue, I attempted to add margins to the bottom of the post titles specifically for mobile: https://i.stack.imgur.com/BHi3C.png H ...

Check to see if the data provided is found in the database and retrieve the matching information

Here is a table defined as DB: city ------- cityID cityname store Next, we have an HTML form: <input type="text" class="store"> Objective: My goal is for JavaScript to display an alert after the store value is entered, if it already exists ...

Selecting items from a list at random using the power of math.random() and math.floor()

When attempting to randomize the popping of elements in a list stored in the baby variable, only baby[1] is being popped. How can I make it pop random elements? <body> <h1 id="1">bebe</h1> <h1 id="2">t ...

Enhancing the appearance of buttons with hover effects in Twitter Bootstrap

I am interested in customizing the hover button styling for Bootstrap v3.2. The default behavior is to darken on hover, but I would like to make it lighter instead. Although I checked the Buttons Less variables, I couldn't find an option specifically ...