0

Here is the site.

I have an unordered list containing a group of photos. The navigation is set up like a filter so that if I click on X, only the photos in the X category get displayed, etc. All of the photos are initially set with the 'visible' class like this:

<ul>
  <li class="visible">photo</li>
  <li class="visible">photo</li>
  <li class="visible">photo</li>
  etc.
</ul>

When a certain filter is clicked, the photos that are not in that category get the 'visible' class removed causing them to fade out. However, while the photos get faded out, they still take up the same amount of space causing large amounts of empty space to appear.

enter image description here

I am trying to get rid of this empty space. My idea was to write code that says:

iflidoes not have the 'visible' class, then hide. else, show.

Below is my attempt. However, this causes none of the photos to show up. Where have I gone wrong?

jQuery(document).ready(function($){

    if (!$( '#gallery-wrap li' ).hasClass( 'visible')) {
        this.hide();
    } else {
        this.show();
    }

});
J82
  • 8,267
  • 23
  • 58
  • 87
  • Sounds like you need to update your css. Change `visibility:hidden/visible;` to `display:none/block;` http://stackoverflow.com/questions/133051/what-is-the-difference-between-visibilityhidden-and-displaynone – PeterKA Dec 23 '14 at 00:38
  • possible duplicate of [What is the difference between visibility:hidden and display:none?](http://stackoverflow.com/questions/133051/what-is-the-difference-between-visibilityhidden-and-displaynone) – PeterKA Dec 23 '14 at 00:41

3 Answers3

2

Can do this with one line

$('#gallery-wrap li').hide().filter('.visible').show();

It first hides all then filters the visible class and shows that subset

OR Can do this with pure CSS

   #gallery-wrap li{ display:none}
   #gallery-wrap li.visible{ display:inline-block}
charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • Actually, I'm trying to get the reverse effect. So if the element has the `visible` class, it should show, otherwise (if there is no `visible` class), it should be hidden. – J82 Dec 23 '14 at 01:00
  • I tried your code but the `li` elements without the `visible` class are still being shown. In the html, it looks like this: `
  • `
  • – J82 Dec 23 '14 at 01:04
  • create a demo with just enough css to see what's going on – charlietfl Dec 23 '14 at 01:06
  • i suspect you were trying to use the script before the elements existed – charlietfl Dec 23 '14 at 01:16
  • Good intuition, charlietfl: always try to figure it out with CSS first, that's my motto at least, ;-) – Marventus Dec 23 '14 at 01:52