Tips for eliminating any hidden image whitespace in a category filtering system

I recently implemented a category filter feature that hides images I don't need. When I click on the car logo at the top, I want to hide every element that doesn't have the "car" id. However, I'm facing an issue where hidden images still retain their dimensions.

For example:

https://i.sstatic.net/Exc5E.png

https://i.sstatic.net/1dNQJ.png

https://i.sstatic.net/H3JKQ.jpg

CSS code:

.block_container{
    flex-wrap: wrap;
    width: 632px;
    height: 350px;
    overflow-y: scroll;
    background-color: rgba(201, 201, 201, 0.5); 
    border-radius: 10px;
    box-sizing: border-box;
    display: block;
}
.game_item{
    margin: 13px;
    width: 176px;
    height: 100px;
    border-top-left-radius: 10px;
    border-top-right-radius: 10px;
    box-sizing: border-box;
    float: left;
    transition: all 0.4s ease-in-out;
}
.hide{
    transform: scale(0);
    width: 0;
    height: 0;
    padding: 0;
    transition: all 0.4s ease-in-out;
}

JavaScript code:

$(document).ready(function(){
    $('.category_item').click(function(){
        var category=$(this).attr('id');

        if(category=='all'){
            $('.game_item').addClass('hide');
            setTimeout(function(){
                $('.game_item').removeClass('hide');
            }, 300);
        } else{
            $('.game_item').addClass('hide');
            setTimeout(function(){
                $('.'+category).removeClass('hide');
            }, 300);
        }
    });
});

Answer №1

It seems like the issue may be related to the margin property. Setting it to 0 for the class ".hide" should help achieve the desired outcome.

Here is a working example:

$(document).ready(function(){
    $('.category_item').click(function(){
      var category=$(this).attr('id');

      if(category=='all'){
        $('.game_item').addClass('hide');
        setTimeout(function(){
          $('.game_item').removeClass('hide');
        }, 300);
      } else{
        $('.game_item').addClass('hide');
        setTimeout(function(){
          $('.'+category).removeClass('hide');
        }, 300);
      }
    });
});
.block_container {
    flex-wrap: wrap;
    width: 632px;
    height: 350px;
    overflow-y: scroll;
    background-color: rgba(201, 201, 201, 0.5); 
    border-radius: 10px;
    box-sizing: border-box;
    display: block;
}

.game_item {
    margin: 13px;
    width: 176px;
    height: 100px;
    border-top-left-radius: 10px;
    border-top-right-radius: 10px;
    box-sizing: border-box;
    float: left;
    transition: all 0.4s ease-in-out;
}

.hide {
    transform: scale(0);
    width: 0;
    height: 0;
    padding: 0;
    margin: 0;
    transition: all 0.4s ease-in-out;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div>
  <button id="all" class="category_item" type="button">all</button>
    <button id="car" class="category_item" type="button">car</button>
    <button id="house" class="category_item" type="button">house</button>
    <button id="puzzle" class="category_item" type="button">puzzle</button>
</div>
<div class="block_container">
    <img src="https://picsum.photos/id/234/200/300" class="game_item puzzle" />
    <img src="https://picsum.photos/id/235/200/300" class="game_item puzzle" />
    <img src="https://picsum.photos/id/236/200/300" class="game_item house" />
    <img src="https://picsum.photos/id/237/200/300" class="game_item car" />
    <img src="https://picsum.photos/id/238/200/300" class="game_item car" />
    <img src="https://picsum.photos/id/239/200/300" class="game_item house" />
</div>


In addition, instead of manipulating width, height, and transform properties, simply using opacity: 0; and display: none; (with jQuery's hide() method) might suffice.

Here is an alternative working example:

$(document).ready(function(){
    $('.category_item').click(function(){
      var category=$(this).attr('id');

      if(category=='all'){
        $('.game_item').addClass('hide');
        setTimeout(function(){
          $('.game_item').show().removeClass('hide');
        }, 300);
      } else{
        $('.game_item').addClass('hide');
        setTimeout(function(){
          $('.game_item').hide();
          $('.'+category).show().removeClass('hide');
        }, 300);
      }
    });
});
.block_container {
    flex-wrap: wrap;
    width: 632px;
    height: 350px;
    overflow-y: scroll;
    background-color: rgba(201, 201, 201, 0.5); 
    border-radius: 10px;
    box-sizing: border-box;
    display: block;
}

.game_item {
    margin: 13px;
    width: 176px;
    height: 100px;
    border-top-left-radius: 10px;
    border-top-right-radius: 10px;
    box-sizing: border-box;
    float: left;
    transition: opacity 0.4s ease-in-out;
}

.hide {
    opacity: 0;
    transition: opacity 0.4s ease-in-out;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div>
    <button id="all" class="category_item" type="button">all</button>
    <button id="car" class="category_item" type="button">car</button>
    <button id="house" class="category_item" type="button">house</button>
    <button id="puzzle" class="category_item" type="button">puzzle</button>
</div>
<div class="block_container">
    <img src="https://picsum.photos/id/234/200/300" class="game_item puzzle" />
    <img src="https://picsum.photos/id/235/200/300" class="game_item puzzle" />
    <img src="https://picsum.photos/id/236/200/300" class="game_item house" />
    <img src="https://picsum.photos/id/237/200/300" class="game_item car" />
    <img src="https://picsum.photos/id/238/200/300" class="game_item car" />
    <img src="https://picsum.photos/id/239/200/300" class="game_item house" />
</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

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 ...

Show the precise selection from one dropdown menu based on the chosen value in another dropdown menu

How can I retrieve the scheduleID value from a specific MovieID value within a comboBox? Movie Selection Box: <?php $query = "select * from ref_movies ORDER BY MovieID"; $result = mysql_query($query) or die (mysql_error()); echo '<s ...

Adding picture to table using JavaScript

I am currently developing a Web application using the Play Framework technology. In one of my scala.html views, I have successfully displayed an image with the following code: <img src="@routes.Assets.at("images/image/1003.png")" width="30" height="30" ...

Maximizing the efficiency of this JavaScript code

Is there a way to optimize this code for better efficiency? I'm trying to enhance my coding skills and would appreciate some feedback on this particular section: $(function(){ $('#buscar').focus(function(){ if(($(this).val() === ...

Experimenting with Jquery hyperlinks beyond the realm of Google Chrome

Is there a way to verify a jQuery link without relying on the inspect element feature in Google Chrome? I initially thought that was the issue because the links failed to load, but even after correcting it, the show function still does not perform the anim ...

Execute angular.js as a callback function, such as within a $.ajax call

In developing my app, I am primarily working with two key JavaScript files: resources_loader.js and app.js. The role of resources_loader.js is to load some JSON files that are utilized by app.js. However, the issue arises when considering the sequence in ...

I am encountering an issue with the jquery.min.js file not loading when I try to utilize the Google CDN for jQuery

Currently in the process of learning about jQuery and testing its functionality, however, I am facing an issue where changes are not being reflected. The error message states that it is unable to load. I have confirmed that I am using Google CDN and also h ...

"Switching from vertical to horizontal time line in @devexpress/dx-react-scheduler-material-ui: A step-by-step guide

Is there a way to switch the Time to a horizontal line using @devexpress/dx-react-scheduler-material-ui? <WeekView startDayHour={7} endDayHour={20} timeTableCellComponent={TimeTableCell} dayScaleCellComponent={DayScaleCell} /> Click ...

Can you suggest a method to align this form to the center?

Take a look at this image Hello there! I am trying to center the login form on my webpage and I have added this code to my index file: Here is the complete code snippet: <section class="row" justify-content-center> <section class="col-12-sm ...

Inaccurate data is being shown by the Ajax method

I have a question that I haven't been able to find a satisfactory answer for: Recently, I started learning about AJAX methods and I'm trying to post some information processed by a php page named page.php. In my HTML file, I've included th ...

Dealing with bodyParser errors in Express.js

Whenever I try to upload an image that exceeds my 5mb limit, I get hit with a payload error. Is there a better way to handle payload errors, like sending JSON or HTML responses instead? PayloadTooLargeError: request entity too large at readStream (D:&b ...

Arranging images effortlessly using jQuery sortable feature in HTML

I'm currently working on creating sortable images using jQuery in an HTML document. I've encountered an issue with the source code here. I have implemented the jQuery sortable function in a functions.js file and successfully tested it on another ...

Increase the date and time by a specified number of days

After searching for solutions on how to add a specific number of days to a given date, I came across various methods. However, my requirement is to add days to both Date and Time in the format MM-DD-YYYY HH:MM:SS. I attempted a method which worked fine wh ...

A tool that enhances the visibility and readability of web languages such as HTML, PHP, and CSS

Looking to organize my own code examples, I need a way to display my code with syntax highlighting. Similar to how Symfony framework showcases it on their website: http://prntscr.com/bqrmzk. I'm wondering if there is a JavaScript framework that can a ...

Is assigning key value pairs a statement in JavaScript?

I am curious to learn about the following code snippet: var a = {x:function(){},y:function(){}} Can we consider x:function(){} to be a statement within this code? ...

Begin a SeleniumWebDriver session after Google Chrome has been launched beforehand

I am looking to create an automation using SeleniumWebDriver and Node.js, but I am facing an issue where I cannot start the automation if Google Chrome is already open and in use by the user. Currently, my workaround is to close all instances of Chrome be ...

Issue with exporting VueJS-generated HTML containing checkboxes results in loss of checkbox checked state

In the template of my component, I have a checkbox code that looks like this: <div ref="htmlData"> <input type="checkbox" class="mycb" :id="uniqID" :disabled="disabled" v-model="cbvalue" > &l ...

Improprove the Express Router in a Node.js application

Is there a way to avoid repeating the isUserAuth and isAdminAuth middleware on each endpoint? Can I apply them just once so they work for all routes without having to specify them individually? const { createBranch, getAllBranch, getBranch } = require(&apo ...

Is there a way to set up my node package to automatically load sources without having to specifically mention the src directory?

My Node package is structured as follows: ./my-package ./src ./index.js ./a.js ./b.js README.md package.json The package.json file specifies "main": "./src/index.js" and the module loads correctly. However, to import a specific JavaScri ...

Search for data within a nested array using MongoDB aggregation techniques

In my data structure, I have nested array documents that are outlined below: countries: [ { "id": "id of country", "cities": [ { "id": "id of city 1", "areas": [ ...