Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6771865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:28:59+00:00 2026-05-26T15:28:59+00:00

I have a simle bit of jQuery code which hides a group of gallery

  • 0

I have a simle bit of jQuery code which hides a group of gallery objects when the controls are clicked, and then shows the filtered objects one by one.

The issue is that some of the delays are inconsistent and not evenly timed. The delay increments every time an object begins to fadein to make them appear one by one, but one or two appear at the same time on random occurances?

Heres the code:

$(document).ready(function() {

    var controls = $('#portfolio-nav'); 
    var gallery = $('#gallery');

    $('a', controls).click(function(){

        /* setup variables */
        var nav             = $(this).closest('ul');
        var listItem        = $(this).parent();
        var current         = 'current';
        var classes         = listItem.attr("class").split(/\s/);
        var elementsToShow  = new Array();
        var allElements     = gallery.children();

        /* filter classes to excude .current */
        classes = jQuery.grep(classes, function(value){
            return value != current;
        });
        var sector = classes[classes.length -1];

        /* remove all .current classes */
        nav.children('.current').each(function() {    
            $(this).removeClass('current');
        });

        /* add the .current class to the clicked li */
        $(this).parent().addClass('current');

        /* define which elements to show */
        $(allElements).each(function() {

            var element = $(this);          
            var elementSectors = element.attr("class").split(/\s/);

            if(sector !== 'all'){

                if(jQuery.inArray(sector, elementSectors) !== -1){
                    elementsToShow.push(element);
                }

            } else {
                elementsToShow.push(element);
            }

        });

        /* log the elements */
        //console.log(elementsToShow);

        /* hide/show all the elements */
        animateThem(allElements, elementsToShow);

        /* clear the array for the next click */
        elementsToShow.length = 0;

        return false;
    });

    function animateThem(allElements, elementsToShow){

        $(allElements).fadeOut('fast').delay(200);

        $(elementsToShow).each(function(index) {
            $(this).delay(100*index).fadeIn('normal');
        });

    }

});

Heres a quick snippet of the html:

<nav id="portfolio-nav">
    <ul>
        <li class="all current"><a href="#all">All</a></li>
        <li class="branding"><a href="#branding">Branding</a></li>
    </ul>
</nav>

<div id="gallery">
    <div class="grid-3 media-holder branding">
        ...
    </div>
    <div class="grid-3 media-holder marketing">
        ...
    </div>
</div>

Its probably an easy fix as my jQuery isn’t great, but if anyone knows of a reason why this would happen I’d love to know.

Cheers,

Dave

Edit: Had issues with declaring the array elements and was getting an undeclared error…after some tweaking its working as expected (thanks to Dannie Hansen), JSFiddle is below if anyone is interested:

http://jsfiddle.net/daveaspinall/8VKad/9/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:29:00+00:00Added an answer on May 26, 2026 at 3:29 pm

    Not entirely sure, there could be some sort of timing issue.

    Why don’t you give it a try using interval:

    var intCount = 0;
    var interval = setInterval(function () {
        if(typeof elementsToShow[intCount] != undefined) {
             if(elementsToShow[intCount] != "fired") {
                  elementsToShow[intCount].fadeIn('normal');
                  elementsToShow[intCount] = "fired";
                  intCount++;
             }
        } else {
        //End of array, clear the interval
             clearInterval(interval);
        }
    }, 100);
    

    That is how it could be done using an interval to time the events. Give it a try and report back if it worked 🙂

    Edit1 Edited to fix bug. – (Thanks to Brighty)

    Edit2 Fixed the fadeIn function. Give it a try now.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple bit of jQuery which displays the row below the current
I have a very simple bit of code (relies on jQuery): var dom_builder =
I have a very simple bit of jQuery to retrieve my latest Tweet $.getJSON(http://twitter.com/statuses/user_timeline/username.json?count=1,
simple bit of jquery. I have a div with clss linklist, inside it has
I have a pretty simple bit of HTML which I'm attempting (and failing miserably
I have a very simple bit of code that is supposed to capture the
I have a bit of an issue here. As Im new to jQuery this
I have this code written in jQuery to have a simple on hover slide
I have an image gallery which pulls in random images from the folder images/flip_images/.
i have very simple code that i making partial postback by jquery and i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.