1

Basically I am combining a filter/sort script with a gallery/thumbnail script. Everything works well apart from changing the opacity of the elements when clicking on the new image. ie. Click on new thumbnail, fades to 1.0, old one fades to 0.3.

I can solve one problem only to have another take place.

For example:

I can get the previous element to fade back to 0.3 opacity and the new element to fade in to 1.0, but only by selecting the entire list using: #thumbs ul.thumbs li. This works perfectly but when the filter kicks in and you have only 3/9 list items showing, when you click on another thumbnail, everything fades back to 0.3, causing all of the thumbnails to reappear.

I was thinking of using a counter to add a class to the clicked item, which then if that class is applied, the element will fade back to 0.3 and the class is then removed. Something like this.

$("#thumbs ul.thumbs li").click( function()
{
    if($("#thumbs ul.thumbs li").hasClass("counter")) {
        $("#thumbs ul.thumbs li").fadeTo('fast', 0.3);
        $(this).removeClass("counter");
    }
    $(this).addClass("counter");
    $(this).fadeTo('fast', 1.0);
});

I know exactly where my problem is. Trying to fade the list back to 0.3. I cannot think of the selector to use that will pick that one class that has the counter and then fade it back. I tried this but of course that applies to the clicked item.

I want to choose the element that has the counter style applied #thumbs ul.thumbs li .counter and then fade that particular items li element back to 0.3.

The reason why it has to be this is the gallery/thumbnail script forces the opacity on the elements li. Now I have tried to work my way through to find that but I cannot, so I thought to write a script to overwrite it.

As I mentioned above, I got it to work without taking the filter into account.

Another option I was thinking of was trying to find the previously clicked item and applying the opacity to that list item.

I am using Galleriffic Example 2 - Thumbnails and Gallery along with MixitUp.

I knew there would be incompatibilities but I have gotten it all to work apart from the opacity. So so so so close now.

Any help would be greatly appreciated. I can provide more info if needed.

I believe all I am missing is the correct selector. I am just at that stage where I cannot get myself out of the hole that is preventing me from thinking of the right selector. I am so close but I just cannot think of it. I know it will end up being something simple and I will kick myself for writing all of this. The DOM is frustrating me. Trying to get the parents and siblings all correct whilst following the gallery/thumbnails code.

Cheers

EDIT:

Using Jack Donnelly's method below, I was able to fix the problems I was facing. However, the thumbnail was still being added to the list even if it did not have a matching filter. This works but I would like to make it so that I dont have to call the code each time for each filter:

if ($("#filter-controls li.web").hasClass("filter-selected")){
    if ($(this).hasClass("web")){
        // Select our `li` element with the `.counter` class
        var $counter = $("#thumbs ul.thumbs li.counter");
        // Check whether this element exists
        if ($counter.length > 0) {
            // If it does exist, make it fade out
            $counter.fadeTo('fast', 0.3);
            // Finally, remove the counter class from our counter element
            $counter.removeClass("counter");
        }
        $(this).addClass("counter");
        $(this).fadeTo('fast', 1.0);
    }
}

I have come across this below but am having trouble implementing it with my existing code:

jQuery: How to check if two elements have the same class

EDIT: I ended up fixing this by removing the fadeto and using a class instead. Although I dont like using !important, it is really the only way without a massive headache in this case.

$("#thumbs ul.thumbs li").click( function()
{
    var $counter = $("#thumbs ul.thumbs li.counter");
    // Check whether this element exists
    if ($counter.length > 0) {
        $counter.removeClass("counter");
    }
    $(this).addClass("counter");
});

CSS has this:

.counter {
    opacity: 1.0 !important;
}

This is nice and simple, and the class does not reset when using the filter, keeping the opacity at 1.0 on the currently selected project.

A better outcome than expected.

Community
  • 1
  • 1
Curley5959
  • 119
  • 2
  • 3
  • 12
  • @Anup *you* may not have the patience to read it all, that doesn't mean others do not. – James Donnelly Jan 16 '14 at 11:34
  • @JamesDonnelly ohh yess...dats why so much responses & answers for this question..! :) – Anup Jan 16 '14 at 11:36
  • 1
    And yet if I made it too short without explaining what I am doing, I would get flamed because people do not know what I am talking about. Sorry for wasting an extra 30 seconds of your time. – Curley5959 Jan 16 '14 at 11:38
  • @Anup the question was asked 2 minutes before you posted your comment... That argument is moot. – James Donnelly Jan 16 '14 at 11:40

1 Answers1

1

This part of your code is where your problem lies:

if($("#thumbs ul.thumbs li").hasClass("counter")) {
    $("#thumbs ul.thumbs li").fadeTo('fast', 0.3);
    $(this).removeClass("counter");
}
$(this).addClass("counter");

This code:

  1. Checks whether any of your li elements pulled by the #thumbs ul.thumbs li selector have a .counter class;
  2. If so, it then fades all the li elements to 0.3 opacity;
  3. It then removes the .counter class from this, rather than the element which has the .counter class;
  4. It then gives the .counter class back to this.

The this within your if statement refers to the li element which was clicked on; not the li element which has the .counter selector.

Without seeing your code in action, I can't give a 100% accurate answer, however I can make a guess that this is what you'll need:

// Select any li element with the counter class
var $counter = $("#thumbs ul.thumbs li.counter");

// Check whether any $counter elements exist
if ($counter.length > 0) {
    // If they does exist, make them fade out
    $counter.fadeTo('fast', 0.3);

    // Finally, remove the counter class from them
    $counter.removeClass("counter");
}

// Add the counter class to the li element which was clicked on
$(this).addClass("counter");
James Donnelly
  • 126,410
  • 34
  • 208
  • 218
  • Thanks for your response. Yes I explained that briefly in the slab of text above. I appreciate the summary. Any ideas on the right selector? EDIT: Just noticed the Update. Cheers. Will look now – Curley5959 Jan 16 '14 at 11:43
  • @Curley5959 the selector you've used in your question is `#thumbs ul.thumbs li .counter`; this selects the `.counter` *descendant(s)* of `... ul.thumbs li`, *not* the `li` element(s) with the `.counter` class. – James Donnelly Jan 16 '14 at 11:45
  • Yes I was aware of that and was trying to find the correct selector. I see that by using li.counter it selects the list element. A basic CSS trick. I knew it was something simple. It works as promised. However. Now I have the problem of the element that has the counter adding itself to the list of thumbnails, even if it is not part of the filter. – Curley5959 Jan 16 '14 at 11:49
  • @Curley5959 I can't really help you with that as you haven't provided any extra code. Would it be possible for you to replicate this issue on [JSFiddle](http://jsfiddle.net)? – James Donnelly Jan 16 '14 at 11:50
  • @Curley5959 for future users who may view this question it would be more beneficial to link to a working reproduction of this issue. – James Donnelly Jan 16 '14 at 11:58
  • Whatever I did, JSFiddle would not load the Gallery for me.. I have narrowed it down anyway to matching classes. Had to update first post. – Curley5959 Jan 16 '14 at 12:57