Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript jQuery delay() and toggle()

  • This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #31078
    GreatPotato
    Member

    Hello… Got a small jQuery question.

    I have the following:

    $(".form h3").click(function() {
    $(this).siblings(".rightcol").slideToggle();
    $(this).siblings(".leftcol").delay(500).fadeToggle();
    });

    or I could have the following (both do the same thing I guess):

    $(".form h3").click(function() {
    $(this).siblings(".rightcol").slideToggle(500, function() {
    $(this).siblings(".leftcol").fadeToggle();
    });
    });

    Which slides down the rightcol and then fades in the leftcol shortly after… Which looks beautiful when it shows. But when it hides, I don’t want the delay… Any help with that? I just need to set the delay only on fade in, and not on fade out…

    #69121
    GreatPotato
    Member

    Figured it out… Basically just built my own toggle by test a variable to see if it was even when an element is clicked…

    var delay = 0;

    $(".form h3").click(function() {
    delay++;

    if(delay%2 == 0) {
    $(this).siblings(".rightcol").delay(500).slideUp();
    $(this).siblings(".leftcol").fadeOut();
    } else {
    $(this).siblings(".rightcol").slideDown();
    $(this).siblings(".leftcol").delay(500).fadeIn();
    }
    });
    #69122
    Chris Coyier
    Keymaster

    I might do this for readability and semantics

    var $el;

    $(".form h3").click(function() {

    $el = $(this);
    $el.data('closed', false);

    if ($el.data('closed')) {
    $el
    .siblings(".rightcol")
    .delay(500)
    .slideUp()
    .end()
    .siblings(".leftcol")
    .fadeOut()
    .end()
    .data('closed', true);

    } else {
    $el
    .siblings(".rightcol")
    .slideDown()
    .end()
    .siblings(".leftcol")
    .delay(500)
    .fadeIn()
    .end()
    .data('closed', false);
    };

    });
    #69036
    GreatPotato
    Member

    Interesting… Will experiment further.

    I have the required effect and it looks awesome, but always nice to be able to tweek it.

    Thanks for the help, Chris!

Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.