tags:

views:

125

answers:

1

Hello!

Problem demo is here: http://www.studioimbrue.com

Currently, when the page loads, all the thumbnails are triggered to dim. I'm trying to make it so that each first thumb remains at full opacity (using :first of course) until another thumb is clicked. I can get it to do it for the first one, but it won't iterate to each . I tried using the each() function, but I couldn't get it to work. My javascript skills aren't that great so if you find a solution, just post the code and then explain (if you can.) Thanks! Below is the current code I'm working with:

$(document).ready(function(){ 
    var activeOpacity   = 1.0,
        inactiveOpacity = 0.6,
        fadeTime = 100,
        clickedClass = "selected",
        thumbs = ".thumbscontainer ul 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 clicked, previous;
     clicked = $(this);
     if (!clicked.hasClass(clickedClass)) {
         previous = $(thumbs+'.'+clickedClass);
         previous.removeClass(clickedClass).fadeTo(fadeTime, inactiveOpacity);
         clicked.addClass(clickedClass).fadeTo(fadeTime, activeOpacity);
     }
 });
});
+1  A: 

You need :nth-child() or :first-child:

$(".thumbscontainer li:nth-child(1)")

And actually, just change the line that reads $(thumbs).fadeTo(1, inactiveOpacity); to this instead:

$(thumbs).not(':first-child').fadeTo(1, inactiveOpacity)
         .end().filter(':first-child').addClass(clickedClass);

:first returns a single item which is index 0 in the result set. :nth-child(1) returns any item that is the first child of its parent, which is subsequently, the first li in each list.

Important: When using :nth-child it is important to realize that the number passed to the selector is 1 based, not 0 based like most arrays or selectors. :nth-child(1) is the first child, :nth-child(2) is the second, and so forth.

Thanks Nick!

Doug Neiner
or `:first-child`, the always-forgotten lonely selector :( Come on Doug...give first-child some love, you know you want to!, or `:nth-child(0n+1)`, ok going to bed.
Nick Craver
@Nick, I feel bad I have forgotten that poor selector. I updated my answer to give it some love :)
Doug Neiner
@Doug - Yay! will do business again, A+++++...+1
Nick Craver
`:nth-child(1)` will select the second child. How does that help?
cletus
@cletus, `:nth-child` is 1 based, not `0` based like `:eq()`. See the docs: http://api.jquery.com/nth-child-selector/ -- And now you owe me an upvote after that downvote :P
Doug Neiner
Excellent! The only problem now is that the first one remains highlighted when another one is clicked (unless it is moused over).
steve
@steve I updated my answer. It was because you were not giving it the `clickedClass`. Be sure to copy both lines, as they are one line of code. The lack of a `;` on the first line is **correct**. The `.end()` just continues the chaining.
Doug Neiner
Haha one problem after another. Now it removes the class from every other thumbnail that's clicked (in other galleries). I will quote Nick though on his ebay-like rating: "A+++++...+1"
steve
@steve, if you want help with *all* your code just say so :) Let me work up a fix for ya... give me a few minutes.
Doug Neiner
I think it has something to do with how the 'thumbs' variable is denoted, so that instead of just changing the ones in this specific UL it's changing all UL's. Maybe the 'thumbs' variable needs to be something like: $(this).parents('.thumbscontainer').child('ul').child('li')edit: Haha I never noticed this before! You're awesome.
steve
@Doug: oops sorry my bad.
cletus
@steve All you needed to do was use `.closest('ul').find('.' + clickedClass)`, but here is the completed code: http://pastie.org/821322 ... let me know if something doesn't work!
Doug Neiner
I updated with the code from pastie, but now the opacity isn't changing when a different thumb is clicked. (I apologize if the site isn't loading/taking a while... of course, when I'm trying to completely revamp my portfolio my host decides to migrate servers!)
steve
@steve Doh! I forgot the `!` in front of the 'hasClass' test. I updated the pastie: http://pastie.org/821322
Doug Neiner
YES! If you were a wizard your name would be Dumbledore.
steve
@steve... haha, at least you didn't say "of OZ"... the whole smoke and mirrors thing :)
Doug Neiner