views:

116

answers:

2

I've gotten help from others on this, but their replies were a little bit too broad to help me... I'm a newb when it comes to javascript so I can't quite wrap my head around their answers (and everything I've tried in the last 3 days hasn't worked.) The working site is here: http://www.studioimbrue.com/beta The problem is that with the thumbnails, once clicking them it adds the .selected class properly but when clicking on another, it fails to strip the .selected class from any of the other thumbnails. If you can just correct the code I have that would be amazing, and if you feel like explaining what I had wrong, go right ahead!

$(document).ready(function(){ 
    var activeOpacity   = 1.0,
        inactiveOpacity = 0.6,
        fadeTime = 100,
        clickedClass = "selected",
        thumbs = "#list li";

    $(thumbs).fadeTo(1, inactiveOpacity);

    $(thumbs).hover(
        function(){
            $(this).fadeTo(fadeTime, activeOpacity);
        },
        function(){
            // Only fade out if the user hasn't clicked the thumb
            if(!$(this).hasClass(clickedClass)) {
                $(this).fadeTo(fadeTime, inactiveOpacity);
            }
        });
     $(thumbs).click(function() {
         // Remove selected class from any elements other than this
         var previous = $(thumbs+'.'+clickedClass).eq();
         var clicked = $(this);
         if(clicked !== previous) {
             previous.removeClass(clickedClass);
         }
         clicked.addClass(clickedClass).fadeTo(fadeTime, activeOpacity);
     });
});
+1  A: 

I think it's as simple as this:

$(thumbs).click(function() {
    var li = $(this);
    var alreadySelected = li.hasClass('selected');

    // Remove selected class from any elements other than this
    $('#list li').removeClass(clickedClass).fadeTo(fadeTime, inactiveOpacity);

    li.addClass(clickedClass).fadeTo(((alreadySelected) ? 0 : fadeTime), activeOpacity);
});

You don't need to calculate which items already have the class, just remove it from all of them then re-add it to the one that's been clicked.

Edit: this should remove the flicker.

Mark B
I just did that and it's still not working :( thanks for trying hahaI also tried just using heirarchy (instead of using #list li I used .thumbscontainer ul li and that didn't work either)
steve
I see what's happening - you're changing the opacity independently of the class, so when you remove it the thumb is still fully opaque. I've modified my code above, give it a try.
Mark B
I just discovered that solution as well! The only thing is now when you click it does a little fade back in instead of just remaining at 100% opacity. Any way to bypass the fadeIn and just remain full opacity? Thanks, you've been extremely helpful.
steve
No problem - I've modified the code again, hopefully something *like* that should work (unfortunately I can't test it at the moment).
Mark B
It almost works without the flash, but I think it's ok having the flash. Thanks again.
steve
A: 

If you're referring to thumbnails of the persons, they work fine on me. Although some menu items doesn't work.

postix
Yes, the menu doesn't work correctly yet, only "Talk to Strangers" and "The Philosophy of Typography"
steve