rearrange index by moving all visible items to the front

I have a situation where I need to dynamically adjust the position of elements within a container based on a filter selection. Currently, the filter is working correctly but when elements are hidden, it leaves empty spaces in the layout. I would like to reposition the visible items within the container so that they are always at the beginning, allowing me to style them using CSS selectors like :first-of-type, :nth-of-type(2), and so on.

Here is the code snippet:

$(".trigger-filter").click(function() {
  var filterData = $(this).attr("data-filter");
  if (filterData == "all") {
    $(".team").show("2000");
  } else {
    $(".team").not("." + filterData).hide("2000");
    $(".team").filter("." + filterData).show("2000");
  }
});

HTML:

<div class="team-filters">
  <div class="trigger-filter" data-filter="tarya-fintech">
    <img src="http://outofsite.co.il/traya/wp-content/uploads/2019/11/Group-610.png">
  </div>
  ...
  <div class="trigger-filter" data-filter="all">
    <img src="http://outofsite.co.il/traya/wp-content/uploads/2019/11/Group-72222222.png">
  </div>
</div>
<div class="team-body">
  ...
  // Team members with different categories and images
  ...
</div>

CSS:


  .team-body {
    position: relative;
  }

  // Styling for team filters icons

  // Positioning rules for each category of team member
  
  ...

If you have any suggestions on how to achieve this dynamic repositioning, please share!

Answer №1

Your precise positioning properties are designated as left and top. This allows you to determine the minimum values for top and left. To manage this, create variables such as topInitial, leftInitial, topOffset, and leftOffset to store initial values and the difference between top and left of consecutive elements to be presented. If you have a variable named visibleElements containing the elements to be displayed, follow this algorithm:

var currentTop = topInitial;
var currentLeft = leftInitial;
for (var visibleElement of visibleElements) {
    visibleElement.css('left', currentLeft + 'px').css('top', currentTop + 'px');
    currentLeft += leftOffset;
    currentTop += topOffset;
}

Answer №2

If you could provide the HTML script as well, it would greatly enhance our ability to assist you. One potential solution could involve utilizing the CSS "float" property. Applying "float: left;" to the style of all your filter elements may cause them to align vertically when one is hidden.

Here is a basic example based on my interpretation of your HTML structure:

$(document).ready(function() {
  $("#btnFilter").click(function() {
    $('#item1').hide();
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<div id="containerDiv" style="width:200px; height:100px; border:1px solid black; padding:1%">
  <div id="imageDiv" style="width:40%; border:1px solid black; float:left; height:100%"> image div</div>
  <div id="imageDiv" style="width:40%; border:1px solid black; float:left; margin-left:5%; padding:5%; height:80%">
    <div id="item1" style="width:90%; height:20; border:1px solid black; float:left;">item 1</div>
    <div id="item2" style="width:90%; height:20; border:1px solid black; float:left; margin-top:5%">item 2</div>
    <div id="item3" style="width:90%; height:20; border:1px solid black; float:left; margin-top:5%">item 3</div>
  </div>
</div>
<input type="button" id="btnFilter" value="Let be filter" style="margin-top:20px" />

I hope this explanation proves helpful!

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 to use jQuery AJAX POST to submit form data without refreshing the page and insert it into a MySQL database

After completing three tutorials, one on submitting form data to a php file, another on writing data to a mysql table, and the third on using jQuery AJAX for seamless form submission without page redirects, I encountered an issue. If you're intereste ...

Continue scrolling down until the bottom of the page touches the top

I am currently working on a portfolio project where I want the page to scroll all the way down until the footer image aligns perfectly with the top image. This will create a seamless merging of the header and footer images. Despite searching extensively o ...

Tips for modifying the font of options within select menus with jQuery Mobile

I have a select menu that is dynamically created in javascript; var rating = ["R","RR","RRR","RRRR","RRRRR"]; var SelectMenuBuild = '<select id="updateconditionselect">'; for (var i = 0; i < rating.length; i++) { SelectMenuBuild = ...

Evaluating the DateTime between the client browser and the remote server using C#

Let's consider a scenario: I'm working on an ASP.NET MVC website with a form that allows clients to input an expiry date (using JQuery datepicker). After the form submission, I need to validate on the server if the entered date is past the curre ...

Change the z-index of divs when clicked

When a button is clicked, I want to iterate through a group of absolutely positioned children divs with varying z-indexes. The goal is for the z-index of the currently visible div to decrease on each click, creating a looping effect where only one div is v ...

Can you tell me what jQuery append sends back?

This is both a genuine question and a bit of a rant. Let's say I have the following html code: <ol id="t"></ol> and this jQuery code: var x = $('#t').append('test'); var y = x.append('<ol>hello</ol>& ...

What methods can I use to ensure my images are optimized for mobile users?

I have gone through several other discussions related to this issue, but none of the suggested solutions are working for me. Even with max-width: 100% and height: auto, the images remain unresponsive. They appear fine on browsers like IE, FF, and Chrome on ...

The Bootstrap grid is not aligning to the center when dynamically adjusting sizes

I am currently in the process of developing a store website using bootstrap. One issue I am facing involves creating a grid for my product display. The problem is that the number of grid cells remains constant even when the screen size is reduced. I would ...

What is the best way to comprehend IE comments in a given condition?

I recently came across some conditional comments in a theme I'm working on that dynamically add classes to the html tag depending on the version of Internet Explorer being used. <!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7" ...

Select a hyperlink to access the corresponding tab

I've been playing around with bootstrap and AngularJS. I placed nav-tabs inside a modal-window and have it open when clicking on a link. However, I want the appropriate tab to open directly upon click. For example, if I click on "travel", I want the t ...

Learn how to guide the user to a new page once the ajax call is successful. If not, show an

Recently Updated: Thank you for taking the time to read this. The login form on my website utilizes login.php to verify if a user has entered a registered email address. If the address is registered, it will return "redirect", and if it is not registered, ...

Seeking a window-adjustable table with stationary headers

I have been searching high and low for a solution to my issue but have come up empty-handed. My goal is to create a table with fixed headers that remain visible while scrolling through the rest of the table. The catch is, I also need it to align properly w ...

My .min file is not being compiled by Gulp Sass, however, no error messages are displayed

I am new to using gulp and have everything properly installed. When I run gulp in the terminal, it shows CLI version 2.3.0 and Local version 4.0.2. However, when I make changes to my SCSS file, nothing seems to happen despite gulp running without errors i ...

Space within a receptacle

As a newcomer to bootstrap5, I am encountering whitespace issues on the left side of my screen. While everything looks fine in full-screen mode, the whitespace around the image persists when I test responsiveness. https://i.sstatic.net/gZTq5.jpg Despite t ...

Exciting jQuery animations and transitions

Is there a way in JavaScript to call function or method names using a string? For example, when writing jQuery code that applies an effect to specific targets, can the effect be dynamic and changeable by the user? I'm hoping for something like jQuer ...

There was an issue with the JSON parsing process due to an unexpected token 'o' at position

Trying to extract titles from a JSON object for a specific feature, here's an example of the JSON structure: [ { "title": "Example 1", "url": "http:\/\/www.example1.com\/" }, { "title": "Example 2", "url": "http:& ...

Slider that allows range selection after midday

Currently facing a dilemma using the noUiSlider plugin. I have set up a time range picker starting from 6 am to 6 am the following day. Everything works smoothly until it reaches 23:59, but I need it to display 1:00, 2:00 instead of 25:00, 26:00 for the ...

What is the best way to widen each tab so they fill up the entire width of the content block?

As a newcomer to both programming and this website, I've been exploring various codes for creating vertical and horizontal tabs. I have a specific query about this particular example found at https://jsfiddle.net/eu81273/812ehkyf/: My goal is to adju ...

`Some Items Missing from Responsive Navigation Menu`

Hey there! I'm currently diving into the world of responsive design and I'm attempting to create a navigation bar that transforms into a menu when viewed on a mobile device or phone. Everything seems to be working fine, except that not all the na ...

Utilizing Bresenham's line drawing algorithm for showcasing box contours

I am seeking a similar behavior to what is demonstrated on , but with some modifications to the boxes: div { display: inline-block; width: 100px; height: 100px; border: 1px solid black; cursor: pointer; } div.selected, div:hover { backgroun ...