Forums

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

Home Forums CSS How Do i Make This jQuery Code Shorter And More Efficient?

  • This topic is empty.
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #45167
    Anonymous
    Inactive

    //automatic panel_one expand on load. to remove the automatic expand remove the 3 lines bellow.
    $(document).ready(function() {
    $(“#panel_one”).stop().animate({height:’265px’},400);
    });
    //first panel
    $(“#panel_one”).click(function() {
    $(this).animate({height:’265px’},300);
    $(“#panel_two”).animate({height:’110px’},300);
    $(“#panel_three”).animate({height:’110px’},300);
    $(“#panel_four”).animate({height:’110px’},300);
    });

    //second panel
    $(“#panel_two”).click(function() {
    $(this).animate({height:’265px’},300);
    $(“#panel_one”).animate({height:’110px’},300);
    $(“#panel_three”).animate({height:’110px’},300);
    $(“#panel_four”).animate({height:’110px’},300);
    //fadein when hovering
    });

    //third panel
    $(“#panel_three”).click(function() {
    $(this).animate({height:’265px’},300);
    $(“#panel_one”).animate({height:’110px’},300);
    $(“#panel_two”).animate({height:’110px’},300);
    $(“#panel_four”).animate({height:’110px’},300);
    //fadein when hovering
    });

    //fourth panel
    $(“#panel_four”).click(function() {
    $(this).stop().animate({height:’265px’},300);
    $(“#panel_one”).animate({height:’110px’},300);
    $(“#panel_two”).animate({height:’110px’},300);
    $(“#panel_three”).animate({height:’110px’},300);
    //fadein when hovering
    });

    This is the code for the tiles expand and shrink of this website. http://reallycoolstuff.net/PROJECTS/Unica/

    #137292
    TheDoc
    Member
    #137293
    TheDoc
    Member

    But the first problem with your code is that you are being too specific with your selectors. DRY (Don’t Repeat Yourself) is something that is being completely ignored here. Imagine if you wanted to add three more panels, it’d be a pain in the ass!

    #137300
    Anonymous
    Inactive

    @TheDoc i forgot to mention that i don’t want to use CSS animation because it is not supported in IE browsers. Only jQuery.

    #137301
    TheDoc
    Member

    I completely disagree with supporting animations in legacy browsers. IE7-9 don’t *need* to see the height animating. The product still functions flawlessly.

    Now, if you *must* have it, for reasons I’ll never understand, you could do this: http://codepen.io/ggilmore/pen/7ba4d2ceaeac063cf1ff18f0dce8b14b

    #137353
    Anonymous
    Inactive

    @TheDoc works like a charm. Although how do i change the speed?

    #137384
    TheDoc
    Member
    #137380
    Anonymous
    Inactive

    @TheDoc Thanks.

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