Grow your CSS skills. Land your dream job.

jquery show/hide and some other questions

  • # October 2, 2008 at 4:06 pm

    Question:

    I’m using jquery to show/hid content on a website. I have just the part I’m trying to show/hid content with here:

    http://dev.cssgirl.com/r/rondaxe/cmc.html

    As you see I’ve got my images cut up and sprited, and the hover actions working and on the top two links (drug product development and process research + development) I have the links hooked up with some jquery to hide some content until their clicked on then it’s shown.

    Here’s what I want to do that I can’t quite figure out:

    I want those bubbles to stay green when the content is being displayed for it. I know kinda how I can do it (have a class added to that link when it’s clicked and the content is showing) but how to I tell the jquery/DOM to do that?

    Thanks.
    Lindsey

    # October 2, 2008 at 8:06 pm

    Weird question:

    What’s up with the jump and content being shown near bottom of circles when clicked in IE6?

    # October 2, 2008 at 11:31 pm

    This is how you can tell jQuery to add a class:

    Code:
    $(‘a.research-dev’).addClass(“active”).click(function() {
    hideAll();
    $(‘#research-dev’).show(400);
    return false;
    });

    Then just have the active class replicate the hover class:

    Code:
    .cmc a:hover, .cms a.active {
    background:url(images/cmcon.gif);
    }

    You also have a lot of repetitive code, maybe try and put that stuff into it’s own function, so hiding all those div’s is much easier:

    Code:
    function hideAll() {
    $(‘#research-dev’).hide(400);
    $(‘#chem-dev’).hide(400);
    $(‘#fermentation’).hide(400);
    $(‘#analytical’).hide(400);
    $(‘#regulatory’).hide(400);
    $(‘#quality’).hide(400);
    $(‘#strategy’).hide(400);
    $(‘#chain’).hide(400);
    $(‘#product-dev’).hide(400);
    $(‘a’).removeClass(“active”); // Also remove class on anchor
    };
    pab
    # October 3, 2008 at 1:44 am

    ah jquery you make writing scripts so much easier, Lindsey I just got karl Swedberg’s book in the mail, I highly recommend it.

    Bear Bibault’ book is good too.

    # October 3, 2008 at 8:34 am

    Thanks Chris and pab. I’ll definitely check out those books.

    My first real messing around and editing things my way with jquery.

    Really appreciate the help!

    # October 3, 2008 at 9:39 am

    Ok, sooo hopefully this will be the last question…

    I tried this:

    Code:
    $(‘a.research-dev’).addClass(“active”).click(function() {
    hideAll();
    $(‘#research-dev’).show(400);
    return false;
    });

    But it made all the circles green onload. So I tried switching it and adding the active class within the click(function() { like so:

    Code:
    $(‘a.research-dev’).click(function() {
    hideAll();
    $(‘#research-dev’).show(400);
    addClass(“active”)
    return false;
    });

    Make the circles load as blue again, but still not keeping green on click.

    So, I tried using "switchClass" instead, with same results. Not staying green. So I’m stuck again.

    Here’s my jquery:

    Code:
    $(document).ready(function() {
    // hides the slickbox as soon as the DOM is ready
    // (a little sooner than page load)
    $(‘#product-dev’).hide();
    $(‘#research-dev’).hide();
    $(‘#chem-dev’).hide();
    $(‘#fermentation’).hide();
    $(‘#analytical’).hide();
    $(‘#regulatory’).hide();
    $(‘#quality’).hide();
    $(‘#strategy’).hide();
    $(‘#chain’).hide();

    function hideAll() {
    $(‘#research-dev’).hide(400);
    $(‘#chem-dev’).hide(400);
    $(‘#fermentation’).hide(400);
    $(‘#analytical’).hide(400);
    $(‘#regulatory’).hide(400);
    $(‘#quality’).hide(400);
    $(‘#strategy’).hide(400);
    $(‘#chain’).hide(400);
    $(‘#product-dev’).hide(400);
    $(‘a’).removeClass(“selected”); // Also remove class on anchor
    };

    // toggles the slickbox on clicking the noted link
    $(‘a.prod-dev’).click(function() {
    hideAll();
    $(‘#product-dev’).show(400);
    addClass(“selected”);
    return false;
    });

    $(‘a.research-dev’).click(function() {
    hideAll();
    $(‘#research-dev’).show(400);
    switchClass(‘research-dev’, ‘research-on’, 400);
    return false;
    });

    $(‘a.chem-dev’).click(function() {
    hideAll();
    $(‘#chem-dev’).show(400);
    addClass(“selected”);
    return false;
    });

    $(‘a.fermentation’).click(function() {
    hideAll();
    $(‘#fermentation’).show(400);
    addClass(“selected”);
    return false;
    });

    $(‘a.analytical’).click(function() {
    hideAll();
    $(‘#analytical’).show(400);
    addClass(“selected”);
    return false;
    });

    $(‘a.regulatory’).click(function() {
    hideAll();
    $(‘#regulatory’).show(400);
    addClass(“selected”);
    return false;
    });

    $(‘a.quality’).click(function() {
    hideAll();
    $(‘#quality’).show(400);
    addClass(“selected”);
    return false;
    });

    $(‘a.strategy’).click(function() {
    hideAll();
    $(‘#strategy’).show(400);
    addClass(“selected”);
    return false;
    });

    $(‘a.chain’).click(function() {
    hideAll();
    $(‘#chain’).show(400);
    addClass(“selected”);
    return false;
    });

    });

    And my CSS/HTML

    Code:



    Product Development
    Research Development
    Chemical Development
    Fermentationt
    Analytical
    Regulatory
    Quality
    Strategy
    Chain

    Lorem ipsum dolor. Set frigilla a.
    Set frigilla a. Lorem ipsum dolor.
    Lorem ipsum dolor. Lorem ipsum dolor. Set frigilla a.
    Lorem ipsum dolor. Set frigilla a. Lorem ipsum dolor.
    Lorem ipsum dolor. Set frigilla a. Lorem ipsum dolor. Lorem ipsum dolor.
    Set frigilla a. Lorem ipsum dolor. Lorem ipsum dolor. Lorem ipsum dolor.
    Lorem ipsum dolor. Lorem ipsum dolor. Set frigilla a.
    Lorem ipsum dolor. Set frigilla a. Lorem ipsum dolor.
    Lorem ipsum dolor. Set frigilla a. Lorem ipsum dolor.

    What am I doing wrong here?

    Demo is here:

    http://dev.cssgirl.com/r/rondaxe/cmc.html

    # October 3, 2008 at 12:22 pm

    I think I led you astray a little bit. You got close though!

    Code:
    $(‘a.research-dev’).click(function() {
    hideAll();
    $(‘#research-dev’).show(400);
    $(this).addClass(“active”);
    return false;
    });

    That will add the class only when you click, which is what you want I think.

    # October 3, 2008 at 6:37 pm

    I don’t know what I did wrong before. I even looked up the addClass thing on jquery.com and add the $(this) but couldn’t get it to work. Whatever though now after I pasted in the way you have shown it does. Many, many thanks ;)!!!

    pab
    # October 3, 2008 at 7:22 pm

    I’ve had that same issue with jquery

    happens once in a blue moon.

    # October 5, 2008 at 10:33 pm

    Yeah, hopefully with more experience I’ll get this down one day ;P

    And I got the finished product up finally if you all want to check it out:

    http://dev.ketanvakil.com/rondaxe.com/s … ulting.php
    and
    http://dev.ketanvakil.com/rondaxe.com/services/

    I even figured out the .hover solution for show/hide all by myself :) Practice, practice, practice!

    Thanks again guys!

    # October 5, 2008 at 10:36 pm

    Looks pretty cool! Good work!

    # October 6, 2008 at 12:15 pm

    Ok, one last question ;) Promise:

    If you view either of those pages in IE6/IE7 you will see little black dots when the hover/click effects are taking place (ie: the show/hide). Looks like an IE issue with the redrawing or whatever you call it. Solutions?

    # October 8, 2008 at 12:03 pm

    This always seems to happen in IE when using show/hide – you also get really nasty un-aliased type (ClearType won’t apply itself for some reason).

    You can workaround it easily by using the sliding effects:

    .slideUp()
    .slideDown()
    .slideToggle()

    instead of

    .show()
    .hide()
    .toggle()

    James

Viewing 13 posts - 1 through 13 (of 13 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".