user2434217
user2434217

Reputation: 21

buggy JQuery .animation()

here i have 6 divs (.sticker) inside a single div, onClicking one of them i would like to fadeOut the others keeping the clicked one in it position (that's why i do the postop/posleft thing) then i want to move it on the middle of the bigger div while it grows by height and width, showing a hided div (.info). The same for closing! So, this code it's working but it's really laggy, it's not smooth as jQuery should be, am i doing something wrong?

Thanks to all the comunity!

$("body").on('click', '.sticker', function () {

    if (!is_open) {
        postop = $(this).position().top;
        posleft = $(this).position().left;
        $('.sticker').not(this).fadeOut(350, function () {
            $(".sticker").css("position", "absolute").css("left", posleft + "px").css("top", postop + "px");
            $(".sticker").animate({
                'top': '0px',
                'left': '300px',
                'height': '480px',
                'width': '750px',
                'left': '90px'
            }, 350);
            $(".sticker").children(".wrap").animate({
                'height': '343px',
                'width': '750px'
            }, 350);
            $(".sticker").find(".imgspace").animate({
                'height': '343px',
                'width': '750px'
            }, 350);
            $(".sticker").find(".info").animate({
                'height': '100px'
            }, 350);
            $('.arrow-left').animate({
                'left': '-20px'
            }, 450);
            $('.arrow-right').animate({
                'left': '880px'
            }, 450);
            is_open = true;

        });
    }
    if (is_open) {
        $(".sticker").children(".wrap").animate({
            'height': '193px',
            'width': '300px'
        }, 350);
        $(".sticker").find(".imgspace").animate({
            'height': '193px',
            'width': '300px'
        }, 350);
        $(".sticker").find(".info").animate({
            'height': '0px'
        }, 350);
        $(".sticker").animate({
            'height': '230px',
            'width': '300px',
            'top': postop,
            'left': posleft
        }, 350, function () {
            $(".sticker").css("position", "static");
            $(".sticker").not(this).fadeIn(300);
            is_open = false;
        });

    }

});

Upvotes: 0

Views: 425

Answers (2)

jammykam
jammykam

Reputation: 16990

As Yotam has commented, difficult to debug without a jsFiddle. But things that stuck out to me (and in no way is this exhaustive and I am no JavaScript expert):

  1. cache your jQuery objects rather than keep requerying the DOM (var $sticker = $('.sticker'))
  2. Chain together your animations on the same objects
  3. What is the difference between $sticker.children(".wrap") and $sticker.find(".imgspace")? Can you not use $sticker.find(".wrap, .imgspace")?

You could further simplify the code by setting your values to variable objects rather than call the same method twice hard coded with different values.

$("body").on('click', '.sticker', function () {

    if (!is_open) {
        var $position = $(this).position(),
            $sticker = $('.sticker');

        $sticker.not(this).fadeOut(350, function () {
            $sticker.css({ 
                        position: 'absolute', 
                        left: $position.left+'px', 
                        top: $position.top+'px'
                    })
                    .animate({
                        'top': '0px',
                        'left': '300px',
                        'height': '480px',
                        'width': '750px',
                        'left': '90px'
                    }, 350);
            $sticker.find(".wrap, .imgspace").animate({
                'height': '343px',
                'width': '750px'
            }, 350);
            $sticker.find(".info").animate({ 'height': '100px' }, 350);
            $('.arrow-left').animate({ 'left': '-20px' }, 450)
                            .animate({ 'left': '880px' }, 450);
            is_open = true;

        });
    }
    if (is_open) {
        $sticker.find(".wrap, .imgspace").animate({
            'height': '193px',
            'width': '300px'
        }, 350);
        $sticker.find(".info").animate({
            'height': '0px'
        }, 350);
        $sticker.animate({
            'height': '230px',
            'width': '300px',
            'top': $position.top,
            'left': $position.left
        }, 350, function () {
            $sticker.css("position", "static")
                    .not(this).fadeIn(300);
            is_open = false;
        });
    }
});

Upvotes: 0

Cam
Cam

Reputation: 1902

You will want to use .siblings to hide all others when you click on one. I would do some research on the jQuery API document. This is where I would start.

Upvotes: 1

Related Questions