How can we determine when a row has been modified while using a list of floating-left LIs?

Check out this list of products for an example:

I have a function that is triggered on document ready which goes through each item in the list and adjusts the height of a span wrapping the image to ensure uniform spacing.

My question is, is it possible for the function to detect when a new "row" starts so that the spans display correctly on the first row with appropriate sizes based on the first two items, but switch to the actual image height for single entry rows?

The function currently also sets the height of the LI elements and I might modify it to handle row-based sizing if there's a viable solution.

Here is the function used to calculate the height:

if ( jQuery('.product_list').length ) {

    var tallest = 0;
    var tallest_img = 0;
    jQuery('.product_list li').each( function() {
        if ( jQuery(this).height() > tallest ) { 
            tallest = jQuery(this).height(); 
        }
        if ( jQuery(this).children('a').children('span').children('img').height() > tallest_img ) { 
            tallest_img = jQuery(this).children('a').children('span').children('img').height(); 
        }
    });
    jQuery('.product_list li').height( tallest );
    jQuery('.product_list li span').height( tallest_img );

}

Any insights or suggestions would be greatly appreciated! Thanks!

Answer №1

If you want to track when a row changes, you can do so by monitoring the offset().top of an element.

Assuming that the spans have been styled with display: inline-block since you are setting their heights, it is important to note that you cannot set the height of an inline element directly.

Given that the spans are nested within the lis, there should be no need to adjust the height of the li elements individually (unless I have misunderstood your query).

The following code snippet loops through the spans and assigns them equal heights per row based on the tallest img within each row:

function resizeSpans() {
  var spans = $('ul span'),
      tallest,
      top = $(spans[0]).offset().top;

  spans.css('height', '');
  tallest = $(spans[0]).height();

  spans.each(function(idx1) {
    if ($(this).offset().top === top) {
      tallest = Math.max(tallest, $(this).height());
      spans.each(function(idx2) {
        if (idx2 <= idx1 &&
           $(this).offset().top === top
          ) {
          $(this).css('height', tallest);
        }
      });
    } else {
      tallest = $(this).height(),
      top = $(this).offset().top;
    }
  });
} //resizeSpans

You can incorporate this into a resize handler like so:

$(window).resize(resizeSpans);

Experimentation Fiddle Link

Simply click on the Size spans option at the top. Adjust the frame size to observe the readjusted sizes.

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

Omit a Mongoose field in a query by utilizing the assignment operator

Despite recognizing that this question has been posed previously, I have not been able to find a solution suitable for my specific scenario. Many websites recommend using .select('-queryData'), but I am unsure how to implement it in my case. My ...

Is there a way to eliminate the padding for the bootstrap jumbotron specifically on mobile devices?

Hello, I am currently utilizing the bootstrap jumbotron class and facing an issue - when the screen size drops below 500 pixels, I want to remove the padding that is automatically set by Bootstrap (padding: 2rem 1rem;). However, I'm struggling to over ...

:host-selector for Angular Material dialog

I am currently working with a dialog component provided by angular-material and I need to customize the appearance of the popup dialog. I am aware that there is some support for styling through the component generation: let dialogRef = dialog.open(MyDi ...

Three.js: Buffer geometry does not provide any performance improvement

After examining the Three.js example found at webgl_performance, I decided to try improving performance by converting the model into a buffer geometry using the following code: var buffer = THREE.BufferGeometryUtils.fromGeometry( geometry ); Despite my e ...

Try utilizing the adjacency selector in Less again

Is it feasible to repeat this pattern an unlimited number of times using Less with the provided CSS selectors? I've been looking into using loops, however, I haven't come across any examples that achieve a similar result. .page-section { ba ...

Rearrange the position of the default app name on the navigation bar

I have developed a MVC5 application and by default, the nav-bar displays the "application name", "home", "contact", etc. The application name is positioned on the left side of the screen, but I would like to move it to the center. How can I achieve that? ...

Change the background color of the selectInput component in Shiny to fit your

Take a look at this sample shiny app: library(shiny) ui <- fluidPage(tags$head(includeCSS("www/mycss.css")), selectInput("foo", "Choose", width = '20%', multiple = F, selected = "red1", choices = list(red = c ...

CSS - extend underline text decoration to include :before elements

Currently, I am utilizing a font icon from Icomoon to include an icon in the :before element of an <a>. The functionality is smooth, but the issue arises with the text-decoration: underline, which only applies to the icon and text within the anchor t ...

Ways to refresh my $scope once new data is inserted into the SQL database

As I implement the angularjs/SQL technique to fetch data from a database, the code snippet below demonstrates how it is done: $http.get("retrieveData.php").then(function(response){ $scope.tasks = response.data.tasks; }) In addition, there is a functi ...

Create various lightboxes on a single webpage using JavaScript (Lightbox 2 and 3 are not functioning)

After exploring many tutorials on the internet, I realized that I need to brush up on my JavaScript skills. It has been three years since I last used JavaScript, and now I am diving back into it along with its frameworks. One particular tutorial that caugh ...

Tips for effectively reusing the modal component in Vue.js without encountering re-render issues

I'm encountering an issue where the same component content is being rendered despite having different components (Rendering problem). I have a reusable modal component called modal.vue, so every time I create a new component, I call the modal compone ...

Is there a way for me to incorporate form data into a JSON GET request upon submission?

I am currently working on a form that allows users to input airport information. Here is the code for my form: <form id="myForm" > <div> <label for="airport">Airport:</label> <input ty ...

How to use Jquery to choose a value within an optgroup?

I am struggling to figure out how to set the selected value for an optgroup, ensuring that the option chosen matches the correct label. My issue arises from having two different labels with potentially identical options. Label 1 = Honda Label 2 = Toyota ...

CSS: Centralizing a Div Element with Fluid Width

I've created a popup that appears in the center of the screen using: transform: translate(-50%, -50%); and position: absolute. However, I'm facing an issue where the width of the popup remains fixed instead of adjusting dynamically based on the c ...

In React JS, the material-ui Accordion component's onchange function is failing to retrieve the current value

I am encountering an issue with the @material-ui Accordion where the onChange function is not capturing the current value. For example, when I click on the panel1 icon, it opens panel2 instead of taking the current value on icon click. I have provided the ...

Browser Compatibility in Three.js

Could someone assist me with activating webgl on Internet Explorer version 8? I am able to use Firefox and Chrome without any issues. Your help would be greatly appreciated. ...

Is it possible for a three.js material to have different repeat values for a bump map compared to a texture map?

I'm attempting to reduce the repetitive nature of my texture by implementing a bump map that repeats less frequently. However, it appears to adopt the repeat value of 'landTexture' (64) below instead of the value I assigned it (1). landText ...

I am looking to create a div that can consistently refresh on its own without needing to refresh

I have implemented a comment system using ajax that is functioning well. I am looking to incorporate an ajax/js code to automatically refresh my "time ago" div every 5 seconds so that the time updates while users are viewing the same post. Important note: ...

Using a global variable to change the class of the <body> element in AngularJS

I've recently developed an angularJS application. Below is the index.html <html ng-app="MyApp"> <head> <!-- Import CSS files --> </head> <body class="{{bodylayout}}"> <div ng-view></div> < ...

Unveiling the search bar as it expands horizontally from the center, causing the submit button to slide to adjust

Wishes and Aspirations My goal is to design an .input-group with one input[type=text] and one button[type=button]. Initially, the text box should be hidden, leaving only the button visible at the center of the page. Upon activation, the text box should ...