views:

80

answers:

3

I'm trying to create a dynamic portfolio gallery where you can hide show items by clicking categories. Everything works apart from adding a class to elements hidden/showing when categories are clicked.

I currently have:

$(document).ready(function() {
 $('ul.filter a').click(function() {
  $(this).css('outline','none');
  $('ul.filter .current').removeClass('current');
  $(this).parent().addClass('current');

  var filterVal = $(this).text().toLowerCase().replace(' ','-');

  if(filterVal == 'all') {
   $('div.portfolio .hidden').fadeIn('slow').removeClass('hidden');
  } else {

   $('div.portfolio').each(function() {
    if(!$(this).hasClass(filterVal)) {
     $(this).fadeOut('normal').addClass('hidden');
    } else {
     $(this).fadeIn('slow').removeClass('hidden').addClass("show");

     $('.portfolio:visible').each(function (i) {
      if (i % 2 == 0) $(this).addClass("last"); // This is the part that doesn't seem to work
     });

    }
   });
  }

  return false;
 });
});

It's this part of the code that seems to have the problem:

$('.portfolio:visible').each(function (i) {
                if (i % 2 == 0) $(this).addClass("last");  // This is the part that doesn't seem to work
            });

Basically, I'm looking to add the class .last to every second "visible" item in my portfolio list.

Anyone?


CSS

/* --------------  Portfolio List  ---------------- */
#portfolio-items {
    font-size: 11px;    
}

#portfolio-items ul, #portfolio-items li {
    list-style:none;    
}
#portfolio-items .portfolio {
    float:left;
    width:265px;
    height:145px;
    margin:0px 60px 50px 0px;
    display:block;
}
#portfolio-items .portfolio .portfolio-client {
    float:left;
    font-size: 11px;
    color: #666;
    text-decoration: none;
}
#portfolio-items .portfolio.last {
    margin-right:0px;   
}
#portfolio-items .portfolio img {
    border:solid 6px #fff;
    -moz-box-shadow: 1px 1px 3px #999; /* Firefox */
    -webkit-box-shadow: 1px 1px 3px #999; /* Safari, Chrome */
    box-shadow: 1px 1px 3px #999; /* CSS3 */
    margin: 0px 0px 5px;

    transition: all .3s; /* in Safari, every animatable property triggers an animation in .2s */
    -o-transition: all .3s; /* in Safari, every animatable property triggers an animation in .2s */
    -webkit-transition: all .3s; /* in Safari, every animatable property triggers an animation in .2s */
    -moz-transition: all .3s; /* in Safari, every animatable property triggers an animation in .2s */

    transform-origin: centre; /* in Safari, the origin for transformation */
    -o-transform-origin: centre; /* in Firefox, the origin for transformation */
    -webkit-transform-origin: centre; /* in Safari, the origin for transformation */
    -moz-transform-origin: centre; /* in Firefox, the origin for transformation */
}

#portfolio-items .portfolio img:hover {
    -moz-box-shadow: 1px 1px 6px #999; /* Firefox */
    -webkit-box-shadow: 1px 1px 6px #999; /* Safari, Chrome */
    box-shadow: 1px 1px 6px #999; /* CSS3 */

    transform: scale(1.05);
    -o-transform: scale(1.05);
    -webkit-transform: scale(1.05);
    -moz-transform: scale(1.05);

    box-shadow: 0px 0px 20px #ccc;
    -o-box-shadow: 0px 0px 20px #ccc;
    -webkit-box-shadow: 0px 0px 20px #ccc;
    -moz-box-shadow: 0px 0px 20px #ccc;
}

.view-project {
    background: url(/themes/cogo/default_site/images/view-project.png) no-repeat right center;
    display: block;
    float: right;
    height: 14px;
    width: 87px;
    padding-right: 20px;
    text-align: right;
}

HTML

<div id="portfolio-items"> 
    <div class="portfolio first cms content-management e-commerce clothing kids shops toys"> 
        <a href="http://ee.dev/portfolio/detail/be-my-bear"&gt; 
            <img src="/images/sized/themes/site_themes/cogo/images/uploads/bemybear-project-254x139.jpg" width="254" height="139" title="Be My Bear" /> 
        </a>
        <a href="http://ee.dev/clients/be-my-bear" class="portfolio-client">Be My Bear</a> 
        <a href="http://ee.dev/portfolio/detail/be-my-bear" class="view-project"><span>View Project</span></a></div> 

    <div class="portfolio last cms content-management business-services"> 
        <a href="http://ee.dev/portfolio/detail/joloda"&gt; 
            <img src="/images/sized/themes/site_themes/cogo/images/uploads/joloda1-254x139.jpg" width="254" height="139" title="Joloda International Ltd" /> 
        </a> 
        <a href="http://ee.dev/clients/joloda-international" class="portfolio-client">Joloda International</a> 
        <a href="http://ee.dev/portfolio/detail/joloda" class="view-project"><span>View Project</span></a></div> 

    <div class="portfolio first buddypress cms content-management wordpress business-services coaching events training"> 
        <a href="http://ee.dev/portfolio/detail/nwwn"&gt; 
            <img src="/images/sized/themes/site_themes/cogo/images/uploads/nwwn-project-254x139.jpg" width="254" height="139" title="NWWN" /> 
        </a> 
        <a href="http://ee.dev/clients/north-wales-womens-network" class="portfolio-client">North Wales Women&#8217;s Network</a> 
        <a href="http://ee.dev/portfolio/detail/nwwn" class="view-project"><span>View Project</span></a></div> 

    <div class="portfolio last e-commerce jewellery shops"> 
        <a href="http://ee.dev/portfolio/detail/italian-world"&gt; 
            <img src="/images/sized/themes/site_themes/cogo/images/uploads/italianworld-254x139.jpg" width="254" height="139" title="Italian World" /> 
        </a> 
        <a href="http://ee.dev/clients/italian-world" class="portfolio-client">Italian World</a> 
        <a href="http://ee.dev/portfolio/detail/italian-world" class="view-project"><span>View Project</span></a></div> 

    <div class="portfolio first cms content-management drupal designers printers"> 
        <a href="http://ee.dev/portfolio/detail/mms-almac"&gt; 
            <img src="/images/sized/themes/site_themes/cogo/images/uploads/mms-project-254x139.jpg" width="254" height="139" title="MMS Almac" /> 
        </a> 
        <a href="http://ee.dev/clients/mms-almac-ltd" class="portfolio-client">MMS Almac Ltd</a> 
        <a href="http://ee.dev/portfolio/detail/mms-almac" class="view-project"><span>View Project</span></a></div> 

    <div class="portfolio last cms content-management charity community training"> 
        <a href="http://ee.dev/portfolio/detail/europe-direct"&gt; 
            <img src="/images/sized/themes/site_themes/cogo/images/uploads/europedirect-project-254x139.jpg" width="254" height="139" title="Europe Direct" /> 
        </a> 
        <a href="http://ee.dev/clients/ectarc" class="portfolio-client">Ectarc</a> 
        <a href="http://ee.dev/portfolio/detail/europe-direct" class="view-project"><span>View Project</span></a></div>     
</div>?

FILTER MENU

<ul class="filter"> 


                                                                    <li class="booking-systems"><a href="http://ee.dev/portfolio/category/booking-systems"&gt;Booking Systems</a></li> 



                                                                    <li class="buddypress"><a href="http://ee.dev/portfolio/category/buddypress"&gt;Buddypress&lt;/a&gt;&lt;/li&gt; 



                                                                    <li class="cms"><a href="http://ee.dev/portfolio/category/cms"&gt;CMS&lt;/a&gt;&lt;/li&gt; 



                                                                    <li class="content-management"><a href="http://ee.dev/portfolio/category/content-management"&gt;Content Management</a></li> 



                                                                    <li class="drupal"><a href="http://ee.dev/portfolio/category/drupal"&gt;Drupal&lt;/a&gt;&lt;/li&gt; 



                                                                    <li class="e-commerce"><a href="http://ee.dev/portfolio/category/e-commerce"&gt;E-Commerce&lt;/a&gt;&lt;/li&gt; 



                                                                    <li class="silverstripe"><a href="http://ee.dev/portfolio/category/silverstripe"&gt;Silverstripe&lt;/a&gt;&lt;/li&gt; 



                                                                    <li class="wordpress"><a href="http://ee.dev/portfolio/category/wordpress"&gt;Wordpress&lt;/a&gt;&lt;/li&gt; 


                                                                    </ul> 
A: 

You'll need to use the :odd selector:

http://api.jquery.com/odd-selector/

Not exactly sure how to implement it in your code, but probably something like $('.portfolio:odd') or even $('.portfolio:visible:odd'). I'll give it a try in jsfiddle in a second.

Litso
I've updated it with html/css above if that helps :)
Ian Jamieson
I changed the script, so that every time you click a link all 'last' classes are removed, and then to all odd visible `.portfolio` classes the class `last` is added again. http://jsfiddle.net/29kjT/ (red border added for clarity on which has a `last` class. Also, the `each()` was not necessary.
Litso
You're a f**king genius! Thanks very mucho. Kicking myself a little though :)
Ian Jamieson
Actually, it's not working right. Go to your http://jsfiddle.net/29kjT/ and click on the filter for e-commerce then click on content management. The red border will appear on the left instead of the right.
Ian Jamieson
I figured out what went wrong; apparently the last classes were removed/added while the fadeIn was still active. I turned it into a callback function, so that everything waits until all fading elements are done animating. http://jsfiddle.net/29kjT/2/
Litso
A: 
  $("tr:even").addClass("even");
  $("tr:odd").addClass("odd");

would give a "zebra stripes" like effect.

Ross
I know about that. It's not what I'm looking for. It has to be applied to visible elements on the fly.
Ian Jamieson
A: 

Well just a minor modification then:

 $("tr:visible:even").addClass("even");
 $("tr:visible:odd").addClass("odd");

but you will have to call it every time you hide/unhide any elements.. And you will also have to remove the classes before also.. something like this:

$("tr").removeClass("even").removeClass("odd");
$("tr:visible:even").addClass("even");
$("tr:visible:odd").addClass("odd");

and i think you should be good!

Uoli